转换功能

  1. @Test
  2. public void ceui() {
  3. String a = "helloworld";
  4. //字符串转数组(使用for循环获取)
  5. char[] chars = "helloworld".toCharArray();
  6. //字符串转字节数组 (使用for循环获取值)
  7. byte[] bytes = a.getBytes();
  8. /*数字转String
  9. * 另外 String.valueOf() 可以将任何数据类型转换成字符串
  10. * */
  11. String s = String.valueOf(132456);
  12. // 将大写转换成小写的字符串
  13. String s1 = "AAAAA".toLowerCase(); //aaaaa
  14. String s2 = "aaaaa".toUpperCase(); //AAAAA
  15. }

分割功能String

  1. public static void main(String[] args) {
  2. /*
  3. * split(String sign)
  4. * 方法可以使用字符串按指定的分割字符或字符串对内容进行分割,
  5. * 并将分割后的结果存放在字符串数组中,有两种分割形式
  6. * str.split(String sign)
  7. */
  8. String[] split = "AAA-aaa".split("-");
  9. /*for (String s : split) {
  10. System.out.println(s);
  11. }*/
  12. /**
  13. * 数组结果:
  14. * AAA
  15. * aaa
  16. */
  17. /*第二个参数是 限制分割的次数*/
  18. String[] bb = "AAA-BBB-CCC-DDD".split("-", 2);
  19. for (String s : bb) {
  20. System.out.println(s);
  21. }
  22. /**
  23. * 数组结果:
  24. * AAA
  25. * BBB-CCC-DDD
  26. */
  27. }

判断功能String

  1. /**
  2. * 各种判断功能
  3. * 和判断非空功能
  4. */
  5. public class 判断功能String {
  6. /**
  7. * 判空的五种方法
  8. */
  9. @Test
  10. public void ceui() {
  11. /*效率最高*/
  12. String usernam = "11111111";
  13. if (usernam == null || usernam.length() <= 0) {
  14. System.out.println("是空的");
  15. } else {
  16. System.out.println("是非空的");
  17. }
  18. //方法一:开发中最常用的方法,看起来高大上
  19. // 使用需要导包:
  20. // if(StringUtils.isNotBlank(str))//判断字符串不为空
  21. // if(StringUtils.isBlank(str))//判断字符串为空
  22. // 方法二:比较简单直接的方法
  23. // if(s == null ||"".equals(s));
  24. // 方法四 效率和方法三几乎相等,但是出于兼容性考虑,不建议用这个方法
  25. // if(s == null || s.isEmpty());
  26. // 方法五 这是一种比较直观,简便的方法,而且效率也非常高,与方法三,方法四效率差不多
  27. // if (s == null || s == "");
  28. }
  29. /*
  30. * 判断功能
  31. * boolean equals(Object anObject) 比较字符串内容是否相同(区分大小写)
  32. * boolean equalsIgnoreCase(String anotherString) 比较字符串内容是否相同(忽略大小写)
  33. * boolean contains(String s) 判断大字符串是否包含小字符串
  34. * boolean startsWith(String prefix) 判断字符串是否以某个字符串开头
  35. * boolean endsWith(String prefix) 判断字符串是否以某个字符串结尾
  36. * boolean isEmpty() 判断字符串是否为空
  37. *
  38. * */
  39. public static void main(String[] args) {
  40. String s1 = "helloworld";
  41. String s2 = "HelloWorld";
  42. String s3 = "hello";
  43. String s4 = "world";
  44. String s5 = "";
  45. //String s6=null;
  46. System.out.println(s1 == s2); //false
  47. System.out.println(s1.equals(s2)); //false
  48. System.out.println(s1.equalsIgnoreCase(s2)); //true
  49. System.out.println(s1.contains(s3)); //true
  50. System.out.println(s2.startsWith(s3)); //false
  51. System.out.println(s2.endsWith(s4)); //false
  52. System.out.println(s4.isEmpty());//false
  53. System.out.println(s5.isEmpty()); //true
  54. //System.out.println(s6.isEmpty());//空指针异常
  55. }

字节数组转String

  1. /*
  2. * String(byte[] bytes) ,把字节数组转成字符串
  3. * String(byte[] bytes, int offset, int length) ,截取字节数组的一部分转换为字符串
  4. * String(char[] value)
  5. * String(char[] value, int offset, int count)
  6. * String(int[] codePoints, int offset, int count)
  7. * String(String original),字符串常量值转换为字符串
  8. */
  9. public static void main(String[] args) {
  10. //从byte型数组中取得的值为阿斯克码表对应的值
  11. byte[] bytes = new byte[]{97, 98, 99, 100, 101};
  12. String str = new String(bytes);
  13. System.out.println("str:" + str);
  14. //我想得到字符串bcd;
  15. String str1 = new String(bytes, 1, 3);//从索引1开始,取4位
  16. System.out.println(str1);
  17. System.out.println("=================================");
  18. char[] ch = {'a', 'b', 'c', 'd'};
  19. String str2 = new String(ch);
  20. System.out.println(str2);
  21. //我想得到cd
  22. String str3 = new String(ch, 2, 2);
  23. System.out.println(str3);
  24. System.out.println("==============================");
  25. //从int型数组中取得的值为阿斯克码表对应的值
  26. int[] arr = {97, 98, 99, 100, 101, 102};
  27. String str4 = new String(arr, 3, 2);
  28. System.out.println(str4);
  29. }

替换功能

https://blog.csdn.net/qq_32967665/article/details/86515587

  • replace是替换旧字符串为新的字符,当然使用replaceAll 也可以达到这个效果
  • replaceAll 是支持正则的,第一个参数是正则表达式
  • replaceFirst 和 replaceAll 一样都是第一个参数是支持正则的,
  • 和replaceAll区别是 replaceFirst只是匹配第一个
  1. String str1 = "Hello world ";
  2. String str2 = "abcdefghijklmnabcdefg";
  3. /*将指定的字符或者字符串替换成新的字符或字符串
  4. * 如果要替换的字符串oldChar在字符串中重复出现多次,replace()方法将所有的
  5. * oldChar全部替换成newChar
  6. * 需要注意: 区分大小写
  7. * */
  8. System.out.println(str1.replace("world", "yangk")); //Hello yangk
  9. //区分大小写
  10. System.out.println("aaabcdeaAAsdaabcde".replace("aaa", "bbb")); //bbbbcdeaAAsdaabcde
  11. System.out.println("aaabcdeaaasdaabcde".replace("aaa", "bbb")); //bbbbcdebbbsdaabcde

获取功能String

  1. public static void main(String[] args) {
  2. /*
  3. * String获取功能
  4. * int length();获取String字符串的长度
  5. * char charAt(int index); 返回指定索引处的 char 值。
  6. * int indexOf(int ch) ;返回指定字符在此字符串中第一次出现处的索引。
  7. * int indexOf(String str) ; 返回指定子字符串在此字符串中第一次出现处的索引。
  8. * indexOf(String str, int fromIndex) ;返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
  9. * indexOf(int ch, int fromIndex) ; 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
  10. * String substring(int beginIndex) ;从指定位置截取字符串(从0开始计算索引的)
  11. * substring(int beginIndex, int endIndex) ;从指定索引位置到指定索引位置,截取字符串
  12. * */
  13. String str = "helloWorld";
  14. // System.out.println(str.length()); //10
  15. // System.out.println(str.charAt(1)); //e
  16. // System.out.println(str.indexOf(101)); // 1
  17. // System.out.println(str.indexOf("llo")); // 2
  18. // System.out.println(str.lastIndexOf("l")); //9
  19. // System.out.println("helloWorld".substring(2)); // lloWorld
  20. System.out.println("helloWorld".substring(2, 5)); // llo
  21. }