java字符串函数

1、replace()字符串替换

 String Str = new String("hello");

 System.out.print("返回值 :" );
 System.out.println(Str.replace('o', 'T'));

 System.out.print("返回值 :" );
 System.out.println(Str.replace('l', 'D'));
返回值 :hellT
返回值 :heDDo

字符串替换?

String sql = "delete from  user_log where  id in (?)";

使用:sql.replace("[?]", "")

2.contain()函数判断是否包含,返回值true与false

	if(str.contains(",")){
    
             }


 String a = "l love feng ye";
 String b = "love";
 boolean c = a.contains(b);

3.compareTo()字符串比较,字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符串,并返回字符串中第一个字母ASCII的差值。

 String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;
 
      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );  //忽略大小写
      System.out.println( str.compareTo(objStr.toString()));

4.indexOf()字符串查找索引出现的位置。String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1。

 String strOrig = "Google Java Taobao";
      int intIndex = strOrig.indexOf("Java");
      if(intIndex == - 1){
         System.out.println("没有找到字符串 Java");
      }else{
         System.out.println("Java 字符串位置 " + intIndex);
      }
paramPrimarykey.indexOf(",")==0

5.lastIndexOf()查找字符串最后一次出现的位置

 String strOrig = "Hello world ,Hello Runoob";
      int lastIndex = strOrig.lastIndexOf("Hello");
      if(lastIndex == - 1){
         System.out.println("没有找到字符串");
      }else{
         System.out.println("字符串最后出现的位置: "+ lastIndex);
      }

6.substring()截取字符串

通过字符串函数 substring() 函数来删除字符串中的一个字符,我们将功能封装在 removeCharAt 函数中。

StringBuilder paramSubval=new StringBuilder();
paramPrimarykey.substring(1)
String Str = new String("www.taobao.com");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );

返回值 :taobao.com
返回值 :taobao

字符串截取获取除最后一位之前的字符串

condictionSql = "UPDATE "+templateTable+" SET "+updateSqlValue.substring(0, updateSqlValue.length()-1)+ updateCondition +"";

7.reverse() 字符串反转

Java 的反转函数 reverse() 可字符串反转。

String str="Hello Java.";
            String reverse = new StringBuffer(str).reverse().toString();
            System.out.println("字符串反转前:"+str);
            System.out.println("字符串反转后:"+reverse);

8.split()字符串分割

split(string) 方法通过指定分隔符将字符串分割为数组。

String str="www-baidu-com";
            String[] temp;
            String delimeter = "-";  //指定分隔符
            temp = str.split(delimeter);  //分割字符串
            //普通for循环
            for(int i =0; i < temp.length; i++){
                System.out.println(temp[i]);
                System.out.println("");
            }
            
            System.out.println("----java for each循环输出的方法-----");
            String str1 = "www.baidu.com";
            String[] temp1;
            String delimeter1 = "\\.";   //指定分隔符,.号需要转义
            temp1 = str1.split(delimeter1);
            for (String x : temp1){
                System.out.println(x);
                System.out.println("");
            }    

9.toUpperCase() 字符串小写转大写

String toUpperCase() 方法将字符串从小写转为大写。

String str = "string runoob";
String strUpper = str.toUpperCase();

10.regionMatches()测试两个字符串区域是否相等

 String first_str = "Welcome to Microsoft";
      String second_str = "I work with microsoft";
      boolean match1 = first_str.
      regionMatches(11, second_str, 12, 9);
      boolean match2 = first_str.
      regionMatches(true, 11, second_str, 12, 9); //第一个参数 true 表示忽略大小写区别
      System.out.println("区分大小写返回值:" + match1);
      System.out.println("不区分大小写返回值:" + match2);

first_str.regionMatches(11, second_str, 12, 9) 表示将 first_str 字符串从第11个字符"M"开始和 second_str 字符串的第12个字符"M"开始逐个比较,共比较 9 对字符,由于字符串区分大小写,所以结果为false。

如果设置第一个参数为 true ,则表示忽略大小写区别,所以返回 true。 

11.append()连接字符串

通过 "+" 操作符和StringBuffer.append() 方法来连接字符串。

  long startTime = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            String result = "This is"
            + "testing the"
            + "difference"+ "between"
            + "String"+ "and"+ "StringBuffer";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 + 操作符 : " 
        + (endTime - startTime)+ " ms");
        long startTime1 = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            StringBuffer result = new StringBuffer();
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 StringBuffer : "
        + (endTime1 - startTime1)+ " ms");
字符串连接 - 使用 + 操作符 : 0 ms 
字符串连接 - 使用 StringBuffer : 42 ms

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/82710555
今日推荐