将两个String分成子字符串,然后将它们配对 [英] Divide two String into substrings and pair them

查看:67
本文介绍了将两个String分成子字符串,然后将它们配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关此问题的有趣解决方案:

I am looking for interesting solutions for this problem :

String key = "1;2;3;4";
String value = "Value1;Value2;Value whitespace;"

现在';'彼此区分每个值.相同的符号;"还提供了键.

Now ';' devides each value from another. The same symbol ';' devides the keys also.

现在我想结束:

{"1" : "Value1", "2" : "Value2", "3" : "Value whitespace", "4" : null}

当然,如果值多于键,则null不应位于该对的左侧(null:"Value5").

Of course if the values were more then the keys then the null should be no the left side of the pair (null: "Value5").

我使用char数组为这个问题提供了一个非常完善的解决方案,但是它是包含许多情况和事情的FOR(这是O(n)).所以我很好奇看到正则表达式或子字符串解决方案或不包含大循环的东西.

I made a pretty complecated solution to this problem using char arrays but is one big FOR with many cases and stuff.(it is O(n)). So I am curious to see a regex or substring solution or something that not includes big loop.

排雷解决方案:

private List<ExampleObject> getExampleObjects(String key , String value) {
    // s
    if (key  == null || value == null) {
        return new ArrayList<ExampleObject>();
    }

    List<ExampleObject> exampleObjects = new ArrayList<ExampleObject>();

    char[] keyToCharArray = key.toCharArray();
    char[] valueToCharArray = value.toCharArray();

    StringBuilder name = new StringBuilder();
    StringBuilder value = new StringBuilder();

    boolean nameCompleted = false;
    boolean valueCompleted = false;

    for (int i = 0, j = 0; i < keyToCharArray.length || j < valueToCharArray.length;) {
        if (!nameCompleted) {
            char a = ' ';
            try{
                 a = keyToCharArray[i];
            } catch(Exception e){
                 a = ';';
                // throw : VALES and key  not match. More key  then value
                //throw(e);
            }

            if (a == ';' ) {
                nameCompleted = true;
            } else if (!(i + 1 < keyToCharArray.length)){
                name.append(a);
                nameCompleted = true;
            }   else {
                name.append(a);

            }
            i++;
        }
        if (!valueCompleted) {

            char a = ' ';
            try{
                 a = valueToCharArray[j];
            } catch(Exception e){
                 a = ';';
                // throw : VALES and key  not match. More value then key 
                //throw(e);
            }

            if (a == ';') {
                valueCompleted = true;
            } else if(!(j + 1 < valueToCharArray.length)) {
                value.append(a);
                valueCompleted = true;
            } else {
                value.append(a);
            }
            j++;
        }
        if (nameCompleted && valueCompleted) {
            exampleObjects.add(new ExampleObject(name.toString(), value.toString()));
            name.setLength(0);
            value.setLength(0);
            nameCompleted = false;
            valueCompleted = false;
        }
    }
    return exampleObjects;
}

其中 ExampleObject.class 具有字段 key value .

推荐答案

我为您的问题提出了解决方案:

I've come up with a solution to your problem:

输出

{"1" : "Value1", "2" : "Value2", "3" : "Value whitespace", "4" : "null"}       

代码

public class HelloWorld{

     public static void main(String []args){
        String key = "1;2;3;4";
        String value = "Value1;Value2;Value whitespace;";

        String[] keyArr = key.split(";");
        String[] valueArr = value.split(";");

        String finalJSON = "{";
        for(int i=0; i<(keyArr.length > valueArr.length ? keyArr.length : valueArr.length); i++) {

            try {
                finalJSON += "\"" + keyArr[i] + "\"";
            }
            catch(ArrayIndexOutOfBoundsException e) {
                finalJSON += "\"null\"";
            }

            finalJSON += " : ";

            try {
                finalJSON += "\"" + valueArr[i] + "\"";
            }
            catch(ArrayIndexOutOfBoundsException e) {
                finalJSON += "\"null\"";
            }
            if(i!=(keyArr.length > valueArr.length ? keyArr.length : valueArr.length) - 1) 
                finalJSON += ", ";
        }
        finalJSON += "}";

        System.out.println(finalJSON);
     }
}

这篇关于将两个String分成子字符串,然后将它们配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆