简体   繁体   中英

Separating a char from a String?

I am trying to separate the char from the following examples of inputs:

C450.00 C30 P100

I would like to have the char such as "C" or "P" separated so I can work with them alone, as well as the "450.00", "30", and "100" separated as ints. What would be the easiest way to do this?

If the String are always that format:

char ch = yourString.charAt(0);
Double d = Double.valueOf(yourString.substring(1, yourString.length()));

NOTE: I used a Double because you have dots . in the String . You can convert from double to int easily if you won't have any decimals. But that depends on your needings.

You can split the String with whitespace as delimiter. Afterwards use substring on every part of your string. Now you have the C and the 450.0 as Stings. Finally cast the second part of your substring into an integer and you are done.

to split:

String[] parts = string.split(" ");

to substring:

String first = parts[0].substring(0, 1);
String second = parts[0].substring(1);

假设您知道要在给定的String中查找的内容(例如,您知道要查找C字符),则可以使用Regex模式: https : //docs.oracle.com/javase/7/docs/api /java/util/regex/Pattern.html

You can use this library org.​apache.​commons.​lang3.​StringUtils http://commons.apache.org/proper/commons-lang/download_lang.cgi

public static String[] split(String str, String separatorChars)

    String[] res = StringUtils.split("C450.00 C30 P100", "CP ");        
    for (String r : res) {
        System.out.println(r);
    }

I assume the separate 'numbers' are always in this format:

C130
P90
V2.0

that is, a single letter followed by a number (possibly with a floating point).

String input = "C450.00 C30 P100";

// First split the string by a space
String[] parts = input.split(" ");

for (String part : parts) {
    // Get the character from the string
    char ch = part.charAt(0);

    // Get the remains of the string and convert it to a double
    double number = Double.parseDouble(part.substring(1));

    // Then do something with 'ch' and 'number'
}

If the separate parts possibly have two or more letters in it, eg AC130 , then you need another approach:

String input = "AC130 AG36 P90";
String[] parts = input.split(" ");

for (String part : parts) {
    /* Do some regular expression magic. */
}

See http://www.vogella.com/tutorials/JavaRegularExpressions/article.html .

here some piece of code:

public static void main(String[] args) {

    String a = "C450.00 C30 P100";
    String letters = "";
    String numbers = "";

    for (int i = 0; i < a.length(); i++) {
        if ((a.charAt(i) + "").matches("\\d|\\.| ")) {
            numbers += a.charAt(i);
        } else {
            letters += a.charAt(i);
        }
    }
    String[] strArray = numbers.split(" ");
    int[] numberArray = new int[strArray.length];
    for (int i = 0; i < strArray.length; i++) {
        numberArray[i] = (int) Double.parseDouble(strArray[i]);
        System.out.println(numberArray[i]);
    }
    System.out.println(letters);
}

the result is numberArray , which contains all numbers as ints.
and letters , which is a String with all letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM