Java backslash split string - java

I have a string in the format like "test\00216243".
I need to split the string in Java based on the backslash '\' .
My Program:
public class StringTest {
public static void main(String[] args) {
String taskOwner = "test\00216243";
String taskArray[] = taskOwner.split(Pattern.quote(System.getProperty("file.separator")));
System.out.println(taskArray[0]);
}
}
When i run this program i am getting the following result but not the result as 'test'. Any help?
Result:
test16243

Just to add on
\ is a special character in regular expressions, as well as in Java string literals. If you want a literal backslash in a regex, you have to double it twice.
When you type "\\", this is actually a single backslash (due to escaping special characters in Java Strings).
Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\" as pattern to match a single backslash.
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String taskArray[] = taskOwner.split("\\\\");
System.out.println(taskArray[0]);
}
output
test
00216243

\002 represents a unicode character. SO i suggest you to split your input according to the character other than alphabet or digit.
String string1 = "test\00216243";
String part[] = string1.split("[^a-z0-9]");
System.out.println(part[0]);
Output:
test

String "test\00216243" should be replaced with "test\\00216243"
Because \ represent the escape sequence, you need to use \\ in the string to represent a \
Try this
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String myarray[] = taskOwner.split("\\\\");
System.out.println(myarray[0]+" "+myarray[1]);
}
Output
test 00216243
Reference:
How to split a java string at backslash

Related

Escape any non-alphanumeric characters in a string using Regex

I would like to escape non-alphanumeric characters occurring in a string as follows:
Say, the original string is: "test_", I would like to transform as "test\_".
In order to do this, one approach I can take by scanning the original string, and constructing a new string and while a non-alphanumeric character is found, append a '\' in front of this character.
But I am wondering if there is any cleaner approach to do the same using regular expression.
You can use the replaceable parameter as shown below:
public class Main {
public static void main(String[] args) {
String s = "test_";
s = s.replaceAll("[^\\p{Alnum}]", "\\\\$0");
System.out.println(s);
}
}
Output:
test\_
Notes:
$0 represents the string matched by the complete regex pattern, [^\\p{Alnum}].
\p{Alnum} specifies alphanumeric character and ^ inside [] is used to negate the pattern. Learn more about patterns from the documentation.
Notice the extra pair of \\ which is to escape \ that has been used to escape \.

How to check weather string contains backslash?

I want to check if a string contains "\" or not. I used the string method contains(), but its not working
String path = "D:\steve\";
if(path.contains("\"){
path = path.replaceAll("\\\\","");
}
Use escape character \
public class test{
public static void main(String[] args) {
String str = "a\\b";
System.out.println(str.contains("\\"));
}
}
A character preceded by a backslash (\) is an escape sequence and has
special meaning to the compiler.
https://docs.oracle.com/javase/tutorial/java/data/characters.html

Representing ^A (Unicode \u0001) correctly in Java when passed as argument

public class TestU {
public static void main(String[] args) {
String str = "\u0001";
System.out.println("str-->"+str);
System.out.println("arg[0]-->"+args[0]);
}
}
Output :
str-->^A
arg[0]-->\u0001
I am passing arg[0] as \u0001
I executed this code in linux, the command line variable is not taken as unicode special character.
The argument you pass from command line is not actually unicode character but it's a String of unicode character which is escaped with \. Ultimately, your String will become \\u0001 and that's why it is printing \u0001. Same way, if you enter \ as a command line argument it will become \\ to escape your backward slash.
While the String you have declared in main is actually unicode character.
String escapedstring = "\\u0001";//in args[0]
String unicodeChar = "\u0001";// in str
So, now you want \\u0001 to be converted into \u0001 and there are lot of ways to achieve that. i.e. you can use StringEscapeUtils#unescapeJava method of utility or you can also try following way.
String str = "\\u0001";
char unicodeChar = (char) Integer.parseInt(str.substring(2));
System.out.println(unicodeChar);
NOTE : You can find other ways to convert unicode String to unicode characters in following question.(Already provided in comment by Marcinek)
How to convert a string with Unicode encoding to a string of letters

Regular expression for password at least 8 characters with alphanumeric/special characters

My test method is here and all the other inputs are fine but the problem is can't test on the string containing \ backslash character,
public static void main(String[] args) {
String reg2 = "^[a-zA-Z0-9#\\#$%&*()_+\\]\\[';:?.,!^-]{8,}$";
System.out.println("".matches(reg2));
System.out.println("Pass".matches(reg2));
System.out.println("P#ssw0r\\".matches(reg2)); //not work
System.out.println("P#ssw0r$".matches(reg2));
System.out.println("P#ssw0r(".matches(reg2));
System.out.println("P#ssw0r)".matches(reg2));
System.out.println("P#ssw0r_".matches(reg2));
System.out.println("P#ssw0r+".matches(reg2));
System.out.println("P#ssw0r'".matches(reg2));
System.out.println("P#ssw0r;".matches(reg2));
System.out.println("P#ssw0r:".matches(reg2));
System.out.println("P#ssw0r?".matches(reg2));
System.out.println("P#ssw0r.".matches(reg2));
System.out.println("P#ssw0r.][".matches(reg2));
}
Please suggest my regular expression to work on the string with \ character, and guide me if my approach is wrong.
Escapse \ character twice (one for java and one for regex)
String reg2 = "^[a-zA-Z0-9#\\\\#$%&*()_+\\]\\[';:?.,!^-]{8,}$";

How to replace \ followed by letters in java String?

I need to delete all tokens that are started with \ and followed by any characters.
I created such a pattern:
input.replaceAll("\\[a-zA-Z0-9]*", "");
But it doesn't work because it doesn't delete \rad from string 5 4\rad.
EDIT:
public static void main(String[] args)
{
String input="Wolf 3 3\4par";
String replaceAll = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println("replaceAll=" + replaceAll);
}
Thank you!
The \ is special both in string literals and in regular expressions. To put an actual \ in a regular expression, you have to escape it twice. You also have to assign the result somewhere, which it wasn't clear from your question you were doing. So:
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
Complete example: Live Copy
import java.util.*;
public class Temp {
public static void main(String[] args) {
String input = "4 5 \\rad";
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println(input);
}
}
Output:
4 5
To create \ literal in regex you need to pass \\ to regex engine. But to create \ literal in String you also have to escape it so you need to write it as "\\".
\ literal in regex engine
\\ regex pattern
"\\\\" String representing regex pattern
Now you are using one \ in your regex pattern regex engine sees it as \[ which escapes [ making it simple literal.
Try this way
input.replaceAll("\\\\[a-zA-Z0-9]*", "");
From
Sorry, but my String is exactly 5 4\rad. Indeed how to delete \rad? – Volodymyr Levytskyi
Try
String k= "5 4\rad";
System.out.println(k.replaceAll("\r\\w*", ""));
Output
5 4

Categories