如何将字符串转换为数组(int和字符串数组)。

时间:2017-01-07 07:25:52

标签: java arrays string integer

如何从String获取String和int值,如下所示:"a:10,b:15,c:20,d:30"

String mixedString = "a:10,b:15,c:20,d:30";
String requiredArray1[] = [a,b,c,d];
int requiredArray2[] = [10,15,20,20];

3 个答案:

答案 0 :(得分:1)

你可以循环你的String并逐个测试你的字符串:

<强>第一

您需要将String拆分为:

String myString = "a:10,b:15,c:20,d:30";
//split the String to get only the Strings and int in your case you need to split with , and :

String mixedString[] = myString.split(":|\\,");

<强>第二

测试如果String是Integer,则返回true并将其插入到Integers数组中,否则将其插入到字符串数组中:

public static boolean test(String s){
    try{
        Integer i = Integer.parseInt(s);
        return true;
    }catch(Exception e){
        return false;
    }
}

以下是您的计划的样子:

public static void main(String[] args) {
    String myString = "a:10,b:15,c:20,d:30";
    String mixedString[] = myString.split(":|\\,");
    String requiredArray1[] = new String[mixedString.length];
    int requiredArray2[] = new int[mixedString.length];
    int s = 0;
    int n = 0;
    for (int i = 0; i < mixedString.length; i++) {
        if (!test(mixedString[i])) {
            requiredArray1[s] = mixedString[i];
            s++;
        } else {
            requiredArray2[n] = Integer.parseInt(mixedString[i]);
            n++;
        }
    }
}

public static boolean test(String s) {
    try {
        Integer i = Integer.parseInt(s);
        return true;
    } catch (Exception e) {
        return false;
    }
}

答案 1 :(得分:1)

如果您在帖子中显示混合字符串,其中每个字母字符始终后跟冒号分隔符(:)然后是数字值的字符串表示,那么你真的不喜欢&#39 ; t需要一个额外的方法来测试数值是否存在。你只是知道那里就像你知道那里有一个alpha值......或者......也许你不会,也许你也应该测试alpha。您没有在帖子中指定混合字符串中可能存在的不同可能性。因此,我们可以假设:

每个alpha部分都用冒号(:)分隔,然后是数字值的字符串表示,到目前为止,它确实显示为整数。然后是逗号(,)分隔符和另一个以冒号分隔的字母/数字对。

String mixedString = "a:10,b:15,c:20,d:30";
System.out.println("Original String: \"" + mixedString + "\"\n");

String[] mStringArray= mixedString.split(",");
String[] alphaArray = new String[mStringArray.length];
int[] numericArray = new int[mStringArray.length];

for (int i = 0; i < mStringArray.length; i++) {
    String[] tmp = mStringArray[i].split(":");
    alphaArray[i] = tmp[0];
    numericArray[i] = Integer.parseInt(tmp[1]);
}

// Display contents of the two Arrays
System.out.println("Elements From Alpha Array");
for (int i = 0; i < alphaArray.length; i++) {
    System.out.println(alphaArray[i]);
}

System.out.println("\nElements From Numeric Array");
for (int i = 0; i < numericArray.length; i++) {
    System.out.println(numericArray[i]);
}

答案 2 :(得分:1)

   public static void main(String[] args) {
    String myString = "a:10,b:15,c:20,d:30";
    // extract all numbers (All elements are numbers so you can convert it to int easily )
    String[] requiredArray1 = extractAllAccordingToRegex("\\d+", myString);
    // extract all characters        
    String[] requiredArray2 = extractAllAccordingToRegex("[a-zA-Z]+",myString);
    }

   static String[] extractAllAccordingToRegex(String inputRegex, String input) {
    List<String> extractedItems = new ArrayList<String>();
    Pattern reg = Pattern.compile(inputRegex);
    Matcher m = reg.matcher(input);
    while (m.find()) {
        extractedItems.add(m.group());
    }
    return extractedItems.toArray(new String[1]);
    }