开发者

How can I split a string in Java?

Imagine I have this st开发者_如何学编程ring:

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("\\|");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜