简体   繁体   English

在Java中将字符串拆分为子字符串

[英]Splitting string into substring in Java

I have one string and I want to split it into substring in Java, originally the string is like this 我有一个字符串,我想用Java将其拆分为子字符串,最初的字符串是这样的

Node( <http://www.mooney.net/geo#wisconsin> )

Now I want to split it into substring by (#), and this is my code for doing it 现在我想用(#)将其拆分为子字符串,这是我的代码

String[] split = row.split("#");

String word = split[1].trim().substring(0, (split[1].length() -1));

Now this code is working but it gives me 现在这段代码可以正常工作了,但它给了我

    "wisconsin>"

the last work what I want is just the work "wisconsin" without ">" this sign, if someone have an idea please help me, thanks in advance. 我想要的最后一个作品只是“威斯康星州”的作品,没有“>”这个标志,如果有人有想法请帮助我,在此先感谢。

Java1.7 DOC for String class 字符串类的Java1.7 DOC

Actually it gives you output as "wisconsin> " (include space) 实际上,它的输出为"wisconsin> " (包括空格)

Make subString() as 使subString()

String word = split[1].trim().substring(0, (split[1].length()-3));

Then you will get output as 然后您将获得输出为

wisconsin

Tutorials Point String subString() method reference 教程Point String subString()方法参考

String string = "Enter parts here"; String string =“在此输入零件”;

String[] parts = string.split("-"); String []部分= string.split(“-”);

String part1 = parts[0]; 字符串part1 = parts [0];

String part2 = parts[1]; 字符串part2 = parts [1];

您可以像以前一样再次拆分(用>代替#),并使用元素[0]而不是[1]

Consider 考虑

String split[] = row.split("#|<|>");

which delivers a String array like this, 它提供了这样的String数组,

{"http://www.mooney.net/geo", "wisconsin"}

Get the last element, at index split.length()-1 . 获取最后一个元素,索引为split.length()-1

You can just use replace like. 您可以使用replace like。

word.replace(char oldChar, char newChar) word.replace(char oldChar,char newChar)

Hope that helps 希望能有所帮助

You can use Java String Class's subString() method. 您可以使用Java String Class的subString()方法。

Refer to this link . 请参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM