java中String的常用方法,含代码运行结果

 

 

string的常用方法

String s1 = "hello";

      String s2 = "world";

      String s3 = "he";

      char[] c = {'h','e','l','l','o','h','e','l','l','o'};

      String result = null;

     

     

     

         1.charAt(1) 返回指定索引处的值

      char charAt = s1.charAt(1);

      System.out.println(charAt);  //e

     

         2. codePointAt(1)  返回指定索引处的字符(Unicode代码点)。

      System.out.println(s1.codePointAt(1));  //101

      System.out.println(s1.codePointAt(2));  //108

      System.out.println(s1.codePointAt(3));  //108

     

         3.codePointBefore(1)  返回指定索引之前的字符(Unicode代码点)。

      System.out.println(s1.codePointBefore(2));  //101

      System.out.println(s1.codePointBefore(3));  //108

      System.out.println(s1.codePointBefore(4));  //108

     

      System.out.println("----------------------------");

     

        4.s1.compareTo(s2)  返回第一个不同的字符的值的差,如果字符相同则返回字符串长度的差

      int compareTo = s1.compareTo(s2);

      System.out.println(compareTo);  //-15 hw的差为-15

     

      System.out.println("a".compareTo("b")); //-1

      System.out.println("a".compareTo("daaa")); //-3

      System.out.println("a".compareTo("d")); //-3

      System.out.println("a".compareTo("A")); //32

     

      //compareToIgnoreCase不区分大小写

      System.out.println("a".compareToIgnoreCase("b")); //-1

      System.out.println("a".compareToIgnoreCase("c")); //-2

      System.out.println("a".compareToIgnoreCase("d")); //-3

      System.out.println("a".compareToIgnoreCase("A")); //0

     

     

         5.concat(s2) 拼接字符串

      System.out.println(s1.concat(s2));  //helloworld

     

     

         6. contains("hel") 是否包含某个字符串

      boolean contains = s1.contains("hel");

      System.out.println(contains);  //true

     

     

     

      System.out.println("-------------------------------------------");

     

         7.equals 对比两个string类型的数据是否相同

         8.contentEquals 比较stringBuffer

      StringBuffer sb = new StringBuffer("hello");

      boolean equals = s1.equals(sb);

      System.out.println(equals);  //false

      boolean contentEquals = s1.contentEquals(sb);

      System.out.println(contentEquals);  //true

     

     

         9.copyValueOf 静态方法,从char数组的第2个数,向后取5个数,返回一个string数据

      String copyValueOf = String.copyValueOf(c,2,5);

      System.out.println(copyValueOf);

     

     

         10.endsWith/startsWith 判断是否由字符串结束/开始

      System.out.println(s1.startsWith("he"));  //true

      System.out.println(s1.endsWith("lo"));  //true

     

         11.getBytes  使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。

      byte[] bytes = s1.getBytes();

      System.out.println(Arrays.toString(bytes));  //[104, 101, 108, 108, 111]

     

         12.indexOf 返回字符串第一次出现的位置

      System.out.println(s1.indexOf('o'));  //4

      System.out.println(s1.indexOf("ll"));  //2

     

     

         13.intern

      //如果常量池中存在当前字符串, 就会直接返回当前字符串.

      //如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回。

      String s5 = new String("hello");

      System.out.println(s1 == s5.intern());  //true

      System.out.println(s1 == s5);  //false

     

      System.out.println("-------------------------------------------");

     

     

         14.lastIndexOf 最后一次出现字符串的位置

      System.out.println(s1.lastIndexOf("l"));

     

         15.replace 替换字符串中的所有char字符

      String replace = s1.replace('l', 'b');

      System.out.println(replace);  //hebbo

     

     

     

         16.split(",")  按,分隔字符串,返回一个字符串数组

      String s6 = "h,e,l,l,p,a,d,g,d,s,dg,s,f,g";

      String[] split = s6.split(",");

      for(String string : split) {

          System.out.print(string); //hellpadgdsdgsfg

      }

       

      System.out.println();

     

         17.substring(2, 6) 返回一个字符串从2-6    subSequence(2, 6) 返回一个字符序列,本质就是substring(2, 6);

      CharSequence substring = s6.substring(2, 6);

      System.out.println(substring);  //e,l,

      CharSequence subSequence = s6.subSequence(2, 6);

      System.out.println(subSequence);  //e,l,

     

         18.toCharArray()  将字符串转换为字符数组

      char[] charArray = s6.toCharArray();

      for(char c1 : charArray) {

          System.out.print(c1); //h,e,l,l,p,a,d,g,d,s,dg,s,f,g

      }

      System.out.println();

     

         19.toUpperCase() 转大写

      String upperCase = s6.toUpperCase();

      System.out.println(upperCase);  //H,E,L,L,P,A,D,G,D,S,DG,S,F,G

     

         20.trim()  删除字符串前后的空格

      String s7 = " he  llo  ";

      String trim = s7.trim();

      System.out.println(trim);  //he  llo

     

         21.valueOf(d) 静态方法,将object,char[]类型转为string类型数据

     

double d = 22;

      String valueOf = String.valueOf(d);

      System.out.println(valueOf);  //22.0

      String valueOf2 = String.valueOf(charArray);

   System.out.println(valueOf2);//h,e,l,l,p,a,d,g,d,s,dg,s,f,g

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
String a="hello world"; //在java有一个常量池,当创建String 类型的引用变量给它赋值时,java会到它的常量池找"hello world"是不是在常量池已存在。如果已经存在则返回这个常量池的"hello world"的地址(在java叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址值,而这个值是指向一个字符串对象的。在程序凡是以"hello world"这种常量似的形式给出的都被放在常量池String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么会出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址值,只是java为了安全不允许我们对想c语言的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值