String常用方法使用
String str = "ABCDEFGHIJKLMN";
for(int i = 0;i < str.length();i++) {
char ch = str.charAt(i);
String hex = Integer.toHexString(ch);
System.out.println("0X"+hex);
}
System.out.println("hello".compareTo("hOrld"));
System.out.println("hello".compareToIgnoreCase("hOrld"));
System.out.println("hello".concat("world"));
System.out.println("helloworld".contains("wor"));
String path = "http://www.softeem.com/index/imgs/logo.png";
System.out.println(path.endsWith(".png"));
System.out.println(path.startsWith("http"));
System.out.println(path.indexOf("www"));
System.out.println("".isEmpty());
System.out.println(String.join("-", "hello","world","softeem"));
String常用方法练习
public class TestString {
public void replace() {
String commons = "听说单身狗凤姐去了米国,找到了心仪男票,三天两头在微博上抨击单身狗。。。。";
String[] words = { "凤姐", "米国", "单身狗" };
for (String k : words) {
commons = commons.replace(k, placeholder(k));
}
System.out.println("替换后:" + commons);
}
public String placeholder(String s) {
String p = "";
for (int i = 0; i < s.length(); i++) {
p += "*";
}
return p;
}
public String handler(String phoneNum) {
return phoneNum.substring(0,3)+"****"+phoneNum.substring(8);
}
public static void main(String[] args) {
String url = "http://www.softeem.com/query/list.html";
int i = url.lastIndexOf("/");
System.out.println(i);
System.out.println(url.substring(i + 1));
System.out.println("13567898765".matches("1[3578]\\d{9}"));
System.out.println("无极剑圣剑客".replace('剑', '刀'));
String commons = "听说单身狗凤姐去了米国,找到了心仪男票,三天两头在微博上抨击单身狗。。。。";
System.out.println(commons.replace("单身狗", "***"));
new TestString().replace();
System.out.println("张三13456543212男".replaceAll("1[3578]\\d{9}", "*"));
String phone = new TestString().handler("13245674321");
System.out.println(phone);
String str = "10/张三/男/计算机科学与技术/68.8";
String[] info = str.split("/");
Student stu = new Student();
stu.setSno(Integer.parseInt(info[0]));
stu.setSname(info[1]);
stu.setSex(info[2]);
stu.setMajor(info[3]);
stu.setScore(Double.parseDouble(info[4]));
System.out.println(stu);
String file = "美女.png";
System.out.println(file.split("\\.")[0]);
System.out.println("helloworld".toUpperCase());
System.out.println("HelloWorld".toLowerCase());
System.out.println(" hello world 123 ".trim());
String name = " smith scott allen james kobe ";
String[] names = name.trim().split(" ");
for (String n : names) {
n = n.substring(0,1).toUpperCase()+n.substring(1).toLowerCase();
System.out.println(n);
}
System.out.println(String.valueOf(100));
}
}