Java 中常用的字符串操作
1,判断字符是否是普通字符(a-z,A-Z,0-9,下划线_,英文句点.)
必须是英文大小写,数字,下划线,英文句号
- /***
- * 是否是普通字符(a-z,A-Z,0-9,下划线_,英文句点.)
- * @param c
- * @return
- */
- public static boolean isWordChar(char c){
- int number22=(int)c;
- if((number22>=97/*a*/ && number22<=122/*z*/)
- ||(number22>=65/*A*/ && number22<=90/*Z*/)
- ||(number22>=48/*0*/ && number22<=57/*9*/)
- ||number22==95/*下划线*/
- ||number22==46/*英文的句点.*/){
- // System.out.println("is word");
- return true;
- }else{
- return false;
- }
- }
测试:
- @Test
- public void tst2(){
- char c='_';
- System.out.println(isWordChar(c));
- Assert.assertEquals(true, isWordChar(c));
- c=',';
- Assert.assertEquals(false, isWordChar(c));
- c='a';
- Assert.assertEquals(true, isWordChar(c));
- c='z';
- Assert.assertEquals(true, isWordChar(c));
- c='A';
- Assert.assertEquals(true, isWordChar(c));
- c='Z';
- Assert.assertEquals(true, isWordChar(c));
- c='0';
- Assert.assertEquals(true, isWordChar(c));
- c='9';
- Assert.assertEquals(true, isWordChar(c));
- c=' ';
- Assert.assertEquals(false, isWordChar(c));
- c='.';
- Assert.assertEquals(true, isWordChar(c));
- }
2,判断字符串是否是普通字符
必须以字母开头,可以包含(a-z,A-Z,0-9,下划线_,英文句点.),但是不能以句点结尾,不能连续两个句点
- /***
- * 必须以字母开头,可以包含(a-z,A-Z,0-9,下划线_,英文句点.),但是不能以句点结尾,不能连续两个句点
- * @param input
- * @return
- */
- public static boolean isWord(String input){
- char c=input.charAt(0);
- if(!isWordChar(c)){
- System.out.println("不是单词");
- return false;
- }
- int number22=(int)c;
- if((number22>=48/*0*/ && number22<=57/*9*/)
- ||number22==95/*下划线*/||number22==46/*英文的句点.*/){
- return false;
- }
- int length22=input.length();
- int lastChar =(int)input.charAt(length22-1);//最后一个char
- if(lastChar==46/*英文的句点.*/){
- return false;
- }
- for(int j=1;j<length22;j++){
- if(!isWordChar(input.charAt(j))){
- return false;
- }
- }
- //不能有连续两个英文句点
- if(SystemHWUtil.findStr3(input, SystemHWUtil.ENGLISH_PERIOD+SystemHWUtil.ENGLISH_PERIOD)>0){
- System.out.println("不能有连续两个英文句点");
- return false;
- }
- return true;
- }
测试:
- @Test
- public void tetst2(){
- String input="abc,";
- System.out.println(isWord(input));
- Assert.assertEquals(false, isWord(input));
- input="abc.a";
- Assert.assertEquals(true, isWord(input));
- input="1abc,";
- Assert.assertEquals(false, isWord(input));
- input="a111bc,";
- Assert.assertEquals(false, isWord(input));
- input="ab.a.c";
- Assert.assertEquals(true, isWord(input));
- input="ab..c";
- Assert.assertEquals(false, isWord(input));
- }
3,判断是否是空字符
空字符包括:(空格,Tab,换行,回车)
- /***
- * 是否是空格(空格,Tab)
- * @param c
- * @return
- */
- public static boolean isBlank(char c) {
- int number22 = (int) c;
- // System.out.println(number22);
- if(number22==32/*空格*/||number22==9/*Tab*/
- ||number22==10/*\n*/||number22==13/*\r*/){
- return true;
- }
- return false;
- }
测试:
- @Test
- public void test444(){
- char c='\n';
- System.out.println((int)c);
- System.out.println(OAUtil.isBlank(c));
- Assert.assertEquals(true, OAUtil.isBlank(c));
- c = '.';
- Assert.assertEquals(false, OAUtil.isBlank(c));
- c = ' ';
- Assert.assertEquals(true, OAUtil.isBlank(c));
- c = ' '/*Tab*/;
- Assert.assertEquals(true, OAUtil.isBlank(c));
- }
4,搜索字符串,若找到,则返回当时的索引,否则返回-1
- /***
- * 当发现keyWord的时候读到的index,<br>如果没有发现,则始终返回-1
- * @param srcText
- * @param keyWord
- * @param pos
- * @return
- */
- public static int findReadLength(String srcText, String keyWord, int pos) {
- int i, j/*, k = 0*/;
- i = pos;
- j = 0;
- while (i < srcText.length() && j < keyWord.length()) {
- if (srcText.charAt(i) == keyWord.charAt(j)) {
- ++i;
- ++j;
- if (j == keyWord.length()) {
- return i;
- }
- } else {
- i = i - j + 1;
- j = 0;
- }
- }
- return SystemHWUtil.NEGATIVE_ONE;
- }
测试:
- @Test
- public void test_findReadLength(){
- String srcText="abc";
- System.out.println(SystemHWUtil.findReadLength(srcText, "abc", 0));
- Assert.assertEquals(3, SystemHWUtil.findReadLength(srcText, "abc", 0));
- srcText=" abc";
- Assert.assertEquals(4, SystemHWUtil.findReadLength(srcText, "abc", 0));
- srcText=" abcabc";
- Assert.assertEquals(4, SystemHWUtil.findReadLength(srcText, "abc", 0));
- srcText=" ab c";
- Assert.assertEquals(-1, SystemHWUtil.findReadLength(srcText, "abc", 0));
- srcText="aababbabc";
- Assert.assertEquals(9, SystemHWUtil.findReadLength(srcText, "abc", 0));
- }
时间: 2024-09-27 01:22:32