算法训练
目录
String.valueOf(n) //数 转化为 字符串
str.trim()
char[] chars = str.toCharArray(); // 转换成 '字符' 数组
str.indexOf("h"); // 获取字串的起始下标
str.toUpperCase();
str.toLowerCase();
str.substring(2,5); // 截取字符串 String s = str.substring(2); // 表示从下标2到最后
str.replaceFirst("l", "A");
str.replaceAll("l", "A");
str.charAt(0); // 获取单个'字符'
Integer.parseInt(str);
Double.parseDouble(str2);
String[] arr = str.split(" "); // 拆分
//str.eaquals(str2)
//str.equalsIgnoreCase(str2)
ASCLL编码
// 0-9 : 48-57 || A-Z : 65-90 || a-z : 97-122
// 获得ascll编码的方法
// 方法一:
int a = 'a';// a = 97
// 方法二:
char a = 'a';
int i = (int) a;// i = 97
1、字符串
1、去掉首尾空格
//str.trim()
String str = " hello world! ";
String trim = str.trim(); //hello world!
2、返回字符数组
String str = "hello,world!";
char[] chars = str.toCharArray();
System.out.println(chars[5]);//char[1] = 'h'
3、返回单个字符的下标(从0开始)
//str.indexOf("z")
String str = "hello world!";
int h = str.indexOf("h"); //h=0
4、大小写转换
str.toUpperCase();
str.toLowerCase();
5、获取字符串的字串
//substring(start, end) // 包括start,不包括end。start从0开始
String str = "hello world!";
String substring = str.substring(2,5); //substring="llo"
6、字符串的替换
//str.replaceFirst("l", "A"); 替换第一个匹配的字符串
String str = "hello world!";
String s = str.replaceFirst("l", "A");//s="heAlo world!"
//str.replaceAll("l", "A") // 替换所有的l
String str = "hello world!";
String s = str.replaceAll("l", "A");//s="heAAo world!"
7、获取某个下标位置的字符
//str.charAt(0);
String str = "hello world!";
char c = str.charAt(0);// 注意单个字符是 char 类型
8、把字符串转为整数
//Integer.parseInt(str);
String str = "123";
int i = Integer.parseInt(str);
//如果直接写一个数,例如12 这个整数是int类型
//如果直接写一个小数,例如12.2 这个小数默认是double类型
//Double.parseDouble(str2);
String str2 = "123.2";
double v = Double.parseDouble(str2);
9、比较是否相等
//str.eaquals(str2) // hello 与 HELLO 不同
//str.equalsIgnoreCase(str2) // hello 与 Hello 相同
String str = "hello";
String str2 = "HELLO";
if (str.equals(str2)){
System.out.println("true");
}else {
System.out.println("false");
}
10、拆分
//String strArray[] = str.split(正则表达式); //拆分的结果保存到字符串数组中
String str = "hello world!";
String[] arr = str.split(" ");// arr = ["hello","world!"]
11、合并字符串
//或者用 str + str2
String str = "hello";
String str2 = "HELLO";
String concat = str.concat(str2);// concat = "helloHELLO"