Search results
Nov 20, 2016 · In your case, you want to split a string when there is a "-". You can simply do as follows: String str = "004-034556"; String split[] = StringUtils.split(str,"-"); Output: 004. 034556. Assume that if - does not exists in your string, it returns the given string, and you will not get any exception.
Feb 12, 2013 · You need to escape the dot if you want to split on a literal dot: String extensionRemoved = filename.split("\\.")[0]; Otherwise you are splitting on the regex ., which means "any character". Note the double backslash needed to create a single backslash in the regex. You're getting an ArrayIndexOutOfBoundsException because your input string is ...
Sadly, Java lacks a both simple and efficient method for splitting a string by a fixed string. Both String::split and the stream API are complex and relatively slow. Also, they can produce different results. String::split examines its input, then compiles to java.util.regex.Pattern every time (except if the input contains only a single char ...
Dec 2, 2017 · 1. If the original string contains supplementary Unicode characters, then split() would not work, as it splits these characters into surrogate pairs. To correctly handle these special characters, a code like this works: String[] chars = new String[stringToSplit.codePointCount(0, stringToSplit.length())];
Mar 25, 2012 · Java split() regular expression. 2. Split string in java using regex. 2. Java string split() Regex. 0 ...
Feb 14, 2020 · myString.split("\\s+"); This groups all white spaces as a delimiter. So if I have the string: "Hello[space character][tab character]World". This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab]. As VonC pointed out, the backslash should be escaped, because Java would first try to escape the ...
Oct 5, 2016 · 15. Use Stringutils.split() to split the string by whites paces. For example StringUtils.split("Hello World") returns "Hello" and "World"; In order to solve the mentioned case we use split method like this. String split[]= StringUtils.split("Hello I'm your String"); when we print the split array the output will be :
Jun 20, 2009 · 9. split takes a regular expression, so you can do: String[] terms = myString.split("[-+]"); and it will split when it encounters either + or -. Edit: Note that as Michael Borgwardt said, when you split like this you cannot tell which operator (+ or -) was the delimiter. If that's important for your use, you should use a StringTokenizer as he ...
After some research, I found that indexOf and substring are quite efficient but the examples only have single delimiters or results are returning only a single word/element. Sample code using indexOf and substring: String s = "quick,brown,fox,jumps,over,the,lazy,dog"; int from = s.indexOf(','); int to = s.indexOf(',', from+1);
May 17, 2012 · 405. You could do this: String str = "..."; List<String> elephantList = Arrays.asList(str.split(",")); Basically the .split() method will split the string according to (in this case) delimiter you are passing and will return an array of strings. However, you seem to be after a List of Strings rather than an array, so the array must be turned ...