美文网首页
String类的常用方法

String类的常用方法

作者: 加一片柠檬233 | 来源:发表于2019-02-22 20:21 被阅读0次

1. compareTo

按字典顺序比较两个字符串。返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方。

如果参数字符串等于此字符串,则返回值 0;
如果此字符串小于字符串参数,则返回一个小于 0 的值;
如果此字符串大于字符串参数,则返回一个大于 0 的值。
实例

public class Test {
    public static void main(String args[]) {
        String str1 = "Strings";
        String str2 = "Strings";
        String str3 = "Strings123";
 
        int result = str1.compareTo( str2 );
        System.out.println(result);
      
        result = str2.compareTo( str3 );
        System.out.println(result);
     
        result = str3.compareTo( str1 );
        System.out.println(result);
    }
}

以上程序执行结果为:

0
-3
3

2. concat

concat() 方法用于将指定的字符串参数连接到字符串上。
语法

String str1 concat(String str2)

返回值:返回连接后的新字符串。
实例

public class Test {
    public static void main(String args[]) {
        String s = "abc";
        s = s.concat("123");
        System.out.println(s);//abc123
    }
}

3. equals

equals() 方法用于将字符串与指定的对象比较。

boolean equals(String str):区分大小写的比较
boolean equalsIgnoreCase(String str):不区分

示例1:

public class Demo { 
   public static void main (String args []){ 
       String str1= new String("Apple");
       String str2= new String("MANGO");
       String str3= new String("APPLE");
       System.out.println(str1.equalsIgnoreCase(str3));//true
       System.out.println(str1.equals(str3));//false
       System.out.println(str1.equals("Apple"));//true
       System.out.println(str2.equalsIgnoreCase("mango"));//true
     }
}

4. join

在Java字符串连接()方法返回与给定的分隔符加入了一个字符串。在字符串连接方法中,为每个元素复制分隔符。

 List<string> names=new List<string>();    
names.add("1");
names.add("2");
names.add("3");
System.out.println(String.join("-", names));//1-2-3

String[] arrStr=new String[]{"a","b","c"};
System.out.println(String.join("-", arrStr));//a-b-c

5. split

split() 方法根据匹配给定的正则表达式来拆分字符串,返回字符串数组。

注意: . 、 | 和 * 等转义字符,必须得加 \。

注意:多个分隔符,可以用 | 作为连字符。

public class Test {
    public static void main(String args[]) {
      String str = "a-b-c";
      String arr[]=str.split("-");
            System.out.println("- 分隔符返回值 :" );
            System.out.println(arr);//输出[Ljava.lang.String;@154617c
           //arr={"a","b","c"}
  }  
}

6. trim

trim():去掉字符串首尾的空格。

public static void main(String arg[]){
 
        String a=" hello world ";
 
        String b="hello world";
 
        System.out.println(b.equals(a));//false
 
        a=a.trim();//去掉字符串首尾的空格
 
        System.out.println(a.equals(b));//true
    }

7. isEmpty

isEmpty()方法检查String是否为空。如果给定的字符串为空,则此方法返回true,否则返回false。当字符串为 null 时,则此时字符串的isEmpty()会出现空指针异常.isEmpty()的判断和null的判断,两者是有区别的,不可作为一种情况去处理!!!

public class Test{  
   public static void main(String args[]){  
    String str1 = null; 
    String str2 = "book";  

    if(str1 == null || str1.isEmpty()){
       System.out.println("String str1 is empty or null"); 
    }
    else{
       System.out.println(str1);
    }
    if(str2 == null || str2.isEmpty()){
       System.out.println("String str2 is empty or null");  //String str1 is empty or null
    }
    else{
       System.out.println(str2);//book
    }
   }
}

相关文章

  • string类

    string类 1. string类常用方法 2. string类常用方法2 3. string类的查找 判断字符...

  • Java基础 - 常用类

    9.JDK常用类 9.1 String类 常用方法 String format(String format, O...

  • Java 中的 String 类常用方法 Ⅱ

    Java 中的 String 类常用方法 Ⅱ 我们继续来看 String 类常用的方法,如下代码所示: 运行结果:...

  • String

    1.String类2.String类和常量池3.String类常用的方法 1.String类 1.String类又...

  • Java中String类常用方法 + StringBuilder

    学习笔记:String类常用方法 + StringBuilder与String的相互转换 String 类代表字符...

  • String类常用方法总结

    String类常用方法总结 常用9种函数总结

  • 3/7day05_常用API(BigInteger,BigDec

    回顾 今日内容 BigInteger类 BigDecimal类 Arrays类 包装类 String类的常用方法 ...

  • Java常用类笔记

    字符串相关的类 String类及常用方法 String的特性 String类:代表字符串。Java 程序中的所有字...

  • String类常用方法

    一.1.getChars : 方法将字符从字符串复制到目标字符数组 二.替换:String replace(ch...

  • String类常用方法

    compareTo 比较两个字符串,返回值为整数。 比较规则为:取出两个字符串的长度,比较较小的长度内,两者是否相...

网友评论

      本文标题:String类的常用方法

      本文链接:https://www.haomeiwen.com/subject/nqkyyqtx.html