如何将变量名称及其值包含在单个字符串中?

时间:2017-10-17 17:51:36

标签: java android string split

如果我将信息包含在一个字符串中,如何获取字符串变量名并为其赋值?

我有一个这样的字符串:

"id: 123487, street1: Stanton, street2: Gateway, street3: Hawkins, city: Horizon"

我试图实现的结果是:
id =" 123487"
street1 =" Stanton"
street2 =" Gateway"
street3 =" Hawkins"
city =" horizo​​n"

到目前为止我的代码是:

String scannedData = "id: 123487, street1: Stanton, street2: Gateway, street3: Hawkins, city: Horizon"


String[] data = scannedData.split(",");
for (String result1 : data) {
    Toast.makeText(scanQrActivity.this,
            result1, Toast.LENGTH_LONG).show();
}

所以我能够使用Toast向用户显示的是用逗号分隔的字符串,我有这个:
" id:123487"
" street1:Stanton"
" street2:Gateway"
" street3:Hawkins"
"城市:地平线"

也许我之前可以创建5个变量,并且只能获取值,所以在for循环中我可以为每个变量分配每个值?
但是我不确定你能帮我实现我想做的事吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

我创建了一个Location类,它将保存我们要解析的数据。

public class Location {

    private int id;

    private String street1, street2, street3, city;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStreet1() {
        return street1;
    }

    public void setStreet1(String street1) {
        this.street1 = street1;
    }

    public String getStreet2() {
        return street2;
    }

    public void setStreet2(String street2) {
        this.street2 = street2;
    }

    public String getStreet3() {
        return street3;
    }

    public void setStreet3(String street3) {
        this.street3 = street3;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

现在我们可以使用以下方法将输入字符串解析为Location对象。

public static Location parseLocation(String input) {
    // Get the input and split it into an array of "key: value" strings.
    String[] splitInput = input.split(", ");

    // Create an array to hold the actual key-values.
    String[][] keyValues = new String[splitInput.length][2];

    // Split each "key: value" string into its consisting parts.
    for (int i = 0; i < splitInput.length; i++) {
        keyValues[i] = splitInput[i].split(": ");
    }

    // Create a Location object to populate.
    Location location = new Location();

    // Find the keys & values from the input that you're looking for.
    for (String[] keyValue : keyValues) {

        // Switch on the key.
        switch (keyValue[0]) {
            case "id":
                location.setId(Integer.valueOf(keyValue[1]));
                break;

            case "street1":
                location.setStreet1(keyValue[1]);
                break;

            case "street2":
                location.setStreet2(keyValue[1]);
                break;

            case "street3":
                location.setStreet3(keyValue[1]);
                break;

            case "city":
                location.setCity(keyValue[1]);
                break;
        }
    }
    return location;
}

此解决方案允许将更多键值添加到输入字符串,而不会导致程序崩溃。您需要做的就是为新键添加一个case语句,并为Location对象添加一个getter / setter。

用法:

public static void main(String[] args) {
    String   input    = "id: 123487, street1: Stanton, street2: Gateway, street3: Hawkins, city: Horizon";
    Location location = parseLocation(input);

    System.out.println(String.format("id: %d\n"
            + "street1: %s\n"
            + "street2: %s\n"
            + "street3: %s\n"
            + "city: %s", location.getId(), 
            location.getStreet1(), location.getStreet2(), 
            location.getStreet3(), location.getCity()));
}

答案 1 :(得分:0)

//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111

import java.util.*;
import java.lang.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Rextester
{  

        private static final String REGEX = ",";//"\\d";
                private static final String REGEX2 = ":";//"\\d";

    private static final String INPUT ="id: 123487, street1: Stanton, street2: Gateway, street3: Hawkins, city: Horizon";

        //"one9two4three7four1five";

    //public class SplitDemo2 {



    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(INPUT);
        for(String s : items) {
            System.out.println(s);
            Pattern p2 = Pattern.compile(REGEX2);
            String[] items2 = p2.split(s);
            for(String s2: items2){
                System.out.println(s2);
            }
        }
    }
//}

}//end Rextester

http://rextester.com/NZKDG47991

答案 2 :(得分:0)

感谢你的帮助,你给了我很棒的建议,最后,我用更简单的方式做了,它可能不是最好的编码,但我还在学习,我认为我的目标是编码和你们一样,所以你的答案真的帮助我意识到我有很多东西需要学习。

    String scannedData = "id: 123487, street1: Stanton, street2: Gateway, street3: Hawkins, city: Horizon"

    String[] data = scannedData .split(",");
    String id = null;
    String street1 = null;
    String street2 = null;
    String street3 = null;
    String city = null;
    for (String result : data) {

        id = data[0].replace("id: ", "");
        street1 = data[1].replace("street1: ", "");
        street2 = data[2].replace("street2: ", "");
        street3 = data[3].replace("street3: ", "");
        city = data[4].replace("city: ", "");
    }

然后我使用Toast显示每个变量。

相关问题