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
我会亲自尝试String.split方法。
是的,有类似的东西。
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
使用String.split() 。
你需要使用\\\\,someString.split(“\\\\ |”)来转义管道分隔符;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.