1. String的内存分配情况
public class String1
{
public static void main(String[] args)
{
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
String s4 = new String("hello");
System.out.println("s1==s2: "+(s1==s2)); //== 比较的内存地址 //true
System.out.println("s2==s3: "+(s2==s3)); //false
System.out.println("s3==s4: "+(s3==s4)); //false
System.out.println("s1 equals s2: "+s1.equals(s2)); //true
System.out.println("s2 equals s3: "+s2.equals(s3)); //true
System.out.println("s3 equals s4: "+s3.equals(s4)); //true //equals 默认比较两对象的地址
s2 = "world";
System.out.println(s1); //hello
}
}
运行结果:
s1==s2: true
s2==s3: false
s3==s4: false
s1 equals s2: true
s2 equals s3: true
s3 equals s4: true
hello
2.String的构造方法
//使用字节数组或者整形数组都可以构造一个字符串
public class String2 {
public static void main(String[] args)
{
byte[] a = {97,98,99,100,101};
String s = new String(a);
System.out.println(s); //使用一个字节数组构建一个字符串
// public String(byte[] bytes,int offset, int length)
// bytes - 要解码为字符的 byte
// offset - 要解码的第一个 byte 的索引
// length - 要解码的 byte 数
s = new String(a,1,3); //0是第一个
System.out.println(s);
char[] c = {'c','h','a','r'};
s = new String(c);
System.out.println(s);
s = new String(c,2,2);
System.out.println(s);
int[] i = {97,98,99};
s = new String(i,0,3); //没有s = new String(i);的方法
System.out.println(s);
s = new String("abc");
System.out.println(s);
}
}
运行结果:
abcde
bcd
char
ar
abc
abc
3.String方法
① 获取方法
//int length() 获取字符串的长度
//char charAt(int index) 获取特定位置的字符 (角标越界)
//int indexOf(String str) 获取特定字符的位置(overload)
//int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。
public class String3
{
public static void main(String[] args)
{
String s = "abcdecdcd";
System.out.println(s.length()); //9
char c = s.charAt(2);
System.out.println(c); //c
int w = s.indexOf('b');
System.out.println(w); //1
w = s.lastIndexOf('c');
System.out.println(w); //7
}
}
②判断方法
//boolean endsWith(String str) 是否以指定字符结束
//boolean isEmpty()是否长度为0 如:“” null V1.6
//boolean contains(CharSequences) 是否包含指定序列 应用:搜索
//boolean equals(Object anObject) 是否相等
//boolean equalsIgnoreCase(String anotherString) 忽略大小写是否相等
public class String4
{
public static void main(String[] args)
{
String s = "abcdefg";
System.out.println(s.endsWith("fg")); //true
System.out.println(s.isEmpty()); //false
System.out.println(s.contains("gh")); //false
String s1 = "ABCDEFG";
System.out.println(s.equalsIgnoreCase(s1));//ture
}
}
③转换方法
String(char[] value) 将字符数组转换为字符串
String(char[] value, int offset, int count)
Static String valueOf(char[] data)
static String valueOf(char[] data, int offset, int count)
char[] toCharArray() 将字符串转换为字符数组
④ 其他
import java.util.Arrays;
//String replace(char oldChar, char newChar) 替换
//String[] split(String regex) 切割
//String substring(int beginIndex)
//String substring(int beginIndex, int endIndex)截取字串
//String toUpperCase() 转大写
//String toLowerCase() 转小写
//String trim() 去除首尾空格
public class String5
{
public static void main(String[] args)
{
String s1 = " aabbccd";
String s2 = s1.replace('b', 'c');
System.out.println(s2); // aaccccd
s2 = s1.replace("bb", "ccccccc");
System.out.println(s2); // aacccccccccd
s2 = s1.substring(2);
System.out.println(s2); //bbccd
s2 = s1.substring(2,4);
System.out.println(s2); //bb
String[] a = s1.split("c");
System.out.println(Arrays.toString(a)); //[ aabb, , d]
s2 = s1.trim();
System.out.println(s2); //aabbccd去除首尾的空格
s2 = s1.toUpperCase();
System.out.println(s2); // AABBCCD
}
}