gpt4 book ai didi

java - 如何获取单个字符串中包含的变量名称及其值?

转载 作者:行者123 更新时间:2023-12-02 12:07:24 26 4
gpt4 key购买 nike

如果我的信息包含在单个字符串中,如何获取字符串变量名称并为其分配值?

我有一个像这样的字符串:

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

我想要实现的结果是这样的:
id =“123487”
street1=“斯坦顿”
street2=“网关”
street3=“霍金斯”
城市=“地平线”

到目前为止我所拥有的是这段代码:

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”
“1号街:斯坦顿”
“street2:网关”
“第三街:霍金斯”
“城市:地平线”

也许我之前可以创建 5 个变量,并且以某种方式只获取值,因此在 for 循环中我可以将每个值分配给每个变量?
但我不知道如何,你能帮助我实现我想要做的事情吗?
谢谢!

最佳答案

我创建了一个 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()));
}

关于java - 如何获取单个字符串中包含的变量名称及其值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46796232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com