开发者

regular expression to remove a space

I need a way to remove only the first space found in a string and then put the string in an array. For example

hello there. Hey.
开发者_如何学运维

I want that to be split like [hello][there. Hey]. I tried with

String [] s = str.split(" ")

by that will naturally remove all the spaces and create several strings. i just need 2. Can you please tell me how to do that ? Ether by regular expression or another way.


String [] s = str.split (" ", 2); should do the trick, documentation here.

You may also want to consider using \s+ as the regex - it may split the string more intelligently.


Using a regular expression for this isn't necessarily your best option.

Find the first space using position() (whatever the java method is), and then use substring() from the beginning of the string to that position, and again from that position to the end of the string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜