public static void main(String[] args) {
String str = "abcdef";
char ch = str.charAt(2);
System.out.println(ch);//输出:c
}
compareTo
compareToIgnoreCase
public static void main(String[] args) {
String str1 = "abcdef";
int ret1 = str1.compareTo("abcee");
System.out.println(ret1);//输出-1
int ret2 = str1.compareToIgnoreCase("AbcDe");
System.out.println(ret2);//输出1
}
关于返回值:
- 返回0表示两个字符串的值相同。
- 如果不相同,返回两个字符串 的 第一位不相同字符 的 ASCII之差。
- 如果是包含关系的话,返回长度之差。
public static void main(String[] args) {
String str = "abcdef";
boolean bool1 = str.contains("abc");
boolean bool2 = str.contains("aaa");
System.out.println(bool1);//输出true
System.out.println(bool2);//输出false
}
public static void main(String[] args) {
String str = "abcdef";
boolean bool1 = str.equals("abcdef");
boolean bool2 = str.equals("aaa");
System.out.println(bool1);//输出true
System.out.println(bool2);//输出false
String str2 = "abcdef";
boolean bool3 = str.equalsIgnoreCase("Abcdef");
System.out.println(bool3);//输出true
}
注意:比较两个字符串内容是否相等使用“==”是在比较引用对象存储的地址。
indexOf
public static void main(String[] args) {
String str = "abcdef";
int ret = str.indexOf("cdef");
System.out.println(ret);//输出2
int ret2 = str.lastIndexOf("cde");
System.out.println(ret2);//输出2
}
public static void main(String[] args) {
String str1 = "abcdef";
boolean bool1 = str1.isEmpty();
String str2 = "";
boolean bool2 = str2.isEmpty();
System.out.println(bool1);//输出false
System.out.println(bool2);//输出true
}
public static void main(String[] args) {
String str = "abcdef";
System.out.println(str.length());//输出6
}
注意与属性的length区分,求字符串长度要加(),数组则不用!
replace
replaceFrist
public static void main(String[] args) {
String str1 = "Hi do you like learning English?";
String str2 = str1.replace(' ','/');
System.out.println(str2);//输出Hi/do/you/like/learning/English?
String str3 = str1.replaceFirst(" ",",");
System.out.println(str3);//输出Hi,do you like learning English?
String str4 = str1.replaceAll(" ","//");//替换字符串,类似replace替换字符!
System.out.println(str4);//输出Hi//do//you//like//learning//English?
}
public static void main(String[] args) {
String str1 = "Hi do you like learning English?";
String[] str2 = str1.split(" ");
for (String x:str2) {
System.out.println(x);/*输出 Hi
do
you
like
learning
English?*/
}
}
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = str1.substring(1,3);
System.out.println(str2);//输出bc
}
public static void main(String[] args) {
String str1 = "abcdef";
char[] str2 = str1.toCharArray();
for (char ch:str2) {
System.out.print(ch+" ");//输出a b c d e f
}
}
toUpperCase
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = str1.toUpperCase();
System.out.println(str2);//输出ABCDEF
String str3 = str2.toLowerCase();
System.out.println(str3);//输出abcdef
}
public static void main(String[] args) {
String str1 = " Hi do you like learning English? ";
String str2 = str1.trim();
System.out.println(str1);//输出 Hi do you like learning English?
System.out.println(str2);//输出Hi do you like learning English?
}
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14);
System.out.println(s);//输出2019-9-14
}
class Student {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() { //重写toStrong便于我们打印
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public class text2 {
public static void main(String[] args) {
Student student = new Student(10,"zhangsan");
String str = String.valueOf(student);
System.out.println(str);//输出Student{id=10, name='zhangsan'}
}
}
使用StringBuilder需要导入包:import java.lang.StringBuilder;
public static void main(String[] args) {
StringBuilder strbu1 = new StringBuilder("abcdef");
strbu1.reverse();
System.out.println(strbu1);//输出fedcba
}
public static void main(String[] args) {
StringBuilder strbu1 = new StringBuilder("abcdef");
strbu1.append('g');
System.out.println(strbu1);//输出abcdefg
}