String的常用方法演示

- 日理万妓 2022-06-06 06:38 244阅读 0赞

package day09;

import org.junit.jupiter.api.Test;

/**
* java基础:String类
* Author:知而无涯
* Description:String类常用方法
* Date: 2017-10-31 11:04
*/
public class StringDemo04 {
//1:String类的判断功能

  1. /**
  2. * boolean equals(Object obj) 比较两个字符串的内容是否相同,区分大小写
  3. * //boolean equalsIgnoreCase(String str) 比较两个字符串,不区分大小写
  4. * //boolean contains(String str) 判断大字符串是否包含小字符串,必须连在一起哦
  5. * // boolean startsWith(String str) 判断字符串是否以某个字符开头
  6. * //boolean endsWith(String str) 判断字符串是否以某个字符结尾
  7. * // boolean isEmpty() 判断字符串为空
  8. */
  9. @Test
  10. public void Test1() {
  11. String s1 = "helloworld";
  12. String s2 = "HelloWorld";
  13. String s3 = "helloworld";
  14. long starTime = System.currentTimeMillis();
  15. System.out.println(starTime);
  16. System.out.println("equals方法:" + s1.equals(s2)); //false
  17. System.out.println("equals方法:" + s1.equals(s3)); //true
  18. System.out.println("*******************");
  19. System.out.println("equalsIgnoreCase忽视大小写:" + s1.equalsIgnoreCase(s2));//true
  20. System.out.println("*******************");
  21. System.out.println("contains大小字符串是否包含小字符串:" + s1.contains("hello"));//true
  22. System.out.println("contains大小字符串是否包含小字符串:" + s1.contains("ho"));//flase
  23. System.out.println("*******************");
  24. System.out.println("startsWith是否以某个字母开头:" + s2.startsWith("H"));//true
  25. System.out.println("startsWith是否以某个字母开头:" + s2.startsWith("Hello"));//true
  26. System.out.println("*******************");
  27. System.out.println("endsWith判断是否以某个字符结尾:" + s3.endsWith("d"));
  28. System.out.println("endsWith判断是否以某个字符结尾:" + s3.endsWith("world"));
  29. System.out.println("*******************");
  30. System.out.println("isEmpty判断字符串是否为空:" + s3.isEmpty());
  31. String s4 = "";
  32. String s5 = null;
  33. System.out.println("isEmpty判断字符串是否为空:" + s4.isEmpty());//true
  34. // System.out.println("isEmpty判断字符串是否为空:"+s5.isEmpty());//java.lang.NullPointerException
  35. long endTime = System.currentTimeMillis();
  36. System.out.println(endTime);
  37. long Time = endTime - starTime;
  38. System.out.println(Time);
  39. }
  40. //2:string类的获取功能
  41. /**
  42. * int length() //获取字符串的长度
  43. * char charAt(int index) //获取指定索引位置的字符
  44. * int indexOf(int ch) //返回指定字符在此字符串中第一次出现处的索引
  45. * 为什么是int而不是char哩,因为'a'和97都可以代表a
  46. * int indexOf(String str) //返回指定字符串在此字符串中第一次出现的索引
  47. * int indexOf(int ch,int fromIndex) //返回指定字符在此字符中从指定位置最后一次出现处的索引
  48. * int indexOf(String str,int fromIndex) //返回指定字符串在此字符串中从指定位置后第一次出现的索引
  49. * String substring(int beginIndex) //从指定位置开始截取字符串,默认到末尾
  50. * String substring(int start,int endIndex) //从指定位置开始到指定位置结束截取字符串
  51. */
  52. @Test
  53. public void Test2() {
  54. String s = "yangjishun20171031yang";
  55. //int length() 获取字符串的长度
  56. System.out.println("s.length()=" + s.length()); //22
  57. //char charAt(int index) 获取指定索引位置的字符
  58. System.out.println("s.charAt(5)=" + s.charAt(5)); //i
  59. //int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引
  60. System.out.println("s.indexOf(3)=" + s.indexOf("0")); //11
  61. // int indexOf(String str) 返回指定字符串在此字符串中第一次出现的索引
  62. System.out.println("s.indexOf(3)=" + s.indexOf("2")); //10
  63. // int indexOf(int ch,int fromIndex) 返回指定字符在此字符中从指定位置第一次出现处的索引
  64. System.out.println("s.indexOf(1,5)=" + s.indexOf('1', 5)); //12
  65. //int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现的索引
  66. System.out.println("s.indexOf(5)=" + s.indexOf("y", 5)); //18
  67. // String substring(int beginIndex) 从指定位置开始截取字符串,默认到末尾
  68. System.out.println("s.substring(5)=" + s.substring(15)); //031yang
  69. // String substring(int start,int endIndex) 从指定位置开始到指定位置结束截取字符串
  70. System.out.println("s.substring(3,5)=" + s.substring(3, 5)); //gj
  71. }
  72. //字符串的转换功能
  73. /**
  74. * byte[] getBytes() 把字符串转换为字节数组
  75. * char[] toCharArray() 把字符串转换为字符数组
  76. * static String valueOf(char[] chs) 把字符数组转换成字符串
  77. * static String valueOf(int i) 把int类型的数据转换成字符串
  78. * 注意:String类的valueOf方法可以把任意类型的数据转成字符串
  79. * String toLowerCase() 把字符串转成小写
  80. * String toUpperCase() 把字符串转成大写
  81. * String concat(String str) 拼接字符串
  82. */
  83. @Test
  84. public void Test3() {
  85. String s = "woaijava";
  86. //byte[] getBytes() 把字符串转换为字节数组
  87. byte[] bytes = s.getBytes();
  88. System.out.println(bytes);//输出的只是地址值,需要遍历
  89. System.out.println("测试1:byte[] getBytes() 把字符串转换为字节数组");
  90. System.out.print("[");
  91. for (int i = 0; i < bytes.length; i++) {
  92. // byte aByte = bytes[i];
  93. if (i == bytes.length - 1) {
  94. System.out.print(bytes[i]);
  95. } else {
  96. System.out.print(bytes[i] + ",");
  97. }
  98. }
  99. System.out.println("]"); //[119,111,97,105,106,97,118,97],输出的是ASILL码值
  100. System.out.println("测试2:char[] toCharArray() 把字符串转换为字符数组");
  101. //char[] toCharArray() 把字符串转换为字符数组
  102. System.out.print("[");
  103. char[] chars = s.toCharArray();
  104. for (int i = 0; i < chars.length; i++) {
  105. char aChar = chars[i];
  106. if (i==chars.length-1){
  107. System.out.print(aChar);
  108. }else{
  109. System.out.print(aChar+",");
  110. }
  111. }
  112. System.out.print("]"); //[w,o,a,i,j,a,v,a]
  113. System.out.println();
  114. //static String valueOf(char[] chs) 把字符数组转换成字符串
  115. String s1=s.valueOf(chars);
  116. System.out.println("测试3:static String valueOf(char[] chs)把字符数组转换成字符串");
  117. System.out.println(s1); //woaijava
  118. //static String valueOf(int i) 把int类型的数据转换成字符串
  119. int a=100;
  120. String s3=String.valueOf(a);
  121. //String toLowerCase() 把字符串转成小写
  122. String s4="ABCDEFGH";
  123. System.out.println("s4.toLowerCase()="+s4.toLowerCase()); //abcdefgh
  124. //String toUpperCase() 把字符串转成大写
  125. String s5="abcde";
  126. System.out.println("s5.toUpperCase()="+s5.toUpperCase()); //ABCDE
  127. //String concat(String str) 拼接字符串
  128. String s6="abc";
  129. String s7="def";
  130. System.out.println("s6.concat(s7)="+s6.concat(s7)); //abcdef
  131. }
  132. //字符串的替换功能
  133. /*
  134. String replace(char old,char new)
  135. String replace(String old,String new)
  136. */
  137. @Test
  138. public void Test4(){
  139. String s8="helloworld";
  140. // String replace(char old,char new)
  141. String s9=s8.replace('l','A');
  142. System.out.println("s8.replace('l','A')="+s9); //heAAoworAd
  143. //String replace(String old,String new)
  144. String s10=s8.replaceFirst("hello","java"); //javaworld
  145. System.out.println(s10);
  146. }
  147. //去除字符串两边的空格 String trim()
  148. @Test
  149. public void Test5(){
  150. String s11=" woaijava ";
  151. String s12=s11.trim();
  152. System.out.println(s11); // woaijava
  153. System.out.println(s12); //woaijava
  154. }
  155. //字符串的遍历 concat
  156. @Test
  157. public void Test6(){
  158. String s13="javapythonphp";
  159. for (int i = 0; i < s13.length(); i++) {
  160. char chars= s13.charAt(i); //通过索引遍历得到每一个字符
  161. System.out.print(chars+"\t");//j a v a p y t h o n p h p
  162. }
  163. }

}

发表评论

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

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

相关阅读