1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 黑马程序员——String类总结

黑马程序员——String类总结

时间:2020-09-11 13:42:12

相关推荐

黑马程序员——String类总结

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- String类是什么? String类是操作字符串的类,它不是常用的基本类型,它是一个类 -------------------------构造函数----------------------- String() String(String original) String(char[] value) String(char[] vlaue, int offset, int count) String(int[] codePoints, int offset, int count) String(StringBuffer buffer) String(StringBuilder builder) ------------------------常用方法--------------------------- public int length(): 返回count,字符个数。 public boolean isEmpty(): 返回bolean值。 public char charAt(int index): 返回index下标位置的字符。 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin): 复制字符串(从srcBegin到srcEnd-1)到指定字符数组(dst)中。从det中下标detBegin开始。 public byte[] getBytes(String charsetName): 转换为byte数组 public boolean equals(Object anObject): 比较值。 public boolean contentEquals(StringBuffer sb): 与stringBuffer比较。 public boolean equalsIgnoreCase(String anotherString): 忽略大小写进行比较 public int compareTo(String anotherString): 比较字符串大小,相等返回0, 大于返回大于0的数,小于返回小于0的数。 public int compare(String s1, String s2): 比较两个字符串大小。 public boolean regionMatches(int toffset, String other, int ooffset, int len): 比较两个字符串从offset处的len个字符是否相等。 public boolean startsWith(String prefix, int toffset): 测试字符串是否已指定前缀开始 public boolean endsWith(String suffix): 测试字符串是否已制定后缀结尾。 public int indexOf(int ch): 返回指定字符在此字符串中的位置。 public String substring(int beginIndex, int endIndex): 返回子串 public String concat(String str): 连接两个字符串 public String replace(char oldChar, char newChar): 替换字符 public boolean matches(String regex): 是否匹配给定正则表达式 public boolean contains(CharSequence s): 当且仅当此字符串包含指定的 char 值序列时,返回 true。 public String[] split(String regex, int limit): 根据匹配给定的正则表达式来拆分此字符串。 public String toLowerCase(): 全部转换为小写 public String toUpperCase(): 全部转换为大写 public String trim(): 返回字符串的副本,忽略前导空白和尾部空白。 public String toString(): 转换为字符串 public char[] toCharArray(): 转换为数组

1 练习 2 package blogtest4; 3 /* 4 * 演示String类的常用功能 5 */ 6 7 public class StringTest1 8 { 9 public static void main(String[] args)10 {11String str = new String("abcdefdg");12 13show(str.length());14show(str.charAt(3));15show(str.indexOf('d'));16show(str.indexOf('d',4));17show(str.indexOf("cd"));18show(str.indexOf("fd",3));19show(str.lastIndexOf("d"));20show(str.lastIndexOf('d',7));21show(str.lastIndexOf("fd"));22show(str.lastIndexOf("de",6));23 24 25String str1 = "shanshanlisl.java";26show(str1.startsWith("shanshan"));27show(str1.endsWith(".java"));28show(str1.contains("woaini"));29show(str1.isEmpty());30show(str1.equals("shanshn"));31show(str1.equalsIgnoreCase("shanshn"));32 33 34char[] arr = new char[]{'a','b','c'};35String str3 = new String(arr,1,2);36show(str3);37 38char[] arr2 = new char[]{'a','b','c'};39String str4 = new String();40 41show(str4.copyValueOf(arr));42show(str4.copyValueOf(arr,1,2));43show(String.valueOf(3));44 45 46byte[] arr3 = new byte[]{'A',44,55,-34};47String str5 = new String(arr);48show(str5);49 50 51String arr4 = new String("abcdef");52byte[] ch = arr4.getBytes();53for(int i = 0; i < ch.length; i++)54{55show(ch[i]);56}57 58String arr6 = new String("abcbdef");59show(arr6.replace('b','g'));60show(arr6.replace("bc","gf"));61String[] acc = arr6.split("b");62for(int i = 0; i < acc.length; i++)63{64show(acc[i]);65}66 67String arr7 = new String("abcbdef");68show(arr7.substring(2));69show(arr7.substring(2,5));70 71String arr8 = new String("abDbHef");72show(arr8.toLowerCase());73show(arr8.toUpperCase());74String acc2 = new String (" abc dd ff ");75show(acc2.trim());76 }77 public static void show(Object obj)78 {79System.out.println(obj);80 }81 }82

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。