字符串

一、创建String类型对象的3种方式

  1.String name =new String();

  2.String name =new String("张三");

  3.String name="李四";

二、==和equals()方法的区别

  ==:用来比较数值类型,如果用++比较String(引用类型),会比较内存地址是否相等

  equals()方法:用来比较两个字符串的值是否相等

三、正则表达式

四、字符串常用方法

  1.查找指定字符串位置

    public int indexOf(int ch)       搜索第一个出现的字符ch(或字符串value)

    public int idexOf(String value)

    public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value)

         public int lastIndexOf(String value)

  2.截取字符串

    public String substring(int index)    提取从位置索引开始的字符串部分

    public String substring(int beginindex,int endindex)

    public String trim()        返回一个前后不含任何空格的调用字符串的副本

  3.拼接字符串

    1.使用“+”号

    2.使用concat()方法

  4.比较两个字符串的值

    equals()

    equalsIgnoreCase()忽略大小写

  5.字符串转换大小写

    toUpperCase()转换为大写
    toLowerCase()转换为小写
  6.字符串长度:length()
  7.字符串分割split()
    String str="长亭外,古道边,芳草碧连天,晚风拂 柳笛声残 夕阳山外山";
    //根据什么分割
    String[] split = str.split(",");
    for (int i = 0; i < split.length; i++) {
      System.out.println(split[i]);
    }
五.StringBuffer
  StringBuffer:String增强版
  创建StringBuffer对象:
    StringBuffer sb = new StringBuffer();
    StringBuffer sb = new StringBuffer("aaa");

六.StringBuffer对象的常用方法(append(),insert(),toString())
    StringBuffer sb=new StringBuffer("超级演说家马上开始");
    //拼接字符串
    sb.append("!");
    //再指定位置插入字符串
    sb.insert(5, ",");
    //从StringBuffer对象转换为String对象
    String string = sb.toString();
    

 1 public class SteingTest {
 2     public static void main(String[] args) {
 3 
 4         /**
 5          * 如何创建字符串对象? 1.方式一:new 2.方式二:new时赋值 3.方式三:创建的创建变量方式()
 6          */
 7         String name1 = new String();
 8         name1 = "王强";
 9         String name2 = new String("张三");
10         String name3 = "李四";
11         System.out.println(name1);
12         System.out.println(name2);
13         System.out.println(name3);
14 
15         System.out.println("=======================字符串拼接===========");
16         /**
17          * 方式一:使用+拼接时,会自动将其他类型的数组转换为String类型字符串 
18          * 方式二:使用concat()进行字符串拼接。
19          * 区别:concat()不会新创建字符串
20          */
21         System.out.println("王洪涛今年" + 1.1 + "岁" + 100);
22         String names = "王洪";
23         names = names.concat("德");
24         System.out.println(names);
25 
26         
27         
28         /**
29          * 字符串大小
30          */
31         String name="tom";
32         //大写
33         name=name.toUpperCase();
34         //小写
35         name=name.toLowerCase();
36         System.out.println(name);
37         
38         
39         System.out.println("=======================查找指定字符串的位置===========");
40         String address = "北京海淀区五道口区";
41         // indexOf字符串第一次出现的位置
42         int indexOf = address.indexOf("区");
43         System.out.println(indexOf);
44         // lastindexOf字符串最后一次出现的位置
45         int lastindexOf = address.indexOf("区");
46         System.out.println(lastindexOf);
47         //如果没有检测到,name返回值为-1
48         
49         System.err.println("=============分割字符串======");
50         String str="长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
51         //根据什么分割
52         String [] split=str.split(" ");
53         for (int i = 0; i < split.length; i++) {
54             System.out.println(split[i]);
55         }
56         System.err.println("=============StringBuffer======");
57         StringBuffer sb=new StringBuffer("超级演说家马上开始");
58         //拼接字符串
59         sb.append("!");
60         //插入字符串
61         sb.insert(5, ",");
62         //从StringBuffer对象转换为String对象
63         String string =sb.toString();
64         System.out.println(string);
65         
66 }
67 
68 }

System.out.println(string);

原文地址:https://www.cnblogs.com/F017/p/9812537.html