How can I split a string in Java? - java

Imagine I have this string:
string thing = "sergio|tapia|gutierrez|21|Boston";
In C# I could go:
string[] Words = thing.Split('|');
Is there something similar in Java?
I could use Substring and indexOf methods but it is horribly convoluted. I don't want that.

You can use String.split.
String test = "a|b|c";
String[] splitStr = test.split("\\|"); // {"a", "b", "c"}

String thing = "sergio|tapia|gutierrez|21|Boston";
String[] words = thing.split("\\|");
The problem with "|" alone, is that, the split method takes a regular expression instead of a single character, and the | is a regex character which hava to be scaped with \
But as you see it is almost identical

I would try the String.split method, personally.

Yes, there's something similar.
String[] words = thing.split("|");

It's easy. You just call the split method with a delimiter
String s = "172.16.1.100";
String parts[] = s.split("\\.");

Exactly the same : String.split

Use String.split().

you need to escape the pipe delimiter with \\, someString.split("\\|");

Related

Splitting a string starts with $

I am trying to split a string like "$ 12,9608,03" and just want the numbers and convert to an integer.
For splitting how should I use split() in java as there is a space after the $ sign.
Tried with following:
String[] arr_1=mystring.Split(“[\$, ] “);
String array1=arr_1[0];
Sopln(array1);
You can do what you want, I believe, using this:
String splited = "$ 12,9608,03".replaceAll("[^0-9]", "");
Then you will have splitted only the numbers by commas, but as String. Then you can use, for each String you got, Integer.valueOf() method.
String[] splitted = mystring.split(" ");
String numb = splitted[1];
Just split on the whitespace.
Just like that:
String[] arr_1=mystring.Split("$");
String array1=arr_1[0].trim();
Sopln(array1);
use space in regular experssion to split the string.
String[] split = str.split("( )");
System.out.println(split[1]);
Some fine answers here, to finish off the whole question
and convert to an integer
Here you are:
String myString = "$ 12,9608,03";
String[] splitted = myString.split(" ");
int numb = Integer.parseInt(splitted[1].replaceAll(",", ""));
System.out.println(numb);
To take just the digits, you'd do better with replace:
mystring.replaceAll("[^0-9]", "")
The first parameter is a regex that matches anything but a digit. So this will return you just the number
Stop being insane. Almost everything you will ever do (applies to all readers) has already been done (often better) by the Apache Commons project.
Read the Apache Commons StringUtils API page, pay attention to the RemoveAll method.
Use the StringUtils.RemoveAll method.
Convert the output from the StringUtils.RemoveAll method to an int;
catch any exceptions and handle them appropriately.

use split() to split string like "004*034556"

In my project I used the code to split string like "004*034556" , code is like below :
String string = "004*034556";
String[] parts = string.split("*");
but it got some error and force closed !!
finally I found that if use "#" or another things its gonna work .
String string = "004#034556";
String[] parts = string.split("#");
how can I explain this ?!
Your forgetting something very trivial.
String string = "004*034556";
String[] parts = string.split("\\*");
I recommend you check out Escape Characters.
Use Pattern.quote to treat the * like the String * and not the Regex * (that have a special meaning):
String[] parts = string.split(Pattern.quote("*"));
See String#split:
public String[] split(String regex)
↑
Refer JavaDoc
String[] split(String regex)
Splits this string around matches of the given regular expression.
And the symbol "*" has a different meaning when we talk about Regex in Java
Thus you would have to use an escape character
String[] parts = string.split("\\*");

Split string containing newline characters Java

Say I have a following string str:
GTM =0.2
Test =100
[DLM]
ABCDEF =5
(yes, it contains newline characters) That I am trying to split with [DLM] delimiter substring like this:
String[] strArr = str.split("[DLM]");
Why is it that when I do:
System.out.print(strArr[0]);
I get this output: GT
and when I do
System.out.print(strArr[1]);
I get =0.2
Does this make any sense at all?
str.split("[DLM]"); should be str.split("\\[DLM\\]");
Why?
[ and ] are special characters and String#split accepts regex.
A solution that I like more is using Pattern#quote:
str.split(Pattern.quote("[DLM]"));
quote returns a String representation of the given regex.
Yes, you're giving a regex which says "split with either D, or L, or M".
You should escape those boys like this: str.split("\[DLM\]");
It's being split at the first M.
Escape the brackets
("\\[DLM\\]")
When you use brackets inside the " ", it reads it as, each character inside of the brackets is a delimiter. So in your case, M was a delimiter
use
String[] strArr = str.split("\\[DLM]\\");
Instead of
String[] strArr = str.split("[DLM]");
Other wise it will split with either D, or L, or M.

Case insensitive String split() method

When I perform
String test="23x34 ";
String[] array=test.split("x"); //splitting using simple letter
I got two items in array as 23 and 34
but when I did
String test="23x34 ";
String[] array=test.split("X"); //splitting using capitalletter
I got one item in array 23x34
So is there any way I can use the split method as case insensitive or whether there is any other method that can help?
split uses, as the documentation suggests, a regexp. a regexp for your example would be :
"[xX]"
Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :
"(?i)x"
In this case, x can be any litteral properly escaped.
Use regex pattern [xX] in split
String x = "24X45";
String[] res = x.split("[xX]");
System.out.println(Arrays.toString(res));
You can also use an embedded flag in your regex:
String[] array = test.split("(?i)x"); // splits case insensitive
I personally prefer using
String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String[] parts = modified.split(splitterValue);
In this way you can ensure any regex will work, as long as you have a unique splitter value
In addition to the existing answers, you can use Pattern.CASE_INSENSITIVE flag to convert your regex pattern into a case-insensitive pattern which you can directly use to split your string e.g.
String[] arr = Pattern.compile("x", Pattern.CASE_INSENSITIVE).split("23x34 ");
Demo:
import java.util.Arrays;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("x", Pattern.CASE_INSENSITIVE);
System.out.println(Arrays.toString(pattern.split("23x34 ")));
System.out.println(Arrays.toString(pattern.split("23X34 ")));
}
}
Output:
[23, 34 ]
[23, 34 ]
Java's String class' split method also accepts regex.
To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split
For JavaScript:
var test="23x34 ";
var array = test.split(\x\i);
It's a bit complex, but here's how it could be implemented:
Lowercase both the strings (overall text and search term)
Run the text.split(searchTerm)
This gives you an array of strings that are NOT search terms
By walking through this array, you're getting lengths of each of these strings
Between each of those strings, there must be a search term (with known length)
By figuring out indexes, you can now .slice() the pieces from the original string
You could use a regex as an argument to split, like this:
"32x23".split("[xX]");
Or you could use a StringTokenizer that lets you set its set of delimiters, like this:
StringTokenizer st = new StringTokenizer("32x23","xX");
// ^^ ^^
// string delimiter
This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.

Java split string with "||" taking it as regex instead of string

I have the following string "ABC" and "AAA||BBB"
I am trying to split it using the characters "||" but the split method is taking this as a regex expression, returning an array of characters instead of {"ABC"} and {"AAA", "BBB"}
I have tried scaping the bar with a back slash, but that didn't work.
How can I make the split method to take "||" as a String and not as a regex?
Thanks
Escape the pipes
Use \\|\\| instead
If you don't want to deal with escaping then you can use Pattern#quote:
String[] tok = "AAA||BBB".split(Pattern.quote("||"));
OR simple:
String[] tok = "AAA||BBB".split("\\Q||\\E"));
String[] result = "The||man is very happy.".split("\\|\\|");
for (int x=0; x<result.length; x++){
System.out.print(result[x]);
}
There you go its simple

Categories

Resources