* 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
* 通过查看API,我们可以知道
* A:字符串字面值"abc"也可以看成是一个字符串对象。
* B:字符串是常量,一旦被赋值,就不能被改变。
*
* 构造方法:
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
*
* 字符串的方法:
* public int length():返回此字符串的长度。
* String s = new String(“hello”)和String s = “hello”;的区别?
* 有。前者会创建2个对象,后者创建1个对象。
*
* ==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
* 看程序写结果
* 字符串如果是变量相加,先开空间,在拼接。
* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);// false
System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
System.out.println(s3.equals("hello" + "world"));// true // 通过反编译看源码,我们知道这里已经做好了处理。
// System.out.println(s3 == "helloworld");
// System.out.println(s3.equals("helloworld"));
* String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。
*
* 注意:
* 字符串内容为空和字符串对象为空。
* String s = "";
* String s = null;
String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());
* String类的获取功能
* int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* 为什么这里是int类型,而不是char类型?
* 原因是:'a'和97其实都可以代表'a'
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
// 定义一个字符串对象
String s = "helloworld"; // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('l', 4));
System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
System.out.println("----------------------"); // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
System.out.println("substring:" + s.substring(5));
System.out.println("substring:" + s.substring(0));
System.out.println("----------------------"); // String substring(int start,int
// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
System.out.println("substring:" + s.substring(3, 8));
System.out.println("substring:" + s.substring(0, s.length()));
字符串遍历:
for (int x = 0; x < s.length(); x++) {
// char ch = s.charAt(x);
// System.out.println(ch);
// 仅仅是输出,我就直接输出了
System.out.println(s.charAt(x));
}
判断该字符到底是属于那种类型的:
if(ch>='a' && ch<='z'){
smallCount++;
}else if(ch>='A' && ch<='Z'){
bigCount++;
}else if(ch>='0' && ch<='9'){
numberCount++;
}
* String的转换功能:
* byte[] getBytes():把字符串转换为字节数组。
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
* String toLowerCase():把字符串转成小写。
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。
* String类的其他功能:
*
* 替换功能:
* String replace(char old,char new)
* String replace(String old,String new)
*
* 去除字符串两空格
* String trim()
*
* 按字典顺序比较两个字符串
* int compareTo(String str)
* int compareToIgnoreCase(String str)
String类的compareTo方法的源码解析:
private final char value[]; 字符串会自动转换为一个字符数组。 public int compareTo(String anotherString) {
//this -- s1 -- "hello"
//anotherString -- s2 -- "hel" int len1 = value.length; //this.value.length--s1.toCharArray().length--5
int len2 = anotherString.value.length;//s2.value.length -- s2.toCharArray().length--3
int lim = Math.min(len1, len2); //Math.min(5,3); -- lim=3;
char v1[] = value; //s1.toCharArray()
char v2[] = anotherString.value; //char v1[] = {'h','e','l','l','o'};
//char v2[] = {'h','e','l'}; int k = 0;
while (k < lim) {
char c1 = v1[k]; //c1='h','e','l'
char c2 = v2[k]; //c2='h','e','l'
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2; //5-3=2;
} String s1 = "hello";
String s2 = "hel";
System.out.println(s1.compareTo(s2)); //
* 需求:把数组中的数据按照指定个格式拼接成一个字符串:
public static String arrayToString(int[] arr) {
// 定义一个字符串
String s = ""; // 先把字符串拼接一个"["
s += "["; // 遍历int数组,得到每一个元素
for (int x = 0; x < arr.length; x++) {
// 先判断该元素是否为最后一个
if (x == arr.length - 1) {
// 就直接拼接元素和"]"
s += arr[x];
s += "]";
} else {
// 就拼接元素和逗号以及空格
s += arr[x];
s += ", ";
}
} return s;
}
* 字符串反转
public static String myReverse(String s) {
// 定义一个新字符串
String result = ""; // 把字符串转成字符数组
char[] chs = s.toCharArray(); // 倒着遍历字符串,得到每一个字符
for (int x = chs.length - 1; x >= 0; x--) {
// 用新字符串把每一个字符拼接起来
result += chs[x];
}
return result;
}
* 统计大串中小串出现的次数
* 举例:
* 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
* 结果:
* java出现了5次
public static int getCount(String maxString, String minString) {
// 定义一个统计变量,初始化值是0
int count = 0; int index;
//先查,赋值,判断
while((index=maxString.indexOf(minString))!=-1){
count++;
maxString = maxString.substring(index + minString.length());
} return count;
}
String的特点一旦被赋值就不能改变: