在java中将字符串拆分为多个子字符串

时间:2017-04-08 13:28:57

标签: java

我是编程新手 我有一个像

这样的字符串
String input="|abc{123{asd|xyz{456{qwe";

我必须把这个字符串分成3个部分 我期待的输出是

string1={"abc","xyz"}  
no={"123","456"}  
string2={"asd","qwe"}

有人可以帮帮我吗。

4 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

substring(int beginIndex, int endIndex) //Returns a new string that is a substring of this string.

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)以分割字符串。

试试这个:

String input = "|abc{123{asd|xyz{456{qwe";
//input length
int length= input.length();
int parts = length/3;

//three substrings
String s1 = input.substring(0, parts);
String s2 = input.substring(parts, 2*parts);
String s3 = input.substring(2*parts, length);
System.out.println("s1: "+s1+"\ns2: "+s2+"\ns3: "+s3);

Output is:
s1: |abc{123
s2: {asd|xyz
s3: {456{qwe

答案 1 :(得分:0)

如果您尝试使用代码将String值截断为其他三个值;我会看一下:Cut Java String at a number of character。本文介绍如何截断字符串。

答案 2 :(得分:0)

如果我们假设我们应该用{|分割字符串,你可以像我一样提出类似的东西(这不是一个最佳解决方案而且我还没有为所有角落情况测试了它,但在这里它是)

public static void main(String[] args) {

        String input="|abc{123{asd|xyz{456{qwe";

        List<String> lettersFirst = new ArrayList<String>();/delimiter/ | 
        List<String> lettersSec = new ArrayList<String>();// delimiter {
        List<String> numbers = new ArrayList<String>();//delimiter either { or |

        char[] inputChars = input.toCharArray();

        for(int i=1; i<inputChars.length;i++){

            if(Character.isLetter(inputChars[i])){
                if(inputChars[i-1]=='|')
                    lettersFirst.add(GetStringUntilDelim(inputChars, i));
                else if (inputChars[i-1]=='{')
                    lettersSec.add(GetStringUntilDelim(inputChars, i));
            }else if(Character.isDigit(inputChars[i])){
                if(inputChars[i-1]=='|' || inputChars[i-1]=='{')
                numbers.add(GetStringUntilDelim(inputChars, i));
            }
        }

        //now format the final strings
        String s1 = "{ ", s2 = "{ ", s3 = "{ ";

        for(int i=0; i<lettersFirst.size();i++){

            s1 = s1 + lettersFirst.get(i);
            s1 = s1 + ",";
        }
        s1 = s1.substring(0,s1.length()-1);//remove the last comma
        s1 = s1 +" }";

for(int i=0; i<lettersSec.size();i++){

            s2 = s2 + lettersSec.get(i);
            s2 = s2 + ",";
        }
        s2 = s2.substring(0,s2.length()-1);//remove the last comma
        s2 = s2 +" }";

for(int i=0; i<numbers.size();i++){

            s3 = s3 + numbers.get(i);
            s3 = s3 + ",";
        }
        s3 = s3.substring(0,s3.length()-1);//remove the last comma
        s3 = s3 +" }";

        System.out.println(s1);
        System.out.println(s3);
        System.out.println(s2);

    }
    public static String GetStringUntilDelim(char[] inputChars, int index){

        String s = "";
        int maxIndex = inputChars.length-1;
        char c;
        do{

        c = inputChars[index++];    
        s = s + c;
        }while( (  c != '|' &&  c != '{')  && index <= maxIndex );

        s = (index-1) == maxIndex ? s : s.substring(0,s.length()-1);
        return s;
    }

输出

{ abc,xyz }
{ 123,456 }
{ asd,qwe }

请注意,使用StringBuilder进行字符串连接而不是字符串也会更有效。

答案 3 :(得分:0)

   String in = "|sha{1234{asd|sha1{4564{zxc";
        String[] string1, number, string2;
        String[] res = in.split("\\|");
        string1 = new String[res.length];
        number = new String[res.length];
        string2 = new String[res.length];
        for (int i1 = 1; i1 < res.length; i1++) {
            String[] temp = res[i1].split("\\{");
            string1[i1] = temp[0];
            number[i1] = temp[1];
            string2[i1] = temp[2];
        }
        for (int i = 1; i < string1.length; i++) {
            System.out.println(string1[i]);
            System.out.println(number[i]);
            System.out.println(string2[i]);
        }  

OUTPUT

1234
ASD
SHA1
4564
zxc