1. 程式人生 > >Java 判斷漢字以及大小寫字母

Java 判斷漢字以及大小寫字母

程式碼如下:

public class MainActivity extends AppCompatActivity {
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.edit_text);

        findViewById(R.id.click).setOnClickListener(new
View.OnClickListener() { @Override public void onClick(View view) { if (editText.getText().length() != 0) { getTabWidth(editText.getText()); } } }); } protected void getTabWidth(CharSequence text) { int
chineseNum = 0; int bigCharNum = 0; int smallNum = 0; Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); int len = 0; for (int i = 0; i < text.length(); i++) { char cr = text.charAt(i); Matcher m = p.matcher(String.valueOf(cr)); if
(m.matches()) { chineseNum ++; } else if (!Character.isLowerCase(cr)) { bigCharNum ++; } else { smallNum ++; } } Log.e("owen", "chineseNum: " + chineseNum + " bigCharNum: " + bigCharNum + " smallNum" + smallNum); } }