简体   繁体   English

从列表中提取字符串时会在Java中拆分字符串吗?

[英]Splitting a string up in java as it is pulled from a list?

I'm looking to split a List of Strings up as i iterate throught the list imagine the following as the List. 我正在寻找一个字符串列表,当我遍历整个列表时,将以下内容想象为列表。

["StringA.StringB.StringC"]["StringA.StringB.StringC"]["StringA.StringB.StringC"]["StringA.StringB.StringC"]

So i want to iterate through the list pull out each string ( "StringA.StringB.StringC" ) 所以我想遍历列表,拔出每个字符串( "StringA.StringB.StringC"

Then want to seperate by the "." 然后要以"."分隔"." and assign StringA to a String variable - String B to one and String C. Perform some work and move on to the next. 并将StringA分配给一个String变量-将String B分配给一个变量,并将C分配给字符串C。执行一些工作,然后继续进行下一个。

Any ideas how i can do this? 任何想法我该怎么做?

This is a fairly simple task. 这是一个相当简单的任务。 Just use a foreach to iterate through the list, and then split the string on the . 只需使用foreach遍历列表,然后在上拆分字符串即可. character. 字符。

for(String s: list) {
    String[] splitString = s.Split("\\.");
    // do work on the 3 items in splitString
}

The variable splitString will have a length of 3. 变量splitString的长度为3。

Of course! 当然! You just want String#split() . 您只需要String#split()

for (String s : myList)
{
    String[] tokens = s.split("\\.");
    // now, do something with tokens
}

You need to escape the period in the argument to split() because the string is interpreted as a regex pattern , and an unescaped period ( "." ) will match any character - not what you want! 您需要在split()参数中转义句点,因为该字符串被解释为正则表达式模式 ,并且未转义的句点( "." )将匹配任何字符- 不是您想要的字符!

List<String> strings=new ArrayList<String>();

for (String s:stringList){
   strings.addAll(Arrays.asList(s.split("\\.")));
}

then you'll have all the strings in one list and you can iterate over them. 那么您将所有字符串都放在一个列表中,然后可以对其进行迭代。

So you have a list of String[] which are concatenated strings. 因此,您有一个String []列表,它们是串联的字符串。 Unless this is some sort of legacy data structure, you might do better to make this more uniform in some way. 除非这是某种传统数据结构,否则您可能会更好地以某种方式使其更加统一。

In any case, iteration is pretty straightforward - 无论如何,迭代非常简单-

List<String[]> l = ....

for (String[] outer: l) { 
       for (String s: outer){
            for (String inner: s.split("\\.")) // s.split takes a regex and returns a String[],
                 // inner has your inner values now

To iterate through a list, see http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html 要遍历列表,请参见http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html

To split a string, use String.split(regex) . 要拆分字符串,请使用String.split(regex)

The String class has the split(String regex) method, which splits the string by regular expression. String类具有split(String regex)方法,该方法通过正则表达式拆分字符串。 If you don't want the hassle of dealing with regular expressions, you can use the very handy StringUtils class from Apache Commons Lang. 如果不想麻烦处理正则表达式,可以使用Apache Commons Lang中非常方便的StringUtils类。

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

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