String类常用方法总结

一时失言乱红尘 2022-07-28 04:16 314阅读 0赞
  1. /**
  2. * 关于String类的常用方法总结
  3. * @author lsq
  4. *
  5. */
  6. public class StringAPIDemo {
  7. public static void main(String[] args) {
  8. /*
  9. * public char[] toCharArray()将一个字符串变成字符数组
  10. * public byte[] getBytes()将一个字符串变成字节数组
  11. */
  12. String str = "Hello";
  13. char[] charArray = str.toCharArray();
  14. byte[] bytes = str.getBytes();
  15. //循环输出字符数组
  16. for (int i = 0; i < charArray.length; i++) {
  17. System.out.print(charArray[i]+",");
  18. }
  19. //循环输出字节数组
  20. for (int i = 0; i < bytes.length; i++) {
  21. System.out.print(bytes[i]+",");
  22. }
  23. System.out.println("");
  24. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  25. /*
  26. * public String(char[] value)将字符数组变成一个字符串
  27. * public String(char[] value, int offset, int count)将制定范围内的字符变为字符串,offset是起始点,count是长度
  28. * public String(byte[] bytes)将全部的字节数组变成字符串
  29. * public String(byte[] bytes, int offset, int length)将制定范围内的字节数组变成字符串,offset是起始点,count是长度
  30. */
  31. char charArray2[] = {'H','E','L','L','O',' ','J','A','V','A'};
  32. byte bytes2[] = {'1','2','3','4'};
  33. String char2str1 = new String(charArray2);
  34. String char2str2 = new String(charArray2, 3, 2);
  35. String byte2str1 = new String(bytes2);
  36. String byte2str2 = new String(bytes2, 1, 2);
  37. System.out.println("char2str1的值是:"+char2str1);
  38. System.out.println("char2str2的值是:"+char2str2);
  39. System.out.println("byte2str1的值是:"+byte2str1);
  40. System.out.println("byte2str2的值是:"+byte2str2);
  41. System.out.println("");
  42. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  43. /*
  44. * public char charAt(int index)从一个字符串中取出指定位置的字符
  45. */
  46. String charAtTest = "charAtTest";
  47. char c = charAtTest.charAt(4);
  48. System.out.println("charAtTest位置为4的字符是:"+c);
  49. System.out.println("");
  50. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  51. /*
  52. * public int length()获取字符串的长度
  53. */
  54. String strLength = "strLength";
  55. int length = strLength.length();
  56. System.out.println(length);
  57. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  58. /*
  59. * 查找指定的字符串是否存在:
  60. * public int indexOf(String str)从头开始查找字符串是否存在,返回int类型,若查找不到,返回-1
  61. * public int indexOf(String str, int fromIndex)从指定位置开始查找指定的字符串是否存在,返回int类型,找不到返回-1
  62. */
  63. String strindexOf = "abcdefgcdhi";
  64. System.out.println(strindexOf.indexOf("cd"));
  65. System.out.println(strindexOf.indexOf("c",3));
  66. System.out.println(strindexOf.indexOf("j"));
  67. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  68. /*
  69. * public String trim()去掉字符串中左右的空格,但中间的空格不会去掉
  70. */
  71. String strTrim = " Hello java ";
  72. System.out.println(strTrim.trim());
  73. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  74. /*
  75. * 字符串截取:从一个字符串中取出部分内容
  76. * substring(int beginIndex)从指定位置开始截取至字符串结尾、
  77. * subString(int beginIndex, int endIndex)截取指定范围的字符串,不包括结束位置的字符(☆)
  78. */
  79. String strsubString = "i love java!";
  80. System.out.println(strsubString.substring(2));
  81. System.out.println(strsubString.substring(2,8));
  82. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  83. /*
  84. * public String[] split(String regex)拆分字符串,以某一个字符串作为拆分点
  85. */
  86. String strsplit = "Hello World! How are you?";
  87. String[] splits = strsplit.split(" ");
  88. for (int i = 0; i < splits.length; i++) {
  89. System.out.println(splits[i]);
  90. }
  91. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  92. /*
  93. * public String toLowerCase():将字符串字符都变为小写
  94. * public String toUpperCase():将字符串字符都变为大写
  95. */
  96. String strToCase = "Hello java!";
  97. System.out.println(strToCase.toLowerCase());
  98. System.out.println(strToCase.toUpperCase());
  99. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  100. /*
  101. * 判断是否以指定的字符串开头或者结尾
  102. * public boolean startsWith(String prefix):判断是否以指定的字符串开头
  103. * public boolean endsWith(String suffix):判断是否以指定的字符串结尾
  104. */
  105. String strWith = "Hello World!";
  106. boolean s = strWith.startsWith("Hel");
  107. System.out.println(s);
  108. boolean e = strWith.endsWith("d!");
  109. System.out.println(e);
  110. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  111. /*
  112. * 判断两个字符串是否相等:
  113. * public boolean equals(Object anObject):区分大小写
  114. * public boolean equalsIgnoreCase(String anotherString):不区分大小写
  115. */
  116. String strEquals1 = "String";
  117. String strEquals2 = "string";
  118. System.out.println(strEquals1.equals(strEquals2));
  119. System.out.println(strEquals1.equalsIgnoreCase(strEquals2));
  120. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  121. /*
  122. * 字符串替换功能
  123. * public String replace(CharSequence target, CharSequence replacement)
  124. * public String replaceAll(String regex, String replacement):将指定的字符regex替换成指定的replacement
  125. */
  126. String strReplace = "Hello java!";
  127. System.out.println(strReplace.replace('o', 'l'));
  128. System.out.println(strReplace.replaceAll("java", "World"));
  129. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  130. }
  131. }

发表评论

表情:
评论列表 (有 0 条评论,314人围观)

还没有评论,来说两句吧...

相关阅读