zoukankan      html  css  js  c++  java
  • Java字符串常见实例与函数

    • 字符串比较

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

    public class StringCompareEmp{
       public static void main(String args[]){
          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()));
       }
    }
    • 字符串查找

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

    public class SearchStringEmp {
       public static void main(String[] args) {
          String strOrig = "Google Java Taobao";
          int intIndex = strOrig.indexOf("Java");
          if(intIndex == - 1){
             System.out.println("没有找到字符串 Java");
          }else{
             System.out.println("Java 字符串位置 " + intIndex);
          }
       }
    }
    • 查找字符串最后一次出现的位置

    字符串函数 strOrig.lastIndexOf(Stringname) 来查找子字符串 Stringname 在 strOrig 出现的位置。

    public class SearchlastString {
       public static void main(String[] args) {
          String strOrig = "Hello world ,Hello Runoob";
          int lastIndex = strOrig.lastIndexOf("Hello");
          if(lastIndex == - 1){
             System.out.println("没有找到字符串");
          }else{
             System.out.println("字符串最后出现的位置: "+ lastIndex);
          }
       }
    }
    •  删除字符串中的一个字符

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

    public class Main {
       public static void main(String args[]) {
          String str = "this is Java";
          System.out.println(removeCharAt(str, 3));
       }
       public static String removeCharAt(String s, int pos) {
          return s.substring(0, pos) + s.substring(pos + 1);
       }
    }
    •  字符串替换

     java String 类的 replace 方法可以替换字符串中的字符。

    public class test {
        public static void main(String args[]){
                String str="Hello World,Hello Java.";
                System.out.println(str.replace('H','W')); //替换全部
                System.out.println(str.replaceFirst("He","Wa")); //替换第一个遇到的
                System.out.println(str.replaceAll("He", "Ha")); //替换全部
           }
    }
    •  字符串反转

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

    public class test {
        public static void main(String args[]){
                String str="Hello Java.";
                String reverse = new StringBuffer(str).reverse().toString();
                System.out.println("字符串反转前:"+str);
                System.out.println("字符串反转后:"+reverse);
           }
    }
    •  字符串分割

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

    public class test {
        public static void main(String args[]){
                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("");
                }    
           }
    }
    •  字符串小写转大写

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

         String str = "string runoob";
            String strUpper = str.toUpperCase();
    •  测试两个字符串区域是否相等
    public class StringRegionMatch{
       public static void main(String[] args){
          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。 

    • 连接字符串

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

    public class StringConcatenate {
        public static void main(String[] args){
            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
  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/dear_diary/p/6591130.html
Copyright © 2011-2022 走看看