-1

我想从给定的字符串输入中分离字符、数字和特殊字符。

目标是首先对字符值进行分组并添加数字,然后打印下一个带有最后一个特殊字符的字符。

Example:
Input:sep2tr8sg0*@sdb($67)hs
Output:seotrsqsdbhs13*@($)
4

2 回答 2

4

我可能不应该回答,但这是我的停顿;-)

public static void main(String args[]) {
    String s = "sep2tr8sg0*@sdb($67)hs";

    StringBuilder letters = new StringBuilder();
    StringBuilder numbers = new StringBuilder();
    StringBuilder other = new StringBuilder();

    for (char c : s.toCharArray()) {
        if (Character.isLetter(c)) letters.append(c);
        else if (Character.isDigit(c)) numbers.append(c);
        else other.append(c);
    }

    String output = letters.append(numbers).append(other).toString();

    System.out.println(output);
}
于 2012-10-19T15:30:39.387 回答
-1

分隔字符编号和特殊符号

 StringBuffer alpha = new StringBuffer(),
 num = new StringBuffer(), special = new StringBuffer();
 final String txt= txtview2.getText().toString();
 alpha1.setText(txtview2.getText().toString());
 num1.setText(txtview2.getText().toString());
 special1.setText(txtview2.getText().toString());
 Toast.makeText(MainActivity.this,"you enter string:"+txt,Toast.LENGTH_SHORT).show();
 for (int i=0; i<txt.length(); i++)
 {
     if (Character.isDigit(txt.charAt(i)))
         num.append(txt.charAt(i));
     else if(Character.isLetter(txt.charAt(i)))
         alpha.append(txt.charAt(i));
     else
         special.append(txt.charAt(i);
 }
 alpha1.setText("The Character: " +alpha);
 num1.setText("The digit:" + num);
 special1.setText("The special symbol: " + special);
}
于 2020-03-23T11:05:26.497 回答