String
是常量,创建之后不可以改变,字面值存储在常量池中(jdk1.7后位于堆中),可以共享。
String name ="miaomi";
创建了一个对象,存储在常量池当中;
String name1 =new String(" miaomi");
创建了两个对象,分别存储在常量池与堆中;
public class Demo01 {
public static void main(String [] args) {
String a1 ="hello world";
String a4 =" hello world haha";
String a6="asdfghjkasdfsdfasdfadsfassdfsffasdfs";
String aa="666";
String a2 =a1.toUpperCase();//1.将字符串转换为大写
System.out.println(a2);
String a3 =a2.toLowerCase();//2.将字符串转为小写
System.out.println(a3);
System.out.println(a3.charAt(6));//3.获取字符串指定下标位置的字符从0开始从左到右
System.out.println(a2.codePointAt(6));//4.获取指定字符 的char表示 码
System.out.println(a1.codePointAt(6));//4.1获取指定字符 的char表示 码(大写小写一样)
System.out.println(a1.codePointCount(6,9));//5.获取起始字符到目标字符的间隔字符数
System.out.println(a1.compareTo(a2));//6.比较字符相差多少
System.out.println(a1.compareToIgnoreCase(a1));//7.比较字符不考虑大小写
System.out.println(a1.compareToIgnoreCase(aa));//7.1比较字符不考虑大小写
System.out.println(a1.concat(a2));//8.将指定字符串与目标字符串拼接
System.out.println(a1.contains("he"));//9.判断该字符串中是否有指定字符串返回Boolean
System.out.println(a1.contains("he"));//9.1判断该字符串中是否有指定字符串返回Boolean
System.out.println(a1.contentEquals(a1));//10.判断字符串内容是否相同返回Boolean
System.out.println(a1.contentEquals(a2));//10.1判断字符串内容是否相同返回Boolean
System.out.println(a1.endsWith("ld"));//11.判断是否以指定字符串结尾返回Boolean
System.out.println(a1.equals(a2));//12.判断与指定字符串内容是否相等,区分大小写,返回Boolean
System.out.println(a1.equalsIgnoreCase(a2));//13.判断与指定字符串内容是否相等,不区分大小写,返回Boolean
System.out.println(a1.hashCode());//14.返回字符串的哈希值
System.out.println(a1.indexOf("o"));//15.返回指定字符首次出现在字符串下标的位置,不存在返回-1
System.out.println(a1.indexOf("ol"));//16.返回指定字符串首次出现在字符串下标的位置,不存在返回-1
System.out.println(a1.indexOf("o", 2));//16.1返回指定字符串首次出现在字符串下标的位置,不存在返回-1
System.out.println(aa.intern());//17.字符串添加到字符串常量池
char[] an=a1.toCharArray();//18.将字符转换为数组,
for(int i=an.length-1;i>=0;i--) {
System.out.print(an[i]);
}
System.out.println();
System.out.println(a4);//1
System.out.println(a4.trim());//19.将字符串前后的空格清空
System.out.println(a1.length());//20.返回字符串的长度
System.out.println(a1.lastIndexOf("o"));//21.返回指定字符最后出现位置的下标
System.out.println(a1.indexOf("o"));//返回指定字符首次出现位置的下标
System.out.println(a1.replace('o', 'x'));//22.目标字符替换成指定字符
//------------------------以下补充----------------
String string= "helloword";
int a=2;
String string1=string.substring(a,string.length());//substring 截取字符(指定位置(indexOf),指定长度(length));
当且仅当 length() 为 0 时返回 true。
String str = UUID.randomUUID().toString();//返回一个随机的字符串
/*
* 欢迎各位盆友纠正,补充
*
*
*
*
*
*
*/
}
}
欢迎纠正,补充,一起进步!