如何拆分String数组并将字符串存储在另一个数组中

时间:2014-04-17 13:42:50

标签: java arrays string

我有一个包含"-"的字符串数组。所以我必须拆分这些字符串来存储它们。

例如。

String s[]={"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};

String k[]={"abc","gt","q","asd","aby"}

我试过的代码是

 public static void main(String...a)
{


String s[]={"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};

int sop=0;

                 String[] sdf=new String[100];

 for(int kk=0;kk<s.length;kk++)
             {

              if(s[sop].equals("-"))
              {

              }
              else
              {
              sdf[sop]=s[sop];

               sop++;
              }

             }
}

但它给出前三个字符串。 abc。我必须添加什么?

8 个答案:

答案 0 :(得分:2)

你可以这样试试:

public static void main(String[] args) {
    String[] s = {"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};
    StringBuilder builder = new StringBuilder();
    ArrayList<String> k = new ArrayList<String>();
    for (String str : s) {
        if (str.equals("-")) {
            k.add(builder.toString());
            builder.setLength(0);
        } else builder.append(str);
    }
    k.add(builder.toString());
    System.out.println(Arrays.toString(k.toArray(new String[k.size()])));
}

输出:

[abc, gt, q, asd, aby]

答案 1 :(得分:1)

如果s[sop].equals("-"),你没有增加sop。你必须每次增加它。它也可以减少到。

          if(!s[sop].equals("-"))
          {
              sdf[sop]=s[sop];
          }
          sop++;

也可以使用循环计数器kk而不是sop;

另一种替代方式可以是,

  1. 遍历第一个数组并将所有元素联系到String。
  2. 使用String.split方法获取最终数组

    StringBuilder sB = new StringBuilder();
     for(String temp:s)
        sB.append(温度);
     String [] output = sB.toString()。split(“ - ”);

答案 2 :(得分:1)

你可以这样做:

    String s[]={"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};

    String split = "-";

    ArrayList<String> list = new ArrayList<String>();
    String temp = "";

    for(int i = 0 ; i < s.length ; i++){
        if(s[i].equals(split)){
            list.add(temp);
            temp = new String();
        }else{
            temp += s[i];
        }
    }

    String array[] =  list.toArray(new String[list.size()]);

    for(String str : array){
        System.out.println(str); // output : abc;gt;q;asd;
    }

答案 3 :(得分:0)

这里有几个问题:

  • 当您到达“ - ”时,您停止递增sop。我认为您可以使用s[sop]代替s[kk]
  • 每次获得新字符时,都不会将它们附加到现有字符串,而是将它们添加到新的数组单元格中

此外,对于您的第二个阵列,请考虑使用ArrayList,因为您最初并不知道它的大小。

以下是更正后的版本:

for (int kk = 0; kk < s.length; kk++) {

    if (!s[kk].equals("-")) {
        if (sdf[sop] != null) {
            sdf[sop] += s[kk];
        } else {
            sdf[sop] = s[kk];
        } 
    } else {
        sop++;
    }

}

答案 4 :(得分:0)

逻辑错误:你在两个数组中使用相同的迭代器,同时从s []中检索值,你应该使用for循环迭代器,kk。

public static void main(String...a)
     {
     String s[]={"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};

     String[] sdf=new String[100];
     int sop = 0;
      for(int kk=0;kk<s.length;kk++)
      {
          if(!s[kk].equals("-"))
          {
              sdf[sop]=s[kk];
              sop++;
          }
      }
     }

答案 5 :(得分:0)

你必须从字符串数组开始吗?

您可能希望使用StringTokenizer使用' - '解析字符串。

String s[] = { "a", "b", "c", "-", "g", "t", "-", "q", "-", "a", "s",
                "d", "-", "a", "b", "y" };

// Join the letters to make a full string
String s_full = "";
for (String letter : s) {
    s_full += letter;
}

StringTokenizer st = new StringTokenizer(s_full, "-");  //Tokenize
String[] words = new String[st.countTokens()];
int i = 0;
while (st.hasMoreTokens()) {
    words[i++] = st.nextToken();    //Add to result array
}

// Print result
for (String word : words) {
    System.out.println(word);
}

答案 6 :(得分:0)

留在你的背景中:

public static void main(String[] a) {


    String s[]={"a","b","c","-","g","t","-","q","-","a","s","d","-","a","b","y"};

    int sop=0;

    String[] sdf=new String[100];
    String temp = "";

    for(int kk=0;kk<s.length;kk++){
        if(s[kk].equals("-")){
            sdf[sop] = temp;
            temp = "";
            sop++;
        } else {
            temp += s[kk];
        }
    }
}

最好使用StringBuilder,这只是为了理解逻辑......

答案 7 :(得分:0)

我是这样的:

String s[] = { "a", "b", "c", "-", "g", "t", "-", "q", "-", "a", "s", "d", "-", "a", "b", "y" };
String delimiter = "-";

List<String> result = new ArrayList<String>();  
StringBuilder group = new StringBuilder();
for (String character : s) {
    if (delimiter.equals(character) && !"".equals(group)) {
        result.add(group.toString());
        group.setLength(0);
    } else {
        group.append(character);
    }
}

if (!"".equals(group)) {
    result.add(group.toString());
}

您可以使用List或数组字符串转换:

result.toArray(new String[0]);