• String类基础知识


    1、String类的构造方法

    (1)String(String original)  //把字符串数据封装成字符串对象

    (2)String(char[] c)   //把字符数组的数据封装成字符串对象

    (3)String(char[] c, int index, int count)  // 把字符数组中的一部分数据封装成字符串对象

    示例:

     1 public class Demo01 {
     2     public static void main(String[] args) {
     3 
     4         String str01=new String("hello");
     5 
     6         char[] c1=new char[]{'h','e','l','l','o'};
     7         String str02=new String(c1);
     8 
     9         char[] c2=new char[]{'h','e','l','l','o','w','o','r','l','d'};
    10         String str03=new String(c2,0,5);
    11 
    12         System.out.println(str01);
    13         System.out.println(str02);
    14         System.out.println(str03);
    15         
    16     }
    17 }

    输出位:

    hello
    hello
    hello

    2、String类常用方法

    (1)public boolean equals(Object obj)

    将此字符串与指定的对象比较。若内容相等,返回true,否则,返回false。

    equal 和 == 的区别:

      equal:比较的是值是否相同

      ==:比较的是地址值是否相同

    示例:

     1 public class demo01 {
     2 
     3     public static void main(String[] args) {
     4         String s1="helloworld";
     5         String s2="hello";
     6         String s3="world";
     7         s2=s2+s3;
     8 
     9         boolean aBoolean1=s1==s2;
    10         boolean aBoolean2=s1.equals(s2);
    11         System.out.println("s1:"+s1+"   s2:"+s2);
    12         System.out.println(aBoolean1);
    13         System.out.println(aBoolean2);
    14     }
    15 }

    输出结果为:

    s1:helloworld   s2:helloworld
    false
    true

    (2)public boolean equalIgnoreCase(String otherString)

    将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。 

    示例:

     1 public class demo02 {
     2     public static void main(String[] args) {
     3         String s1="hello world!";
     4         String s2="Hello World!";
     5 
     6         boolean aBoolean1=s1.equals(s2);
     7         boolean aBoolean2=s1.equalsIgnoreCase(s2);
     8 
     9         System.out.println("s1:"+s1+"  s2:"+s2);
    10         System.out.println("s1.equal(s2):"+aBoolean1);
    11         System.out.println("s1.equalIgnoreCase(s2):"+aBoolean2);
    12     }
    13 }

    输出结果为:

    s1:hello world!  s2:Hello World!
    s1.equal(s2):false
    s1.equalIgnoreCase(s2):true

    (3)public boolean startsWith(String str)

    判断字符串对象是否以指定的str开头

    示例:

     1 public class Demo04 {
     2     public static void main(String[] args) {
     3         String s="hello world!";
     4 
     5         boolean b1=s.startsWith("helo");
     6         boolean b2=s.startsWith("hell");
     7 
     8         System.out.println(b1);
     9         System.out.println(b2);
    10     }
    11 }

    输出为:

    false
    true

    (4)public boolean endsWith(String str)

    判断字符串对象是否以指定的str结尾

     示例:

     1 public class Demo05 {
     2     public static void main(String[] args) {
     3         String s="hello world!";
     4 
     5         boolean b1=s.endsWith("ld!");
     6         boolean b2=s.endsWith("d!");
     7 
     8         System.out.println(b1);
     9         System.out.println(b2);
    10     }
    11 }

    输出为:

    true
    true

     

    (5)public int length()

    获取字符串的长度,其实也就是字符个数

    (6)public char charAt(int index)

    获取指定索引处的字符

    (7)public int indexOf(String str)

    获取str在字符串对象中第一次出现的索引

     示例:

     1 public class Demo06 {
     2     public static void main(String[] args) {
     3         String str="hello world! this is me";
     4 
     5         System.out.println("字符串"hello world! this is me"的长度为:"+str.length());
     6 
     7         char c=str.charAt(2);
     8         System.out.println("位于字符串str,索引为2的字符为:"+c);
     9 
    10         int index=str.indexOf("ll");
    11         System.out.println(""ll"在字符串str的索引位置是:"+index);
    12 
    13         
    14     }
    15 }

    输出为:

    字符串"hello world! this is me"的长度为:23
    位于字符串str,索引为2的字符为:l
    "ll"在字符串str的索引位置是:2

    (8)public String substring(int start)

    从start开始截取字符串

    (9)public String substring(int start,int end)

    从start开始,到end结束截取字符串。包括start,不包括end

    (10)public char[] toCharArray()

    把字符串转换为字符数组

    (11)public String toLowerCase()

    把字符串转换为小写字符串

    (12)public String toUpperCase()

    把字符串转换为大写字符串

    (13)public String trim()

    将去除字符串两端空格 

    (13)public String[] split (String str)

     按照指定符号分割字符串 

     

     

  • 相关阅读:
    Codeforces 1182E Product Oriented Recurrence 矩阵快速幂
    Codeforces 1182D Complete Mirror hash
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    web框架之Vue渐进式框架-安装及入门
    web框架之Jqeury基本使用
  • 原文地址:https://www.cnblogs.com/hanlk/p/11222033.html
Copyright © 2020-2023  润新知