首页天道酬勤刷题(stringbuilder怎么读)

刷题(stringbuilder怎么读)

admin 02-20 23:38 132次浏览

目录一、前言二、 String类的常用方法1. int length (:返回的字符串长度2.charcharat ) intindex ) :返回某个索引处的字符3. boolean isEmpty ) :确定是否为空字符串4.sstring 将String toUpperCase () String中的所需字符转换为大写6. String trim ) )返回字符串的副本。 忽略开头和结尾空白7.booleanequals(objectobj ) :比较字符串内容是否相同8.booleanequalsignorecase (stringanotherstring ) :与equals方法类似忽略大小写9.string case 10.int compare to (stringanotherstring ):11.string substring int begin index,比较两个字符串的大小2.string substring (int beginindex,int endIndex ) )返回从begin index剪切到endIndex的新字符串。 13.boolean endswith (string suffix ):14.boolean starts with (string prefix ) :测试字符串是否以指定的前缀开头prefix ) ) int t offset (:16.boolean contains (char sequences ),用于测试以指定索引开始的字符串的子字符串是否以指定前缀开始:为字符串指定的char值true17.intindexof(stringstr ) :返回指定子字符串在此字符串中出现的第一个索引18.intindexof :从指定索引开始返回指定子字符串在此字符串中出现的第一个索引110 tr ) :返回索引20.intlastindexof ) stringstr,int fromIndex ),指定的子字符串出现在此字符串的最右边。 返回指定子字符串在此字符串末尾出现的索引。 从指定索引反向搜索21.stringreplace(charoldChar,char newChar )。 返回通过将此字符串中出现的oldchar替换为newChar而获得的新字符串。 22 .使用指定的文字替换序列替换字符串,该字符串与字符串替换(字符序列,字符序列替换)字符串匹配。 23.string replace all (string regex,String replacement ) :用指定的replacement替换与此字符串匹配的正则表达式的子字符串。 24.string replace first (string regex,String replacement ) :用指定的replacement替换与指定正则表达式匹配的字符串的第一个子字符串。 25.boolean matches (字符串注册) :通知此字符串是否与指定的正则表达式匹配。 26.string[]split(stringregex ) :根据指定的正则表达式匹配项拆分此字符串。 27.string[]split(stringregex,int limit ) :根据匹配的正则表达式拆分此字符串。 最多可达limit个,超过后剩下的都进入最后一个要素。 三、StringBuilder常用方法1.append (末尾添加:作用与String中的' '相同2.delete ) intstart, int end )3. rep per stringstr (:str4. charat ) intn ),索引中的字符5.insert,删除:start到end之间的中间字符串xxx ) :字符串offset位置字符串xxx6. length ) :返回字符串长度7. reverse ) :反转字符串4,字符串和字符数组转换1 .字符数组----字符串2 .字符串---字符数组String

一、前言

我们刷问题的时候字符串还很多,但是JAVA有三种字符串(String、StringBuilder、StringBuffer )。 那我们呢

应该如何选择用那种字符串呢?一般情况都使用String,但是如果增删改操作很多建议用StringBuilder(线程不安全,效率高)。下面我总结了常用的字符串操作,不用去背,但是每个都要清楚有什么用,能在做题的时候用到,这些方法虽然也许不能提高程序的运行效率,但是能让我们写题的效率大大提高。
更新了目录,方便大家查找,如果有需要欢迎大家收藏。

二、String类常用方法 1. int length():返回字符串的长度 String s1 = "abc";System.out.println(s1.length());//3; 2. char charAt(int index): 返回某索引处的字符 System.out.println(s1.charAt(2));//c;下标从0开始 3. boolean isEmpty():判断是否是空字符串 System.out.println(s1.isEmpty());//false; 4. String toLowerCase():将 String 中的所字符转换为小写 //注意由于String的不可变性,其实该操作并没有改变s2本身String s2 = "ABC";String s3 = s2.toLowerCase();System.out.println(s2);//ABCSystem.out.println(s3);//abc 5. String toUpperCase():将 String 中的所字符转换为大写 System.out.println(s1.toUpperCase());//ABC; 6. String trim():返回字符串的副本,忽略前导空白和尾部空白 String s4 = " abc abc ";System.out.println(s4.trim());//abc abc; 7. boolean equals(Object obj):比较字符串的内容是否相同 String s5 = new String("abc");String s6 = new String("abc");System.out.println(s5.equals(s6));//true; 8. boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写 String s7 = new String("aBc");String s8 = new String("abC");System.out.println(s7.equals(s8));//true; 9. String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+” System.out.println(s7.concat(s8));//aBcabC; 10. int compareTo(String anotherString):比较两个字符串的大小 System.out.println(s7.compareTo(s8));//-32;按字典序比较 11. String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。 String s7 = new String("0123456789");System.out.println(s7.substring(2));//23456789; 12. String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。 System.out.println(s7.substring(2, 4));//23; 13. boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束 System.out.println("456123".endsWith("123"));//true; 14. boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始 System.out.println("456123".startsWith("123"));//false; 15. boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始 System.out.println("456123".startsWith("123", 3));//true; 16. boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true System.out.println("123".contains("12"));//true; 17. int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引 System.out.println("123123123".indexOf("12"));//0;System.out.println("12378".indexOf("789"));//-1; 18. int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始 System.out.println("123123123".indexOf("12", 2));//3; 19. int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引 System.out.println("123123123".lastIndexOf("12"));//6; 20. int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索 System.out.println("123123123".lastIndexOf("12", 5));//3; 21. String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所 oldChar 得到的。 System.out.println("123123123".replace('1','3'));//323323323; 22. String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所匹配字面值目标序列的子字符串。 System.out.println("123123123".replace("12","34"));//343343343; 23. String replaceAll(String regex, String replacement):使用给定的 replacement 替换此字符串所匹配给定的正则表达式的子字符串。 24. String replaceFirst(String regex, String replacement):使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 25. boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。 26. String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。 27. String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。 三、StringBuilder常用方法 1. append():添加到末尾,和String中"+"作用一样 StringBuilder s1 = new StringBuilder("123456");s1.append("789");System.out.println(s1);//123456789; 2. delete(int start,int end):删除start到end(不包含)中间的字符串 StringBuilder s2 = new StringBuilder("123456");System.out.println(s2.delete(1, 4));//156; 3. replace(int start, int end, String str):替换start到end(不包含)中间的字符串成str StringBuilder s3 = new StringBuilder("123456789");System.out.println(s3.replace(1, 4, "000000"));//100000056789; 4. charAt(int n ): 返回某索引处的字符 StringBuilder s4 = new StringBuilder("123456");System.out.println(s4.charAt(3));//4; 5. insert(int offset, xxx):在字符串第offset位置插入字符串xxx StringBuilder s5 = new StringBuilder("123456");System.out.println(s5.insert(3, "7"));//1237456; 6. length(): 返回字符串长度 StringBuilder s6 = new StringBuilder("123456");System.out.println(s6.length());//6; 7. reverse():反转字符串 StringBuilder s1 = new StringBuilder("123456");System.out.println(s1.reverse());//654321; 四、String与字符数组转换 1. 字符数组 --> 字符串 char[] a = {'1','2','3','4','5'};//方式一:构造器String(char[])String s = new String(a);System.out.println(s);//12345;//方式二:构造器String(char[],int offset,int length)//从字符数组的下标offset开始,length个长度截取String s1 = new String(a, 3, 2);System.out.println(s1);//45; 2. 字符串 --> 字符数组 String s = "123456";//方式一:char[] toCharArray();char[] a1 = s.toCharArray();for(char c : a1)System.out.print(c + " ");//1 2 3 4 5 6//方式二:void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);//从s字符串的srcBegin位置开始(包括)到srcEnd位置(不包括)所有的字符串//复制到字符数组dst中,从字符数组的下标dstBegin开始复制。char[] a2 = new char[s.length()];s.getChars(0, s.length(), a2, 0);for(char c : a1) System.out.print(c + " ");//1 2 3 4 5 6 3. String–>StringBuilder //构造器String s = "123";StringBuilder s1 = new StringBuilder(s);System.out.println(s1);//123; 4. StringBuilder --> String //方式一:String的构造器StringBuilder s1 = new StringBuilder("123456");String s = new String(s1);System.out.println(s);//123456;//方式二:toString()方法StringBuilder s1 = new StringBuilder("123456");String s = s1.toString();System.out.println(s);//123456; 5. Integer(int) --> String int x = 100;//方式一:基本数据类型->String(最方便)String s1 = x + "";//方式二:String.valueOf(基本数据类型对象)String s2 = String.valueOf(x);//方式三:包装类.toString(对应的基本数据类型)String s3 = Integer.toString(x); 6. String --> Integer (int) //把字符串"100"转为int类型的100(必须只能是数字,否则会报错)int num = Integer.parseInt("100"); System.out.println(num);//100;
UGUI实现ScrollView无限滚动效果Java中多媒体文件上传及页面回显的操作代码利用iOS动画来模拟音量振动条的实现分布式版Redis架构 云内存 UMem Redis
arraylist定义(string array) 输入输出的功能(stringbuilder append原理)
相关内容