2017-10-17 55 views
0

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

我有一个这样的字符串:

"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(); 
} 

所以我能够证明使用吐司是用逗号分隔的字符串的用户,我有这样的:
“ID:123487”
“street1:斯坦顿”
“STREET2:网关”
‘street3:霍金斯’
‘城市:天际’

也许我以前创建的5个变量,并以某种方式只得到了价值,所以在为周期,我可以分配给每个值的每个变量?
但我不确定你是怎么做到的,你能帮我实现想做的事情吗?
谢谢!

+0

这并不是特别清楚你在问什么,但是你可以再次将“str:Fnord”字符串拆分为两个字符串的数组。 – jdv

+0

好吧,我明白了,我正在尝试创建字符串变量并为它们赋值,例如,如果我有这个字符串: “city:Horizo​​n,street1:Stanton” 我想要一个名为city的字符串变量等于“地平线”,并与街道1等一样。 所以,如果我明白你的建议是再次分裂。但这次按“:”分隔? –

回答

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语句并将一个getter/setter添加到Location对象。

用法:

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())); 
} 
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

+0

https://docs.oracle.com/javase/tutorial/essential/regex/pattern.html – Mookayama

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: ", ""); 
    } 

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

相关问题