public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "hello";
//"hello"存在于data segment数据区,编译器会对数据区进行优化,已经存在的字符串常量,不会在重新分配内存存储,所以s1,s3指向数据区中的同一个字符串常量
System.out.println(s1 == s3);//true
s1 = new String("hello");
s2 = new String("hello");
//这里新建了两个字符串对象,虽然两个字符串的对象的内容都指向数据区中的同一个字符串常量,但是s1,s2指向的却是堆中的两个不同的String对象
System.out.println(s1 == s2);//false
//String对象中的equals方法重写了Object中的equals方法
/*
* public boolean equals(Object anObject)比较此字符串与指定的对象。当且仅当该参数不为 null,并且是表示与此对象相同的字符序列的 String 对象时,结果才为 true。
覆盖:
类 Object 中的 equals
参数:
anObject - 与此 String 进行比较的对象。
返回:
如果 String 相等,则返回 true;否则返回 false。
*/
System.out.println(s1.equals(s2));//true
char c[] = {'s', 'u', 'n', ' ', 'j', 'a', 'v', 'a'};
String s4 = new String(c);
/*
* String
public String(char[] value,
int offset,
int count)分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。该子数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。
参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
*/
String s5 = new String(c, 4, 4);
System.out.println(s4);//sun java
System.out.println(s5);//java
}
}
常用方法:
public char charAt(int index) 返回字符串中第index个字符,从0开始
public int length() 返回字符串长度,注意区分数组中的length属性
public int indexOf(String str) 返回字符串中出现子串str的第一个位置
public int indexOf(String str, int formIndex) 返回字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another) 比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符
public class StringTest2 {
public static void main(String[] args) {
String s1 = "sun java";
String s2 = "Sun Java";
System.out.println(s1.charAt(1));//u
System.out.println(s2.length());//8
System.out.println(s1.indexOf("java"));//4
System.out.println(s1.indexOf("Java"));//-1,没有找到
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
String s = "我是程序员,我在学习java";
String sr = s.replace('我', '你');
System.out.println(sr);//你是程序员,你在学java
}
}
public boolean startsWith(String prefix) 判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix) 判断字符串是否以prefix字符串结尾
public String toUpperCase() 返回一个字符串为该字符串的大写形式
public String toLowerCase() 返回一个字符串为该字符串的小写形式
public String substring(int beginIndex) 返回该字符串从beginIndex开始到结尾的子字符串
public String substring(int beginIndex, int endIndex) 返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim() 返回将该字符串去掉开头和结尾空格后的字符串
public class StringTest3 {
public static void main(String[] args) {
String s = "Welcome to Java World!";
String s1 = " sun java ";
System.out.println(s.startsWith("Welcome"));//true
System.out.println(s.endsWith("World"));//false
String sL = s.toLowerCase();
String sU = s.toUpperCase();
System.out.println(sL);//welcome to java world!
System.out.println(sU);//WELCOME TO JAVA WORLD!
String subS = s.substring(11);//Java World!
String sp = s1.trim();
System.out.println(sp);//sun java
}
}
public static String valueOf(...) 可以将基本数据类型转换为字符串
public String[] split(String regex) 可以将一个字符串按照指定的分隔符分割,返回分割后的字符串数组
public class StringTest4 {
public static void main(String[] args) {
int j = 1234567;
String sNumber = String.valueOf(j);
System.out.println(sNumber + "是" + sNumber.length() + "位数。");//1234567是7位数。
String s = "Mary, F, 1976";
String[] strSplit = s.split(",");
for(int i=0; i<strSplit.length; i++) {
System.out.println(strSplit[i]);
}
/*
* Mary
F
1976
*/
System.out.println(String.valueOf(true));//true
}
}
本文深入解析Java中字符串的创建、比较、操作等核心方法及特性,包括字符串常量池优化、equals方法重写、字符串拼接与截取、大小写转换、匹配与搜索等关键概念。

3万+

被折叠的 条评论
为什么被折叠?



