public class Test {
public static void main(String[] args) {
String str = "hello";
System.out.println(str);
String str2 = new String("hehehe");
System.out.println(str2);
char[] array = {'a','b','c'};
String str3 = new String(array);
System.out.println(str3);
}
}
public class Test {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;
System.out.println(s3);
String s4 = "";//存储0
System.out.println(s4.length());
System.out.println(s4.isEmpty());
String s5 = null;//不存任何数
System.out.println(s5.length());
System.out.println(s5.isEmpty());
}
1.== //比较地址
2. equals //比较值相不相等
3.compareTo //比较值的大小
4.compareToIgnoreCase//忽略大小写比较
public class Test {
public static void main(String[] args) {
String s1 ="Student";
String s2 ="Student";
System.out.println(s1==s2);//比较值
String s3 = new String("JAVA");
String s4 = new String("JAVA");
System.out.println(s3 == s4);//比较地址一样不一样
System.out.println(s3.equals(s4));//比较值
String s5 ="abc";
String s6 ="adc";
//s5>s6 返回正数
//s5==s6 返回0
//s5
System.out.println(s5.compareTo(s6));
//忽略大小写比较
String s7 ="abc";
String s8 ="ABC";
System.out.println(s7.compareToIgnoreCase(s8)); //0
}
public static void main3(String[] args) {
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;
System.out.println(s3);//hello
String s4 = "";//存储0
System.out.println(s4.length());
System.out.println(s4.isEmpty());
String s5 = null;//不存任何数
// System.out.println(s5.length());
System.out.println(s5.isEmpty());
}
public static void main2(String[] args) {
String str = "hello";
System.out.println(str);
String str2 = new String("hehehe");
System.out.println(str2);
char[] array = {'a','b','c'};
String str3 = new String(array);
System.out.println(str3);//abc
}
}
}
找到字符串某个字符的下标的元素
public class Test {
public static void main(String[] args) {
//charAt
String s1 ="English";
char ch = s1.charAt(3);//3下标的值
System.out.println(ch);
//遍历字符串
for (int i = 0; i <s1.length() ; i++) {
char sh = s1.charAt(i);
System.out.print(sh);
}
}
}
int indexOf(int ch)
在字符串中 第一次出现某个字符的下标,没有返回-1
public class Test {
public static void main(String[] args) {
String str = "abacabc";
int index = str.indexOf('a');//字符串中第一次出现字符a的下标
System.out.println(index);
}
}
int indexOf(int ch, int fromIndex)
从fromIndex位置开始找字符第一次出现的位置 没有返回-1
public class Test {
public static void main(String[] args) {
String str = "abacabc";
int index = str.indexOf('a',3);
System.out.println(index);
}
}
int indexOf(String str)
返回字符串第一次出现的位置,没有返回-1
public class Test {
public static void main(String[] args) {
String str = "abacabc";
int index = str.indexOf("abc");
System.out.println(index);//4
}
}
int indexOf(String str, int fromIndex)
从fromIndex位置开始找字符串第一次出现的位置,没有返回-1
public class Test {
public static void main(String[] args) {
String str = "acabacb";
int index = str.indexOf("ab",2);
System.out.println(index);
}
}
int lastIndexOf(int ch)
从后往前找,返回字符第一次出现的位置,没有返回-1
public class Test {
public static void main(String[] args) {
String str = "acabacb";
int index = str.lastIndexOf('b');
System.out.println(index);//6
}
}
int lastIndexOf(int ch, int fromIndex)
从fromIndex位置开始找,从后往前找字符第一次出现的位置,没有返
回-1
public class Test {
public static void main(String[] args) {
String str = "acabacb";
int index = str.lastIndexOf('b',5);
System.out.println(index);//3
}
}
int lastIndexOf(String str)
从后往前找,返回字符串第一次出现的位置,没有返回-1
public class Test {
public static void main(String[] args) {
String str = "acabacb";
int index = str.lastIndexOf("ac");
System.out.println(index);//4
}
}
int lastIndexOf(String str, int fromIndex)
从fromIndex位置开始找,从后往前找字符串第一次出现的位置,没有返
回-1
public class Test {
public static void main(String[] args) {
String str = "acabacb";
int index = str.lastIndexOf("ac",3);
System.out.println(index);//0
}
}
数字转字符串
public class Test {
public static void main(String[] args) {
//数字转字符串
String s1 = String.valueOf(1919);//整型
String s2 = String.valueOf(19.9);//浮点型
System.out.println(s1);//变成字符串了
System.out.println(s2);
}
字符串转数字
public class Test {
public static void main(String[] args) {
int date = Integer.parseInt("198");//整型转字符串
double date2 = Double.parseDouble("15.55");//浮点型转字符串
System.out.println(date);
System.out.println(date2);
}
}
public class Test {
public static void main(String[] args) {
String s1 = "hello";
System.out.println(s1.toUpperCase());//HELLO
String s2 = "WORLD";
System.out.println(s2.toLowerCase());//world
}
public class Test {
public static void main(String[] args) {
String s1 = "hello";
char[] array = s1.toCharArray();
System.out.println(Arrays.toString(array));//Arrays.toString( )是打印数组的
}
}
public class Test {
public static void main(String[] args) {
char[] array ={'h','e','l','l','o'};
String s1 = new String(array);
System.out.println(s1);
}
}
public class Test {
public static void main(String[] args) {
String s =String.format("%d-%d-%d",2023,11,06);
System.out.println(s);//2023-11-06
}
String replaceAll(String regex, String replacement)
public class Test {
public static void main(String[] args) {
String str = "abbaacca";
System.out.println(str.replaceAll("b","q"));//aqqaacca
System.out.println(str);//abbaacca
}
}
String replaceFirst(String regex, String replacement)
public class Test {
public static void main(String[] args) {
String str = "abbaacca";
String s1 = str.replaceFirst("b","88");
System.out.println(s1);
}
注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
public class Test {
public static void main(String[] args) {
String str = "hello beautiful world";
String [] s1 = str.split(" ");
for (int i = 0; i < s1.length; i++) {
System.out.println(s1[i]);
}
}
以空格分开,分成两组
public class Test {
public static void main(String[] args) {
String str = "hello beautiful world";
String [] s1 = str.split(" ",2);
for (int i = 0; i < s1.length; i++) {
System.out.println(s1[i]);
}
}
特殊字符作为分割符可能无法正确切分, 需要加上转义 \ \
public class Test {
// 拆分IP地址
public static void main(String[] args) {
String str = "198.153.1.2";
String [] s1 = str.split("\\.");
for (int i = 0; i < s1.length; i++) {
System.out.println(s1[i]);
}
}
如果一个字符串中有多个分隔符,可以用"|"作为连字符
public class Test {
public static void main(String[] args) {
String str2 = "wo=he=zhangsan&zai&xuexi ";
String [] array =str2.split("=|&");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
从一个完整的字符串之中截取出部分内容。
String substring(int beginIndex, int endIndex)
public class Test {
public static void main(String[] args) {
String str3 = "abcaac";
String s4 = str3.substring(0,3);
System.out.println(s4);//abc
}
}
String substring(int beginIndex)
public class Test {
public static void main(String[] args) {
String str3 = "abcaac";
String s4 = str3.substring(3);
System.out.println(s4);//aac
}
String trim()
public class Test {
public static void main(String[] args) {
String str4 = " abc aac ";
String s5 = str4.trim();
System.out.println(s5);//abc aac
}
给定一个字符串 s ,找到它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1 。
public class Test {
public int firstUniqChar(String s) {
int[] count = new int[26]; //创建一个数组
//每遍历一个字符,就把字符对应的下标++一次
for(int i = 0;i < s.length();i++){
char ch = s.charAt(i);
count[ch-'a']++;
//如果是a-a就是97-97=0,a就能在0下标位置,b-a就是97-97,b就在1下标位置,
//这个方法适用于找字符。
}
for(int i = 0;i < s.length();i++) {
//再遍历一次,如果有i的下标==1,则返回i
char ch = s.charAt(i);
if(count[ch-'a'] == 1){
return i;
}
}
return -1;
}
计算字符串最后一个单词的长度,单词以空格隔开
运用String类中多个库方法计算
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();//输入
while(in.hasNextLine()){
int index = str.lastIndexOf(' ');//从后往前找第一个空格
String s4 = str.substring(index+1);//截取空格后到末尾的字符串
System.out.println(s4.length());//求字符的长度
}
}