Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to replace "\u0023 \u0024 ab"
with:
"\\u0023 \\u0024 ab" to maintain its encoding before storing it in database.
There can me many different values after \u, not just 0023, 0024.
I tried using str.replace("\"\\"); or ("\u","\\u") but it doesn't work in java because it treats \u0023 as one character.
Any suggestions how to achieve this ?
I tried like this
public class test {
public static void main(String args[]) {
String s = "\u0023";
s = s.replace("\\", "\\\\");
System.out.println(s);
}
}
but it is giving following output as:
#
but I am expecting:
\\u0023
As said by the first answer here, you can retrieve the (numerical) unicode value of a character with:
// works up to Unicode 3.0
String hexString = Integer.toHexString(s | 0x10000).substring(1);
Using this number, simply print it out:
System.out.println("\u" + hexString);
(Note: code is untested: give me feedback if it doesn't work)
Hope this helps!
String s = "\\u0023";
s = s.replace("\\", "\\\\");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example, "Hello" will be turned to a number and 10 will be turned to 10. If I will try again "Hello" I will get the same number I got before.
How can I do that?
Edit:
I don't know what "Hello" be turned to, because I don't have a program which does what I want. I don't want a specific number to be displayed.
You could use String::hashCode for Strings, and first try to parse to an integer to get numbers:
public static int convert(String str) {
try{
return Integer.parseInt(str.trim());
}catch(Exception e) {
return str.hashCode();
}
}
But if you have things like "10 10", you will still get the hash code and not just 1010
In case which word is assigned to a number, use dictionary.
For example you could use a hashmap, where for each string you would store the corresponding integer.
When you will want to get the corresponding integer, you would just seek the value of the string key (in your example "Hello")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following code:
public class Search
{
public static void main(String[] args)
{
int[] whiteList = In.readInts(args[0]);
while(!StdIn.Empty())
{
int key = StdIn.readIn();
...
}
}
%java Search largeW.txt < largeT.txt
How to transform it to C# ?
Here you are:
Console.WriteLine("Input your number: "); // input 4, press enter
var theVar = Convert.ToInt32(Console.ReadLine()); // theVar is 4
Hope this help.
Java and C# aren't too different. You just need to know how to read STDIN, right? For a console application, use Console.ReadLine() or one of the other methods provided by the Console class.
Also keep in mind when converting:
1. With method names, you capitalize the first letter of EVERY word, i.e. MyMethod(). (important because Main() needs to be capitalzed)
2. All classes are inside a namespace block.
3. All of your type conversion tools are under Convert
4. File is a static class. You don't create instances of it like in Java. I recommend looking at File.ReadAllLines(string name).
Other than that the syntax is very similar and it should be fairly easy to convert between the two languages.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can get it done by implementing the logic to do it. However, I am looking for a single method call rather than implement it myself.
Example:
String sampleStr = "This is a sample string";
String resultStr = sampleStr.singleMethodCall("sample");
//resultStr will be "This is a sample"
I am ok if this can be achieved with a third party API.
You can use standard String functions.
String resultStr = sampleStr.substring(0, sampleStr.indexOf("sample") + 6);
Take the substring from position 0 (the start) until the position of "sample", but add another 6 characters so you also include "sample" and the preceeding space.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a string
1,11,cda74944-1c61-4325-8137-83a4ab50cb81-A00123,Bs000000216,20140204185143.811
Can you provide me regular expression or java string function to get the sub string:
cda74944-1c61-4325-8137-83a4ab50cb81-A00123 ?
You split the string:
String[] array = String.split(",");
This will return an Array of String like this
["1", "11", "cda74944-1c61-4325-8137-83a4ab50cb81-A00123", "Bs000000216", "20140204185143.811"]
You can access the part you want in array[2].
If there is a string like this, use pattern:
/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{6}/
The pattern is:
[a-f0-9]++(?>-[a-z0-9]++)+
You can split the String
String[] allStrings= String.split(",");
then try to get allStrings[2] //it will give you the desired result cda74944-1c61-4325-8137-83a4ab50cb81-A00123
A few options:
Using length:
String s = yourString.subString(0, yourString.length() >>> 1);
Using split:
String s = yourString.split(",")[2];
Alternatively, if you have something representing physical data that is always represented similarly, it might be smarter to wrap it in a class instead.