如何解析包含整数和数字的字符串

时间:2013-11-24 05:56:15

标签: java string parsing

让我们说输入是C3F5G10

我知道每个其他偶数都是一个字符的事实,每个奇数都是一个整数。但是,这只适用于整数是一位数的情况。在这种情况下,它不是,因为10被读作2个独立的元素。

我如何解决这种情况?基本上输出只会打印整数

旁边的字符数

CCCFFFFFGGGGGGGGGG

 public static String translate(String formatStr) {
        char [] array = formatStr.toCharArray();
    char [] carray = new char[array.length/2];
    char [] narray = new char[array.length/2];
    StringBuilder build1 = new StringBuilder();



    int m = 0;
    for(int i  = 0; i<carray.length; i++){
        carray[i] = array[m];
        m+=2;
    }
    int l =1;
    for(int i = 0; i<narray.length; i++){
        narray[i] = array[l];
        l+=2;
    }



    for(int i = 0; i < carray.length; i++){
        String number = Character.toString(narray[i]);
        for(int j=0;j<Integer.parseInt(number);j++){
            build1.append(carray[i]);   
        }

        }

    return build1.toString();
}

5 个答案:

答案 0 :(得分:0)

我坚信,人们可以通过自己的方式来学习,我会为你提供一些见解,你会自己编码。
保留一个包含临时号码的变量,比如num。从左到右读取您的字符串到名为currentChar的变量,char by char。如果当前字母是字符,那么您知道需要打印的 ;将其保存在变量charToPrint中。如果是数字,则进行以下更改:num = num * 10 + <convert-your-character-to-integer> currentChar。下次遇到角色时,您可以打印上一个找到的角色,charToPrintnum次。之后将0分配给num。
希望能帮到你。

答案 1 :(得分:0)

尝试,

  public static String translate(String word) {
    StringBuilder res = new StringBuilder();
    char let = ' ';
    String num = "";
    for (int i = 0; i < word.length(); i++) {
        char ch = word.charAt(i);
        if (Character.isDigit(ch)) {
            num += ch;
        } else if (Character.isLetter(ch)) {
            if (num.length() > 0) {
                for (int j = 0; j < Integer.parseInt(num); j++) {
                    res.append(let);
                }
            }
            let = ch;
            num = "";
        }
    }
    if (num.length() > 0) {
        for (int j = 0; j < Integer.parseInt(num); j++) {
            res.append(let);
        }
    }
    return res.toString();
  }

答案 2 :(得分:0)

看看这个实现。我希望它有所帮助。

public class Test {

public static void main(String[] args) {

    String input = "C3F5G10";
    StringBuilder sBuilderChar = new StringBuilder();
    String digit = "";
    for (int i = 0; i < input.length(); i++) {
        if (isCharacter(input.charAt(i))) {
            int k = i;// store index of character.
            // loop until the next character is not encountered
            // form a string of all the digits sandwiched between two
            // characters
            while (k < input.length() - 1 && isDigit(input.charAt(k + 1))) {
                digit = digit + input.charAt(k + 1);
                k++;
            }
            // convert the digit string to number
            int n = Integer.parseInt(digit);
            // append the character the number of times calculated
            // previously
            for (int j = 0; j < n; j++) {
                sBuilderChar.append(input.charAt(i));
            }
            // skip all the digits and move to next character
            // i.e. current index + the length of digit obtained
            i = i + digit.length();
            // empty the digit string
            digit = "";
        }

    }
    // print the string formed to console
    System.out.println(sBuilderChar);

}

public static boolean isDigit(char c) {
    // digits are in the range [48,57] in ASCII code
    return c >= 48 && c <= 57;
}

public static boolean isCharacter(char c) {
    // if not digit then 'c' is a character
    return !isDigit(c);
}

}

答案 3 :(得分:0)

这是我的解决方案。似乎工作。来自int tmp=0的行很有意思。这就是我在角色后处理多个数字的方式。

import java.io.PrintWriter;
import java.util.Scanner;

public class QuestionTwo {
  public static void main(String[] args) {
    PrintWriter out = new PrintWriter(System.out);
    Scanner sc = new Scanner(System.in);

    char[] input = sc.nextLine().trim().toCharArray();

    StringBuilder ans = new StringBuilder("");
    int i = 0;
    while (true) {
      if (i > input.length - 1)
        break;
      // Has to be a character.
      char c = input[i];
      i++;
      int tmp = 0;
      while (i < input.length && Character.isDigit(input[i])) {
        tmp *= 10;
        tmp += input[i] - '0';
        i++;
      }
      while (tmp-- > 0)
        ans.append(c);
    }
    out.println(ans.toString());
    out.close();
    sc.close();
  }
}

答案 4 :(得分:0)

如果您想使用正则表达式,那么这是一种方式......

 public static String translate(String formatStr) {
    StringBuilder result = new StringBuilder();
    //make sure it really contains just alphanumeric
    if(formatStr.matches("^[a-zA-Z0-9]*$")) {
        String alphas[] = removeInvalid(formatStr.split("[0-9]"));
        String numerics[] = removeInvalid(formatStr.split("[a-zA-Z]"));
        int alphaPointer = 0;
        for(String num: numerics) {
            int iterator = Integer.valueOf(num);
            for(int i=0; i< iterator; i++) {
                result.append(alphas[alphaPointer]);
            }
            alphaPointer++;
        }
    }
    return result.toString();

}

/*
 * Remove empty results from the regex split 
 */
private static String[] removeInvalid(String[] values) {
    ArrayList<String> list = new ArrayList<String>();
    for(String value: values) {
        if(value.matches("\\d+") || value.matches("\\w+"))
            list.add(value);
    }
    String[] valids = new String[list.size()];
    return list.toArray(valids);
}