This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I have a string "domain\cdsid", where "\" is the delimiter, all I want is to split the string and just print "cdsid".
Input string : "domain\cdsid"
Output string : "cdsid"
How do I do this ?
Try this (using split) :
String myText = "domain\\cdsid";
System.out.println(myText.split("\\\\")[1]);
Output :
cdsid
In Java String object "\" is used to define any escape sequence character like \n for new line, \t for tab, \\ for having a backslash in the String object.
So instead of writing String object as
String str = "domain\cdsid";
You have to write
String str = "domain\\cdsid";
The first option will give compile time error. Java will expect that after backslash their must be some escape sequence character but it is not their in first case. It will compile time error as
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
In the above compile time error each separate value is a escape sequence character in java.
So your final code will be
String str = "domain\\cdsid";
System.out.println(str.split("\\\\")[1]);
Hope this helps.
Splitting is a way that I will recommended to go when you need all the elements resulting of the operation... this is because the result will generate an Array of Strings (what a waste of memory generating an array to only get ONE element! :) dont you think??)
in your case something like regex or just substrings will gently provide you the correct answer..
consider:
String txt = "domain\\cdsid";
System.out.println(txt.substring(txt.indexOf("\\") + 1));
output:
cdsid
Related
I'm trying to do regex for splitting string when tab is spotted.
I used this :
String line = scan.nextLine(); String Splitted[] = line.split("\t");
but it doesn't work so currently I'm using (which is working for me) :
String line = scan.nextLine(); String Splitted[] = line.split("\\s\\s\\s\\s");
Do you guys have idea why I can't use the "\t" regex?
Yes, \t is a valid Regex, but in Java string literals, a backslash has a special meaning, so to get the Regex symbol \t you'll have to use \\t. But since you are processing user input, you never know what this "tab" really consists of (could be a tab symbol or 4 spaces). So maybe you should just split at (\\t|\\s{2,}) - beware, this is a Java string literal. Hence the double backslash.
EDIT: In my above answer i suspect you don't want to split at single whitespaces too, is that right? In case you do want to split at single whitespaces, you could really just use \\s+ instead.
This question already has answers here:
String.split() *not* on regular expression?
(8 answers)
Closed 2 years ago.
In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.
Finding indices (by indexOf and lastIndexOf) does not use regex, so
String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3
gives me the index I need.
However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...
String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need either of the following:
A way to split into an array by a non-regex target
A way to "convert" a non-regex target string into a regex string
Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?
You need to escape your "target" in order to use it as a regex.
Try
String[] someStringSplit = someString.split(Pattern.quote(target));
and let me know if that helps.
String::split do split without regex if the regex is:
a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
two-char String and the first char is the backslash and the second is
not the ascii digit or ascii letter.
Please see String::split() source code for details.
For escaped '.' target it is going to be split without regex.
You can try this one.
String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);
while (tokenValue.hasMoreTokens()) {
System.out.println(tokenValue.nextToken());
}
This question already has answers here:
How do I print escape characters in Java?
(8 answers)
Closed 7 years ago.
I want to print \n.
String text = "";
text = "\n is used for new line";
but the \n is not showing when I run the code. How to do this?
Escape the \ with \
text = "\\n is used for new line";
If you want to actually write the two chars \and n to output, you need to escape the backslash: \\n.
All escape sequences are listed in the documentation: https://docs.oracle.com/javase/tutorial/java/data/characters.html
You can also use System.lineSeparator(), introduced in Java 8:
String result = "cat" + System.lineSeparator() + "dog";
System.out.print(result);
Output:
cat
dog
In java, the \ char is an escape character, meaning that the following char is some sort of control character (such as \n \t or \r).
To print literals in Java escape it with an additional \ char
Such as:
String text = "\\n is used for a new line";
System.out.println(text);
Will print:
\n is used for a new line
This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 8 years ago.
so I have been looking on here and I can find alot of solutions that either completely remove all white space or just remove spaces, or just remove tabs. Basically what I need/ want is a way to take a string, and turn all double spaces+ or tabs and turn them into a single space. ie
String temp = "This is a test for strings";
String result = "This is a test for strings";
any ideas? possible java library methods?
Use String.replaceAll:
String result = temp.replaceAll("\\s+", " ");
where \\s+ stands for more than one whitespace character.
You can use regExp with method #replaceAll other than that you can first use trim to remove leading and trailing spaces.
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " ");
Here \\s+ is regExp which means one or more spaces which will be replaced with single space by replaceAll method.
Try this:
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " "));
When I try to split a String around occurrences of "." the method split returns an array of strings with length 0.When I split around occurrences of "a" it works fine.Does anyone know why?Is split not supposed to work with punctuation marks?
split takes regex. Try split("\\.").
String a = "a.jpg";
String str = a.split(".")[0];
This will throw ArrayOutOfBoundException because split accepts regex arguments and "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0]; //Yes, two backslashes
When the code is compiled, the regular expression is known as "\.", which is what we want it to be
Here is the link of my old blog post in case you are interested: http://junxian-huang.blogspot.com/2009/01/java-tip-how-to-split-string-with-dot.html