Java中将字符串拆分为字符串数组

时间:2014-12-09 16:07:30

标签: java arrays string split

    import java.io.*;

    public class Test{
       public static void main(String args[]){
          String Str = new String("Welcome to Tutorialspoint.com and again some other random stuff in this string.");
          String[] result= new String[8];
          byte c=0b0;
          int i=0;
          int j=0;

          for (int a=0;a<7;a++){
              result[a]="";
          }
          for(i=0; j<Str.length(); j++){
              c=(byte)(Str.charAt(j));  
              result [i]+=(char)c;
              if (i<7){i++;}else{i=0;}
          }
          for (int a=0;a<8;a++){
              System.out.println(result[a]);
          }
       }
    }

目标是从原始字符串创建8个字符串。 String [0]将保存字符0,8,16,...等等。 String [1]将保存字符1,9,17,......等等。 我希望这很清楚。

我用这段代码得到的东西似乎无法克服。

    Wtitdsemis
    eoa. or nt
    l lcam s r
    cTsogertti
    oupma auhn
    mto ionfig
    eoiantdfs.
    null rnn ho  

在最后一行注意null - 我需要这个,因为字符串应该以'rnn ho'开头,就像这样。

    Wtitdsemis
    eoa. or nt
    l lcam s r
    cTsogertti
    oupma auhn
    mto ionfig
    eoiantdfs.
     rnn ho 

如果有人指出如何不获得此输出,我们将非常感激。这是一个“测试”代码,用于分割将保存-126到127二进制值的String。并非所有这些都是可打印的,我需要它们才能正确分割。 在大多数情况下,代码似乎有效,除了输出中看似随机的'null'字符串。

我不介意[null] = 0个字符,只要它们占用1个字符空间而不是4个字符串。

=============================================== =================================== 初始化&lt; 8修复了这个问题。但我甚至没有时间阅读所有其他评论/答案。没想到这么快的答案。谢谢你们 。当我阅读所有相关解决方案和/或获得声誉时,我将投票给任何相关的解决方案。

=============================================== =================================== FIXED!

=============================================== =================================== Ian McLaird的精选答案不仅解决了我的“愚蠢”错误,而且还向我展示了我不了解的更简洁的代码和功能。 无论如何,谢谢大家的意见和答案。

3 个答案:

答案 0 :(得分:1)

这个怎么样?

public class StringSplitter {
    public static void main(String[] args) {
        String str = new String("Welcome to Tutorialspoint.com and again some other random stuff in this string.");
        String[] result = new String[8];

        for (int i = 0; i < result.length; ++i) {
            result[i] = "";
        }

        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            result[i % result.length] += chars[i];
        }

        for (int i = 0; i < result.length; ++i) {
            System.out.println(result[i]);
        }
    }
}

由于您的result数组已经实例化,因此使用length属性初始化元素以清空字符串是安全的。由于您打算处理字符串中的每个字符,请继续并将其作为数组获取,然后使用模数运算符将每个字符放入result数组中的适当位置。作为额外的好处,它也可以安全地防止更改result数组的长度。硬编码的循环哨兵是危险的。

输出

Wtitdsemis
eoa. or nt
l lcam s r
cTsogertti
oupma auhn
mto ionfig
eoiantdfs.
 rnn ho  

答案 1 :(得分:0)

从快速浏览代码开始,问题在于,在第一个for循环中,您将结果[0]实例化为结果[6]而不是实例化结果[7]。放置for (int a = 0; a <7; a++)时,循环将运行直到a = 6 - 当它增加到7时,&lt; 7返回false,因此在第一个for循环之后,result [7] == null

答案 2 :(得分:0)

您可以尝试以下操作:

import java.io.*;

public class Test{
  public static void main(String args[]){
    String Str = new String("Welcome to Tutorialspoint.com and again some other random stuff in this string.");

    String[] result = new String[8];

    for (int a = 0; a < result.length; a++)
       result[a] = "";

    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length; ++i) 
      result[i % result.length] += chars[i];

    for (int a = 0; a < result.length; a++)
      System.out.println(result[a]);
  }
}

产生的结果将是:

Wtitdsemis
eoa. or nt
l lcam s r
cTsogertti
oupma auhn
mto ionfig
eoiantdfs.
 rnn ho