当前位置:  首页>> 技术小册>> Java语言基础9-常用API和常见算法

1.9.1 系列1

  • 字符串是否为空:
  1. public boolean isEmpty() {}

返回字符串的长度:

  1. public int length() {}

拼接,等价于 + :

  1. public String concat(String str) {}

比较字符串是否相等,区分大小写:

  1. public boolean equals(Object anObject){}

比较字符串是否相等,不区分大小写:

  1. public boolean equalsIgnoreCase(String anotherString){}

比较字符串大小,区分大小写,按照 Unicode 编码值比较大小:

  1. public int compareTo(String anotherString){}

比较字符串大小,不区分大小写,按照 Unicode编码值比较大小:

  1. public int compareToIgnoreCase(String str){}

将字符串中大写字母转为小写:

  1. public String toLowerCase(){}

将字符串中小写字母转为大写:

  1. public String toUpperCase(){}

去掉字符串前后空白符:

  1. public String trim(){}

示例:

  1. package com.github.string2.demo;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "hello world";
  10. String s = str.toUpperCase();
  11. System.out.println("将字符串中的小写字母转换为大写字母 = " + s);
  12. String str2 = "Hello WorLD";
  13. String s1 = str2.toLowerCase();
  14. System.out.println("将字符串中的大写字母转换为小写字母 = " + s1);
  15. String str3 = " hello world ";
  16. String s3 = str3.trim();
  17. System.out.println("去掉字符串前后空白符 = " + s3);
  18. }
  19. }

1.9.2 系列2:查找

  • 字符串中是否包含子串:
    1. public boolean contains(CharSequence s){}

从前往后查找字符串中的子串,如果有,有返回第一次出现的下标;否则,返回 -1 :

  1. public int indexOf(String str){}

从后往前查找字符串中的子串,如果有,有返回第一次出现的下标;否则,返回 -1 :

  1. public int lastIndexOf(String str){}

示例:

  1. package com.github.string2.demo2;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "hello world";
  10. String str2 = "or";
  11. boolean contains = str.contains(str2);
  12. if (contains) {
  13. System.out.println(str + "中是否包含" + str2);
  14. }
  15. int index = str.indexOf(str2);
  16. System.out.println(str2 + "在" + str + "中从前往后查找的索引是:" + index);
  17. index = str.lastIndexOf(str2);
  18. System.out.println(str2 + "在" + str + "中从后往前的索引是:" + index);
  19. }
  20. }

1.9.3 系列3:字符串截取

  • 返回一个新的字符串,它是此字符串的从 beginIndex 开始截取到最后的一个子字符串:
    1. public String substring(int beginIndex){}
    返回一个新字符串,它是此字符串从 beginIndex 开始截取到 endIndex (不包含)的一个子字符串:
    1. public String substring(int beginIndex, int endIndex) {}

示例:

  1. package com.github.string2.demo3;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "Java is a good computer language";
  10. System.out.println(str.substring(5)); // is a good computer language
  11. System.out.println(str.substring(5, 11)); // is a g
  12. }
  13. }

1.9.4 系列4:和字符相关

  • 返回 index 位置的字符:
  1. public char charAt(int index){}

将此字符串转换为一个新的字符数组返回:

  1. public char[] toCharArray(){}

返回指定数组中表示该字符序列的 String :

  1. public String(char value[]){}
  2. public String(char value[], int offset, int count){}
  3. public static String copyValueOf(char data[]){}
  4. public static String valueOf(char data[], int offset, int count){}
  5. public static String valueOf(char data[]){}

示例:

  1. package com.github.string2.demo4;
  2. import java.util.Arrays;
  3. /**
  4. * @author maxiaoke.com
  5. * @version 1.0
  6. *
  7. */
  8. public class Test {
  9. public static void main(String[] args) {
  10. // 将首字母抓换为大写
  11. String str = "hello World";
  12. str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
  13. System.out.println("str = " + str); // Hello World
  14. // 将字符串中的字符按照大小顺序排列
  15. String str2 = "helloworldjava";
  16. char[] chars = str2.toCharArray();
  17. Arrays.sort(chars);
  18. System.out.println("chars = " + Arrays.toString(chars)); // [a, a, d, e, h, j, l, l, l, o, o, r, v, w]
  19. }
  20. }

1.9.5 系列5:编码和解码
编码,将字符串转换为字节数组,按照平台默认的字符编码进行编码:

  1. public byte[] getBytes(){}

编码,按照指定的编码方式进行编码:

  1. public byte[] getBytes(Charset charset){}

解码,将字节数组转换为字符串,按照平台默认的字符编码进行解码:

  1. public String(byte bytes[]) {}
  2. public String(byte bytes[], int offset, int length){}

解码,按照指定的编码方式进行解码:

  1. public String(byte bytes[], Charset charset){}
  2. public String(byte bytes[], String charsetName) throws UnsupportedEncodingException{}

示例:

  1. package com.github.string2.demo5;
  2. import java.nio.charset.StandardCharsets;
  3. /**
  4. * @author maxiaoke.com
  5. * @version 1.0
  6. *
  7. */
  8. public class Test {
  9. public static void main(String[] args) {
  10. String str = "我爱中国";
  11. System.out.println(new String(str.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
  12. }
  13. }

1.9.6 系列6:开头和结尾

  • 是否以指定字符串开头:
  1. public boolean startsWith(String prefix){}
  2. public boolean startsWith(String prefix, int toffset){}

是否以指定字符串结尾:

  1. public boolean endsWith(String suffix){}

示例:

  1. package com.github.string2.demo6;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "我爱中国,我喜欢Java语言,但是我的英语不咋的";
  10. System.out.println(str.startsWith("我")); // true
  11. System.out.println(str.startsWith("我爱")); // true
  12. System.out.println(str.startsWith("我爱中国")); // true
  13. System.out.println(str.endsWith("不咋的")); // true
  14. System.out.println(str.endsWith("咋的")); // true
  15. System.out.println(str.endsWith("的")); // true
  16. }
  17. }

1.9.7 系列7:正则表达式

  • 正则表达式:用来专门处理字符串的技术。
  • 字符类:
    • [abc] :只能是 a 或 b 或 c 。
    • [^abc] :除了 a 、b 、c 以外的任意一个字符。
    • [a-zA-Z] :必须是 a - z ,A - Z 中的任意一个字符。
    • [^a-zA-Z] :除了 a - z ,A - Z 中的任意任意一个字符。
  • 数字类:
    • [0-9] :只能是 0 和 9 之间的任意一个数字。
    • \d :等同于 [0-9] 。
    • [^0-9] :除了 0 和 9 之间的任意一个数字。
    • \D :等同于 [^0-9] 。
  • 预定于字符类:
    • . :匹配所有字符。
    • \d :等同于 [0-9] 。
    • \D :等同于 [^0-9] 。
    • \w :等同于 [a-zA-Z_0-9] 。
    • \W :等同于 [^a-zA-Z_0-9] 。
  • 边界匹配器:
    • ^ :行的开头。
    • $ :行的结尾。
  • 数量类:
    • X? :X 字符最多只能出现一次( 0 次或 1 次)。
    • X* :X 字符可以出现 0 次、1 次或 多次。
    • X+ :X 字符可以出现 1 次或多次。
    • X{n} :X 字符只能出现 n 次。
    • X{n*,} :X 字符至少出现 n 次(在数学中表示 [n,+∞) )。
    • X{n,m} :X 字符只能出现 n 到 m 次(在数学中表示 [n,m] )。
  • 字符串是否匹配指定的正则表达式:
  1. public boolean matches(String regex){}

替换,不支持正则:

  1. public String replace(char oldChar, char newChar){}
  2. public String replace(CharSequence target, CharSequence replacement){}

替换第一个匹配部分:

  1. public String replaceFirst(String regex, String replacement){}

替换所有匹配部分:

  1. public String replaceAll(String regex, String replacement){}

示例:

  1. package com.github.string2.demo8;
  2. /**
  3. * 手机号码的规则:
  4. * 开头必须是1,长度规定11
  5. * 第二位 3 - 9
  6. * 第三位到最后一位 0 - 9
  7. *
  8. * @author maxiaoke.com
  9. * @version 1.0
  10. *
  11. */
  12. public class Test {
  13. public static void main(String[] args) {
  14. String iphone = "13800138000";
  15. boolean matches = iphone.matches("^1[3-9]\\d{9}$");
  16. System.out.println("matches = " + matches);
  17. }
  18. }

示例:

  1. package com.github.string2.demo9;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "hello22world.java;234";
  10. // 将其中的非字母替换掉
  11. String s = str.replaceAll("[^a-zA-Z]", "");
  12. System.out.println("s = " + s);
  13. }
  14. }

1.9.8 系列8:拆分

  • 按照某种规则将字符串进行拆分:
  1. public String[] split(String regex){}

示例:

  1. package com.github.string2.demo10;
  2. import java.util.Arrays;
  3. /**
  4. * @author maxiaoke.com
  5. * @version 1.0
  6. *
  7. */
  8. public class Test {
  9. public static void main(String[] args) {
  10. String str = "张三.23|李四.24|王五.25";
  11. // 按照|拆分
  12. String regex = "\\|";
  13. String[] split = str.split(regex);
  14. for (String s : split) {
  15. // 按照.进行拆分
  16. String[] str2 = s.split("\\.");
  17. System.out.println(Arrays.toString(str2));
  18. }
  19. }
  20. }

该分类下的相关小册推荐: