1.字符串的比较
字符串的比较只能用equals
package com.qfedu.b_string;
/*
* 字符串比较问题
*/
public class Demo1 {
public static void main(String[] args) {
String str1 = "千锋教育";
String str2 = "千锋教育";
// 以下两种new String不推荐的
String str3 = new String("千锋教育");
String str4 = new String(str1);
System.out.println("str1 == str2 : " + (str1 == str2));
System.out.println("str1 == str4 : " + (str1 == str4));
System.out.println("str2 == str3 : " + (str2 == str3));
System.out.println("str3 == str4 : " + (str3 == str4));
System.out.println("------------------------------------");
// 无论什么时候字符串比较有且只能使用equals方法!!!
System.out.println("str1.equals(str2) : " + str1.equals(str2));
System.out.println("str1.equals(str4) : " + str1.equals(str4));
System.out.println("str2.equals(str3) : " + str2.equals(str3));
System.out.println("str3.equals(str4) : " + str3.equals(str4));
}
}
1.2获取方法
int length();
获取字符串长度""
char charAt(int index);
获取String字符串指定下标位置的char类型字符,如果index超出范围;
StringIndexOutOfBoundsException
int indexOf(char ch);
int indexOf(String str);
int indexOf(char ch,int fromIndex)
int indexOf(String str,int fromIndex)
这些方法都是获取元素所在位置,元素可以是char类型字符,也可以是字符串,这找出的是指定元素在字符串中第一次出现的位置,当然可以通过一定的约束,从哪个位置开始找fromIndex;
int lastindexOf(char ch);
int lastindexOf(String str);
int lastindexOf(char ch,int fromIndex)
int lastindexOf(String str,int fromIndex)
这些方法都是获取元素所在位置,元素可以是char类型字符,也可以是字符串,这找出的是指定元素在字符串中最后一次出现的位置,当然可以通过一定的约束,从哪个位置开始找fromIndex;
package com.qfedu.b_string;
public class Demo2 {
public static void main(String[] args) {
String str = "土豆牛肉土豆牛肉";
System.out.println("str.length() : " + str.length());
System.out.println("str.charAt(0) : " + str.charAt(0));
// System.out.println("str.charAt(0) : " + str.charAt(4));
System.out.println("str.indexOf('土') : " + str.indexOf('土'));
System.out.println("str.indexOf('面') : " + str.indexOf('面'));
System.out.println("str.indexOf(\"土豆\") : " + str.indexOf("土豆"));
System.out.println("str.indexOf(\"油泼面\") : " + str.indexOf("油泼面"));
System.out.println("str.indexOf('土', 2) : " + str.indexOf('土', 2));
System.out.println("str.indexOf('面', 2) : " + str.indexOf('面', 2));
System.out.println("str.lastIndexOf('土') : " + str.lastIndexOf('土'));
System.out.println("str.lastIndexOf('面') : " + str.lastIndexOf('面'));
System.out.println("str.lastIndexOf(\"土豆\") : " + str.lastIndexOf("土豆"));
System.out.println("str.lastIndexOf(\"油泼面\") : " + str.lastIndexOf("油泼面"));
System.out.println("str.indexOf('土', 2) : " + str.indexOf('土', 2));
System.out.println("str.indexOf('面', 2) : " + str.indexOf('面', 2));
}
}
1.3判断方法
boolean endsWith(String str);
判断当前字符串是否以指定字符串结尾
boolean isEmpty();
判断字符串是否为空""空串
boolean equals(object obj);
继承重写Object类内的方法,完成字符串要求的比较方式
boolean equalsIgnoreCase(String str);
不区分大小写比较
boolean contains(String str);
判断指定字符串是否存在
/*
* 字符串判断方法
*/
public class Demo3 {
public static void main(String[] args) {
String str1 = "红烧茄子";
boolean ret = str1.endsWith("茄子");
System.out.println(ret);
boolean ret2 = str1.isEmpty();
System.out.println(ret2);
System.out.println("".isEmpty());
// String str = null;
// System.out.println(str.isEmpty());
System.out.println("ABCDEFG".contains("AB"));
System.out.println("Abcdefg".equals("ABCDEFG"));
System.out.println("Abcdefg".equalsIgnoreCase("ABCDEFG"));
}
}
1.4转换方法
String(char[] arr);
使用字符数组中内容创建一个字符串对象
String(char[] arr, int offset, int length);
String(char[] arr, int off, int len);
String(char[] arr, int off, int cou);
String(char[] arg0, int arg1, int arg2);
使用字符数组中内容创建一个字符串对象,offset是从char类型数组中指定下标位置开始获取数据,获取的数据长度是length
static String valueOf(char[] arr);
通过类名调用的静态方法,实际执行的是String(char[] arr);
static String valueOf(char[] arr, int offset, int length);
通过类名调用的静态方法,实际执行的是String(char[] arr, int offset, int length);
char[] toCharArray();
返回当前字符串对应的字符数组
import java.util.Arrays;
/*
* 转换方法
*/
public class Demo4 {
public static void main(String[] args) {
char[] arr = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
String str1 = new String(arr);
System.out.println(str1);
String str2 = new String(arr, 2, 5);
System.out.println(str2);
String str3 = String.valueOf(arr);
System.out.println(str3);
String str4 = String.valueOf(arr, 2, 5);
System.out.println(str4);
char[] charArray = "这个字符串会变成一个字符数组".toCharArray();
System.out.println(Arrays.toString(charArray));
}
}
1.5 其他方法
String replace(char oldChar, char newChar)
替换,替换不会修改原始的字符串,会创建一个新字符串返回,并且替换效果是所有的对应 的oldChar全部替换成newChar ???
String[] split(String regex)
按照指定的字符串切割当前字符串
[00:00:15]XXXXXXX
String substring(int beginIndex)
从指定位置开始,截取子字符串,到字符串末尾
String substring(int beginIndex, int endIndex)
从指定位置开始beginIndex,到endIndex结束,要头不要尾
String toUpperCase() 转大写
字符串小写转大写
String toLowerCase() 转小写
字符串大写转小写
String trim() 去除空格
去除字符串两边的无用空格
import java.util.Arrays;
/*
* 字符串其他方法
*/
public class Demo5 {
public static void main(String[] args) {
System.out.println("abcdabcdabcd".replace('a', 'A'));
System.out.println("abcdabcdabcd".replaceAll("ab", "AB"));
System.out.println("abcdabcdabcd".replaceFirst("ab", "AB"));
String[] split = "A,B,C,D,E,F,G".split(",");
System.out.println(Arrays.toString(split));
System.out.println("ABCDEFG".substring(2));
System.out.println("ABCDEFG".substring(2, 5));
System.out.println("abcdefg".toUpperCase());
System.out.println("ABCDEFG".toLowerCase());
System.out.println(" ABCDEFG ABCDEFG ");
System.out.println(" ABCDEFG ABCDEFG ".trim());
// 处理标题
}
}