String类

字符串的本质是一个字符的数组。

构造方法是用来完成String对象的创建,下图中给出了一部分构造方法需要在API中找到,并能够使用下列构造方法创建对象。

String常用方法:

  1、int length();  语法:字符串变量名.length();  返回值为 int 类型。得到一个字符串的字符个数(中、英、空格、转义字符皆为字符,计入长度)

 String a="挨点aidian 	
";
        int l = a.length();
        System.out.println(l);

运行结果:11

 

2、char charAt(值);  语法 :字符串名.charAt(值);  返回值为 char 类型。从字符串中取出指定位置的字符 

 String str="张三疯";
        char c = str.charAt(2);  
        System.out.println("指定字符为:" + c); 

运行结果:指定字符为:疯

   3、char  toCharArray();  语法 :字符串名.toCharArray();  返回值为 char 数组类型。将字符串变成一个字符数组 

String str="张三疯"
char c[] = str.toCharArray(); 
for (int i = 0; i < c.length; i++)
System.out.println("转为数组输出:" + c[i]);
运行结果:

转为数组输出:张
转为数组输出:三
转为数组输出:疯

 4、int indexOf("字符")  语法 :字符串名.indexOf("字符");字符串名.indexOf("字符",值);查找一个指定的字符串是否存在,返回的是字符串的位置,如果不存在,则返回-1 。 

    in lastIndexOf("字符") 得到指定内容最后一次出现的下标

String str="只为挨你近一点";
int a1 = str.indexOf("你");// 查找字符你的位置 
int a2 = str.indexOf("为"2);// 查找为的位置,从第3个开始查找
int a3 = str.lastIndexOf("点"); 
System.out.println("你的位置为:" + a1); 
System.out.println("为的位置为:" + a2);
System.out.println("点最后出现的位置为:" + a3);
运行结果:

你的位置为:3

为的位置为:-1

点最后出现的位置为:7

   5、toUpperCase();  toLowerCase();字符串大小写的转换

String str="hello world";
System.out.println("将字符串转大写为:" + str.toUpperCase());
System.out.println("将字符串转换成小写为:" + str.toUpperCase().toLowerCase());

运算结果:

将字符串转大写为:HELLO WORLD
将字符串转换成小写为:hello world

  6、String[] split("字符")  根据给定的正则表达式的匹配来拆分此字符串。形成一个新的String数组。

String str = "boo:and:foo";
String[] arr1 = str.split(":");
String[] arr2 = str.split("o");
运行结果:
  arr1  //{ "boo", "and", "foo" }
  arr2  //{ "b", "", ":and:f" }

  7、boolean equals(Object anObject)  语法 :字符串变量名.wquals(字符串变量名);  返回值为布尔类型。所以这里用 if 演示。比较两个字符串是否相等,返回布尔值

String str = "hello";
                      String str1="world";  
                      if(str.equals(str1)) 
                      {  
                       System.out.println("这俩字符串值相等"); 
                      }  
                      else
                      {
                       System.out.println("这俩字符串值不相等");  
                      }
      运行结果:

          这俩字符串值不相等

 

  9、String substring(int beginIndex,int endIndex)  截取字符串 

String str = "123挨点博客456";  
         System.out.println("截取后的字符为:" + str.substring(0,3));// 截取0-3个位置的内容   不含3 
         System.out.println("截取后字符为:" + str.substring(2));// 从第3个位置开始截取    含2

  运行结果:

    截取后的字符为:123
    截取后字符为:3挨点博客456

  10、boolean equalsIgnoreCase(String) 忽略大小写的比较两个字符串的值是否一模一样,返回一个布尔值

String str = "HELLO WORLd";
        String str1 = "hello world";
        if(str.equalsIgnoreCase(str1))
        {
            System.out.println("两个字符串相等");
        }
        else
        {
            System.out.println("两个字符串不相等");
        }

运行结果:

    两个字符串相等

  11、boolean contains(String) 判断一个字符串里面是否包含指定的内容,返回一个布尔值

        String str = "HELLO WORLd";
        String str1 = "WO";
        if(str.contains(str1))
        {
            System.out.println("str内容中存在WO");
        }
        else
        {
            System.out.println("抱歉没找着");
        }
运行结果:

  str内容中存在WO

  

原文地址:https://www.cnblogs.com/sy130908/p/11437980.html