String 类中的常用方法

String s1 ="monkey1024";

String s2 =new String  ("monkey1024");

System.out.println(s1.equals(s2));  // true

String s3 ="sadsadaasa";

String s4 ="woaini";

System.out.println(s3.charAt(6));  // 获取index位置的字符

System.out.println(s4.contains("aini"));  // 判断字符串中是否包含ai

String s5 ="wangzherongyao1021";

System.out.println(s5.endsWith("1021"));  // 判断是否以某个字符串结尾

System.out.println(s5.equalsIgnoreCase("WANGZHERONGYAO1021"));  // 忽略大小写

String s6 ="abc";

byte [] b1  = s6.getBytes();  // 这个方法转换成byte数组

for (int i =0; i < b1.length ; i++) {

System.out.println(b1[i] +",");

}

String s7 ="sdsadawadwwf";

System.out.println(s7.indexOf("d"));  // 取得字符在字符串的位置

System.out.println(s7.indexOf("s",1)); // 取得第二s字符的位置

System.out.println(s7.lastIndexOf("w"));  // 从最后面找 找到了第一个w的位置就是10

System.out.println(s7.lastIndexOf("w" , 8));

System.out.println(s7.length());  // 字符串长度

String s8 ="woleasd";

System.out.println(s8.replaceAll("easd" , "wwww"));  // 替换字符串中的元素

String s9 ="a,b,c,d";

String [] array19 = s9.split(",");    // 拆分字符串把,拆掉

for (int y =0 ; y < array19.length ; y++) {

System.out.print(array19[y] +" ");

}

String s10 ="monkey";

System.out.println(s10.startsWith("mo"));  // 判断是否以每个字符串开始的

System.out.println(s10.substring(2));      // 根据输入的索引位置截取子串

System.out.println(s10.substring(1,3));

String s11 ="a,b,c,d";              // 将字符串转换成char类型的数组

char [] array12 = s11.toCharArray();

for (int x =0 ; x < array12.length ; x++) {

System.out.println(array12[x]);

}

String s12 ="a,b,c,d";

System.out.println(s12.toUpperCase());  // 转换为大写

String s13 ="A,B,C,D";

System.out.println(s13.toLowerCase());  // 转换为小写

String s14 =" java good ok ";      // 去除首尾空格 注意 不是去除中间空格

System.out.println(s14.trim());

Object o =new Object();

System.out.println(String.valueOf(o));

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • package cn.itcast_01;/* 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。 ...
    蛋炒饭_By阅读 622评论 0 0
  • 返回char指定索引处的值 将指定的字符串连接到该字符串的尾部 将此字符串与指定的字符串进行比较 将字符串按指定的...
    very_cute_girls阅读 145评论 0 1
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 752评论 0 0
  • 在编写程序的过程中,不了避免的要用到字符串,所以String类的常用方法的用法是必须掌握的。学习一个类的使用方法最...
    Geg_Wuz阅读 1,304评论 0 4
  • final关键字(最终) final修饰的类无法被继承. final修饰的方法无法被覆盖. final修饰的局部变...
    yangliangliang阅读 707评论 0 0