How to get index of Escape Character in a String?
String test="1234\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Result i get:
-1
Result i need: 4
Your original String doesn't contain \. Which means you are searching something which does not exist. Inorder to add \ to your string. You have to escape while adding
String test="1234\\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Should work.
In your case look at the last line in the table.
I don't think you can get that because when you use an escape character is for java to interpret the following character in a special way. In another words, the escape character and the next character you see in the string are really one entity from the point of view of program being executed.
When you search for "\\", you are searching for the literal character '\' not the escape character.
Here you can see the difference: java fiddle
While \5 is the character with code 0x5 (ENQ), 5 is the character 0x35. See the table.
Related
I've been trying to figure out how we can remove a special character along with its preceding letters within a string.
Let's suppose, there a string with "ABC\n000111". In this case we have to remove the "ABC\" character from the string. So, the result would be n000111.
Can someone help me find the efficient way of doing this?
The Java string literal "ABC\n000111" doesn't contain a backslash: \n is a special character sequence, meaning a single character for a (unix) newline.
If you want to replace \n with n, you can do so:
System.out.println("ABC\n000111".replace('\n', 'n'));
If you want to replace everything up to and including the \n with n, you can do so:
System.out.println("ABC\n000111".replaceAll("^.*\n", "n"));
I have a string as String placeStr="place1*place2*place3"
I want to get array contain place1,place2,place3 as follow:
String[] places=placeStr.split("*");
System.out.println(places[0]);
But it display error E/AndroidRuntime(32118): Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1: *
I think cause is character "*". If I change * by -,it display ok.
How must I do ?
When you use a Regexp, \* means 0 or more instances of the last defined expression. So for example.
s* means match anything that has 0 or more "s". This will match "ss", "sssss" or "".
When we want to actually search for *, and not use it as an operator, we need to escape it, using the \ character. However, the singular \ is used for special characters (such as \s and \t) so we also need to escape that character, with another \.
This results in:
\ (ignore the next slash)
\ (ignore the star)
* (would be 0 or many, but its been ignored so means the literal.)
Or in other words
\\*
And in Java, we can use it like this:
String[] places=placeStr.split("\\*");
System.out.println(places[0]);
Try this using the backslashes \\:
String[] places=placeStr.split("\\*");
System.out.println(places[0]);
use backslashes which changes the meaning of a particular character in java
ie String[] places=placeStr.split("\\*"); It will change the meaning of *
I see at least to correct answers to this problem, good for them however the concepts of why their answers are correct.
As pointed out, the * has special meaning, that is it matches all characters. So in your case, you need to escape it in order for the reg ex engine to match exactly it, so you need * in the regular expression.
However, if you attempt to do:
String[] places=placeStr.split("\*");
you will get a compile error. This is because the \ character has special meanings in strings. So you need to escape it.
This is how you arrive at:
String[] places=placeStr.split("\\*");
you will need to escape the asterisk with a back slash (and then escape that with another backslash)
String[] places=placeStr.split("\\*");
This should allow you to get the results you need
PS this will only print the first element
System.out.println(places[0]);
putting it into a for loop will allow you to print them all
int count 0;
for(string place : places) {
System.out.println(places[count]);
count++
}
I think the print code will work although I haven't tested it
I am trying to match some unicode charaters sequence:
Pattern pattern = Pattern.compile("\\u05[dDeE][0-9a-fA-F]{2,}");
String text = "\\n \\u05db\\u05d3\\u05d5\\u05e8\\u05d2\\u05dc\\n <\\/span>\\n<br style=\\";
Matcher match = pattern.matcher(text);
but doing so gives this exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal Unicode escape sequence near index 4
\u05[dDeE][0-9a-fA-F]+
^
how can I use still use regex with some regex chars (like "[") to match unicode?
EDIT:
I'm trying to parse some text. The text somewhere has a sequence of Unicode characters, which I know their code range.
Edit2:
I am now using ranges instead : [\\u05d0-\\u05ea]{2,} but still can't match the text above
Edit3:
ok, now it's working, the problem was I used two backslashes instead of one, both in the regex and text.
The solution for this is, assuming I know there will be two chars or more:
[\u05d0-\u05ea]{2,}
Here is what causing the exception:
\\u05[dDeE][0-9a-fA-F]}{2,}
^^^^
The java regular expression parser thinks you are trying to match a Unicode code point using the escape sequence \uNNNN so it is giving an exception, because \u requires four hexadecimal digits after it and there is only two of them, namely 05 so you need to change it to \\u0005 if that is what you actually want.
On the other hand, if you want to match \\u in the target string, then you need to quad escape each backslash \ like this \\\\ so to match \\u you need \\\\\\\\u.
\\\\\\\\u05[dDeE][0-9a-fA-F]}{2,}
Finally, if you want to match those Unicode code points literally in your target string then you need to modify our last expression a bit like this:
(?:\\\\\\\\u05[dDeE][0-9a-fA-F]){2,}
Edit: Since there is only one backslash in your target string then your regular expression should be:
(?:\\\\u05[dDeE][0-9a-fA-F]){2,}
This will match \u05db\u05d3\u05d5\u05e8\u05d2\u05dc in your string
<\/span><\/span><span dir=\"rtl\">\n \u05db\u05d3\u05d5\u05e8\u05d2\u05dc\n <\/span>\n<br style=\"clear : both; font-size : 1px;\">\n<\/div>"}, 200, null, null);
Edit 2: If you want to match literal \u05db\u05d3\u05d5\u05e8\u05d2\u05dc then you can't use a range.
On the other hand, if you want to match Unicode code points between 05d0 and 05df then you can use:
(?:[\\u05d0\\u05df]){2,}
It's not clear what you're trying to do. If your goal is to simplify matching a range of Unicode characters, then you need to realize that the hex digits are completely case insensitive, and so your a-fA-F is redundant, even if you could split character literals. Try this to match all Unicode characters in the range:
[\\u05d0-\\u0eff]
Looks like you have unnecessary \\ in your input string. Following works by replacing your specified unicode character range in regex:
String text = "\n \u05db\u05d3\u05d5\u05e8\u05d2\u05dc\n </span>\n<br style=\\";
System.out.println(text.replaceAll("[\u05d0-\u05ea]{2,}", "###"));
OUTPUT:
###
</span>
Note that in our input text you had \\n and \\u05db etc that I have fixed.
I have to remove \ from the string.
My String is "SEPIMOCO EUROPE\119"
I tried replace, indexOf, Pattern but I am not able to remove this \ from this string
String strconst="SEPIMOCO EUROPE\119";
System.out.println(strconst.replace("\\", " ")); // Gives SEPIMOCO EUROPE 9
System.out.println(strconst.replace("\\\\", " ")); // Gives SEPIMOCO EUROPE 9
System.out.println(strconst.indexOf("\\",0)); //Gives -1
Any solutions for this ?
Your string doesn't actually contain a backslash. This part: "\11" is treated as an octal escape sequence (so it's really a tab - U+0009). If you really want a backslash, you need:
String strconst="SEPIMOCO EUROPE\\119";
It's not really clear where you're getting your input data from or what you're trying to achieve, but that explains everything you're seeing at the moment.
You have to distinguish between the string literal, i.e. the thing you write in your source code, enclosed with double quotes, and the string value it represents. When turning the former into the latter, escape sequences are interpreted, causing a difference between these two.
Stripping from string literals
\11 in the literal represents the character with octal value 11, i.e. a tab character, in the actual string value. \11 is equivalent to \t.
There is no way to reliably obtain the escaped version of a string literal. In other words, you cannot know whether the source code contained \11 or \t, because that information isn't present in the class file any more. Therefore, if you wanted to “strip backslashes” from the sequence, you wouldn't know whether 11 or t was the correct replacement.
For this reason, you should try to fix the string literals, either to not include the backslashes if you don't want them at all, or to contain proper backslashes, by escaping them in the literal as well. \\ in a string literal gives a single \ in the string it expresses.
Runtime strings
As you comments to other answers indicate that you're actually receiving this string at runtime, I would expect the string to contain a real backslash instead of a tab character. Unless you employ some fancy input method which parses escape sequences, you will still have the raw backslash. In order to simulate that situation in testing code, you should include a real backslash in your string, i.e. a double backslash \\ in your string literal.
When you have a real backslash in your string, strconst.replace("\\", " ") should do what you want it to do:
String strconst="SEPIMOCO EUROPE\\119";
System.out.println(strconst.replace("\\", " ")); // Gives SEPIMOCO EUROPE 119
Where does your String come from? If you declare it like in the example you will want to add another escaping backslash before the one you have there.
I have to make below statement as string.i am trying,but it's giving invalid character sequence.I know it is basic,But not able to do this.any help on this appreciated.
String str="_1";
'\str%' ESCAPE '\'
Output should be: '\_1%' ESCAPE '\'.
Thanks,
Chaitu
String result = "'\\" + str + "%' ESCAPE '\\'";
Inside a string, a backslash character will "escape" the character after it - which causes that character to be treated differently.
Since \ has this special meaning, if you actually want the \ character itself in the string, you need to put \\. The first backslash escapes the second, causing it to be treated as a literal \ inside the string.
Knowing this, you should be able to construct the resulting string you need. Hope this helps.
String str="_1";
String source = "'\\str%' ESCAPE '\\'";
String result = source.replaceAll("str", str);
Another way to implement string interpolation. The replaceAll function finds all occurrences of str in the source string and replaces them by the passed argument.
To encode the backslash \ in a Java string, you have to duplicate it, because a single backslash works as an escape character.
Beware that the first argument if replaceAll is actually a regular expression, so some characters have a special meaning, but for simple words it will work as expected.
String str="_1";
String output = String.format("'\\%s%%' ESCAPE '\\'",str);
System.out.println(output);//prints '\_1%' ESCAPE '\'