1.9.1 系列1
public boolean isEmpty() {}
返回字符串的长度:
public int length() {}
拼接,等价于 + :
public String concat(String str) {}
比较字符串是否相等,区分大小写:
public boolean equals(Object anObject){}
比较字符串是否相等,不区分大小写:
public boolean equalsIgnoreCase(String anotherString){}
比较字符串大小,区分大小写,按照 Unicode 编码值比较大小:
public int compareTo(String anotherString){}
比较字符串大小,不区分大小写,按照 Unicode编码值比较大小:
public int compareToIgnoreCase(String str){}
将字符串中大写字母转为小写:
public String toLowerCase(){}
将字符串中小写字母转为大写:
public String toUpperCase(){}
去掉字符串前后空白符:
public String trim(){}
示例:
package com.github.string2.demo;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "hello world";
String s = str.toUpperCase();
System.out.println("将字符串中的小写字母转换为大写字母 = " + s);
String str2 = "Hello WorLD";
String s1 = str2.toLowerCase();
System.out.println("将字符串中的大写字母转换为小写字母 = " + s1);
String str3 = " hello world ";
String s3 = str3.trim();
System.out.println("去掉字符串前后空白符 = " + s3);
}
}
1.9.2 系列2:查找
public boolean contains(CharSequence s){}
从前往后查找字符串中的子串,如果有,有返回第一次出现的下标;否则,返回 -1 :
public int indexOf(String str){}
从后往前查找字符串中的子串,如果有,有返回第一次出现的下标;否则,返回 -1 :
public int lastIndexOf(String str){}
示例:
package com.github.string2.demo2;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "hello world";
String str2 = "or";
boolean contains = str.contains(str2);
if (contains) {
System.out.println(str + "中是否包含" + str2);
}
int index = str.indexOf(str2);
System.out.println(str2 + "在" + str + "中从前往后查找的索引是:" + index);
index = str.lastIndexOf(str2);
System.out.println(str2 + "在" + str + "中从后往前的索引是:" + index);
}
}
1.9.3 系列3:字符串截取
返回一个新字符串,它是此字符串从 beginIndex 开始截取到 endIndex (不包含)的一个子字符串:
public String substring(int beginIndex){}
public String substring(int beginIndex, int endIndex) {}
示例:
package com.github.string2.demo3;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "Java is a good computer language";
System.out.println(str.substring(5)); // is a good computer language
System.out.println(str.substring(5, 11)); // is a g
}
}
1.9.4 系列4:和字符相关
public char charAt(int index){}
将此字符串转换为一个新的字符数组返回:
public char[] toCharArray(){}
返回指定数组中表示该字符序列的 String :
public String(char value[]){}
public String(char value[], int offset, int count){}
public static String copyValueOf(char data[]){}
public static String valueOf(char data[], int offset, int count){}
public static String valueOf(char data[]){}
示例:
package com.github.string2.demo4;
import java.util.Arrays;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
// 将首字母抓换为大写
String str = "hello World";
str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
System.out.println("str = " + str); // Hello World
// 将字符串中的字符按照大小顺序排列
String str2 = "helloworldjava";
char[] chars = str2.toCharArray();
Arrays.sort(chars);
System.out.println("chars = " + Arrays.toString(chars)); // [a, a, d, e, h, j, l, l, l, o, o, r, v, w]
}
}
1.9.5 系列5:编码和解码
编码,将字符串转换为字节数组,按照平台默认的字符编码进行编码:
public byte[] getBytes(){}
编码,按照指定的编码方式进行编码:
public byte[] getBytes(Charset charset){}
解码,将字节数组转换为字符串,按照平台默认的字符编码进行解码:
public String(byte bytes[]) {}
public String(byte bytes[], int offset, int length){}
解码,按照指定的编码方式进行解码:
public String(byte bytes[], Charset charset){}
public String(byte bytes[], String charsetName) throws UnsupportedEncodingException{}
示例:
package com.github.string2.demo5;
import java.nio.charset.StandardCharsets;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "我爱中国";
System.out.println(new String(str.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
}
}
1.9.6 系列6:开头和结尾
public boolean startsWith(String prefix){}
public boolean startsWith(String prefix, int toffset){}
是否以指定字符串结尾:
public boolean endsWith(String suffix){}
示例:
package com.github.string2.demo6;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "我爱中国,我喜欢Java语言,但是我的英语不咋的";
System.out.println(str.startsWith("我")); // true
System.out.println(str.startsWith("我爱")); // true
System.out.println(str.startsWith("我爱中国")); // true
System.out.println(str.endsWith("不咋的")); // true
System.out.println(str.endsWith("咋的")); // true
System.out.println(str.endsWith("的")); // true
}
}
1.9.7 系列7:正则表达式
public boolean matches(String regex){}
替换,不支持正则:
public String replace(char oldChar, char newChar){}
public String replace(CharSequence target, CharSequence replacement){}
替换第一个匹配部分:
public String replaceFirst(String regex, String replacement){}
替换所有匹配部分:
public String replaceAll(String regex, String replacement){}
示例:
package com.github.string2.demo8;
/**
* 手机号码的规则:
* 开头必须是1,长度规定11。
* 第二位 3 - 9
* 第三位到最后一位 0 - 9
*
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String iphone = "13800138000";
boolean matches = iphone.matches("^1[3-9]\\d{9}$");
System.out.println("matches = " + matches);
}
}
示例:
package com.github.string2.demo9;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "hello22world.java;234";
// 将其中的非字母替换掉
String s = str.replaceAll("[^a-zA-Z]", "");
System.out.println("s = " + s);
}
}
1.9.8 系列8:拆分
public String[] split(String regex){}
示例:
package com.github.string2.demo10;
import java.util.Arrays;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "张三.23|李四.24|王五.25";
// 按照|拆分
String regex = "\\|";
String[] split = str.split(regex);
for (String s : split) {
// 按照.进行拆分
String[] str2 = s.split("\\.");
System.out.println(Arrays.toString(str2));
}
}
}