String类常用方法
/**
* 功能描述
*
* @author yunweixiaocai
* @since 2021-04-20
*/
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.Arrays;
import org.w3c.dom.ls.LSOutput;
class JavaStudy {
static String str = " I am a good 男孩 ";
static String str2 = "I am a good boy";
public static void main(String[] args) {
System.out.println("*****************常用方法*******************");
System.out.println(str.length());
System.out.println(str.charAt(10));
System.out.println(str.isEmpty());
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
System.out.println(str.trim());
System.out.println(str.trim().equals(str2));
System.out.println(str.concat(str2));
System.out.println(str.compareTo("I"));
System.out.println(str.substring(10));
System.out.println(str.substring(10,14));
System.out.println("*****************判断*******************");
System.out.println(str.endsWith("boy "));
System.out.println(str.startsWith(" I"));
System.out.println(str.startsWith("am",5));
System.out.println(str.contains("good"));
System.out.println(str.indexOf(str2));
System.out.println(str.indexOf('o'));
System.out.println(str.indexOf('o', 2));
System.out.println(str.lastIndexOf('o'));
System.out.println("******************替换与切片******************");
System.out.println(str.replace("good", "bad"));
System.out.println(str.replace('o', 'e'));
System.out.println(str.replaceAll("a.", "bb"));
System.out.println(str.replaceFirst("a.", "bb"));
String[] strs = str.split(" ");
for (String s : strs) {
System.out.println(s);
}
System.out.println("******************类型转换******************");
System.out.println(Integer.parseInt("1234"));
System.out.println(Boolean.parseBoolean("true"));
System.out.println(String.valueOf(2134));
System.out.println(String.valueOf(false).getClass());
System.out.println(String.valueOf(new char[]{'g', 'o', 'o', 'd'}));
System.out.println(new String(new char[]{'g', 'o', 'o', 'd'}));
char[] chars = "good".toCharArray();
for (char c : chars){
System.out.println(c);
}
try {
System.out.println(Arrays.toString(str.getBytes("gbk")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] bytes = new byte[]{32, 32, 32, 73, 32, 97, 109, 32, 97, 32, 103, 111, 111, 100, 32, 98, 111, 121, 32};
System.out.println(new String(bytes));
}
}
输出:
*****************常用方法*******************
19
g
false
i am a good boy
I AM A GOOD BOY
I am a good boy
true
I am a good boy I am a good boy
-41
good boy
good
*****************判断*******************
true
true
true
true
3
11
11
16
******************替换与切片******************
I am a bad boy
I am a geed bey
I bb bbgood boy
I bb a good boy
I
am
a
good
boy
******************转换******************
1234
true
2134
class java.lang.String
good
good
g
o
o
d
[32, 32, 32, 73, 32, 97, 109, 32, 97, 32, 103, 111, 111, 100, 32, 98, 111, 121, 32]
I am a good boy
StringBuffer/StringBuilder通用方法
class JavaStudy {
static StringBuffer stringBuffer = new StringBuffer("I am a good boy");
static String str2 = ", are you?";
public static void main(String[] args) {
System.out.println("*****************常用方法*********************");
stringBuffer.append(str2);
System.out.println(stringBuffer);
stringBuffer.delete(7,12);
System.out.println(stringBuffer);
stringBuffer.replace(7,10,"girl");
System.out.println(stringBuffer);
stringBuffer.insert(0, "Hey, ");
System.out.println(stringBuffer);
stringBuffer.reverse();
System.out.println(stringBuffer);
stringBuffer.reverse();
stringBuffer.setCharAt(3,'!');
System.out.println(stringBuffer);
System.out.println("*****************方法链*********************");
StringBuilder sb = new StringBuilder("I am a good boy");
sb.append(str2).delete(7,12).replace(7,10,"girl").insert(0, "Hey, ").reverse().reverse().setCharAt(3,'!');
System.out.println(sb);
System.out.println("*****************无修改操作*********************");
System.out.println(stringBuffer.substring(0, 11));
System.out.println(stringBuffer.length());
System.out.println(stringBuffer.charAt(3));
}
}
输出:
*****************常用方法*********************
I am a good boy, are you?
I am a boy, are you?
I am a girl, are you?
Hey, I am a girl, are you?
?uoy era ,lrig a ma I ,yeH
Hey! I am a girl, are you?
*****************方法链*********************
Hey! I am a girl, are you?
*****************无修改操作*********************
Hey! I am a
26
!