TypechoJoeTheme

Misty rain的博客

统计

java入门篇--字符串操作(下)

2019-08-01
/
76 评论
/
15,260 阅读
/
正在检测是否收录...
08/01

StringBuffer的使用--
StringBuffer相对于String操作更为复杂一点,但是其中的好处是不言而喻的,首先他的运行效率就比String快很多(节约了内存),其次他是线程安全的。

创建StringBuffer-

首先StringBuffer做为一个对象而言当然是new出来的,举例:

    StringBuffer sb1=new StringBuffer("111");
    System.out.println(sb1.toString());

创建sb1对象初始值为字符串111 ,如果创建一个初始值为32个字符的StringBuffer对象呢,那么StringBuffer(32)即可,初始容量为16个字符。
追加字符串-

String用+号就可以将字符串连接到一起,而StringBuffer则不同,使用append追加。

    StringBuffer sb1=new StringBuffer(32);
    
    sb1.append("你好").append("陌生人").toString();
    System.out.println(sb1);

修改字符串-

public void setCharAt(int index, char ch)

公共的setCharAt方法,参数index则为该字符串的下标位置,ch则为替换的字符。

插入字符串-

    sb1.append("你好").append("陌生人").toString();
    System.out.println(sb1);
    
    sb1.insert(2, ',');
    System.out.println(sb1);

在中间加上一个,号。
反序-

    sb1.append("人生陌").append("好你").toString();
    
    sb1.insert(3, ',');
    System.out.println(sb1.reverse());

将所有字符反序排列输出。
移除某一子字符串-

    sb1.append("你好").append("陌生人").toString();
    
    sb1.insert(2, ',');
    
    
    sb1.delete(2, 3);
    System.out.println(sb1);

delete(str,,end) 从某一下标开始再到某一下标结束。
StringBuilder-

于StringBuffer的使用方法一样,比StringBuffer运行效率更高,但不保障线程安全。

相同点于不同点-

字符串分割--

    StringBuffer sb1=new StringBuffer("1,2,3,4,5");
    String[] str=sb1.toString().split(",");

首先sb1.tostring化成String对象再split分割,返回值为String数组,按,分割,此时数组的长度为5。
一般情况下我们根据数据具体使用的情况下对字符串进行分割,只取我们想要的,str[2] 我们只要3,数组的下标也是从0-开始。

字符串大小写--

    StringBuffer sb1=new StringBuffer("abcde");
    System.out.println(sb1.toString().toUpperCase());
    System.out.println(sb1.toString().toLowerCase());

Upper将小写转换成大写,Lower将大写转换为小写。
字符串中的包含和替换--

    String str="abcdec";
    if(str.contains("c")){
        System.out.println("包含");
    }else{
        System.out.println(str);
    }
    
    if(str.replace("c", "f").contains("c")){
        System.out.println("包含");
    }else{
        System.out.println(str.replace("c", "f"));
    }

如图,如果contains则输出包含,不包含则输出原有值。
判断文件格式--

    String str="123.mp3";
    boolean b=str.endsWith(".mp4");
    if(b){
        System.out.println("Yes");
    }else{
        System.out.println("No");
    }

判断str是不是mp4格式的,使用endsWith方法。

    String str="123.mp3";
    boolean b=str.endsWith(".mp3");
    boolean b1=str.startsWith("123");
    boolean b2=str.startsWith("12");
    boolean b3=str.startsWith("1");
    boolean b4=str.startsWith("21");
    System.out.println(b1+"--"+b2+"--"+b3+"--"+b4);

如图,我们看到b1 b2 b3都是符合条件的, 而最后一个21是为false的。

java
朗读
赞(1)
版权属于:

Misty rain的博客

本文链接:

http://101.42.223.25/index.php/archives/10/(转载时请注明本文出处及文章链接)

评论 (76)
  1. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  2. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  3. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  4. oayarq7U 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  5. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  6. ../../../../../../../../../../../../../../etc/passwd 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  7. 1 waitfor delay '0:0:15' -- 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  8. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  9. 1 作者
    Windows 10 · Google Chrome

    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

    2022-04-28 回复
  10. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  11. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  12. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  13. 1 作者
    Windows 10 · Google Chrome

    response.write(9213955*9017682)

    2022-04-28 回复
  14. 1 作者
    Windows 10 · Google Chrome

    '+response.write(9213955*9017682)+'

    2022-04-28 回复
  15. 1 作者
    Windows 10 · Google Chrome

    "+response.write(9213955*9017682)+"

    2022-04-28 回复
  16. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  17. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  18. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  19. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  20. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  21. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  22. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  23. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  24. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  25. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  26. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  27. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  28. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  29. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  30. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  31. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  32. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  33. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  34. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  35. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  36. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  37. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  38. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  39. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  40. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  41. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  42. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  43. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  44. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  45. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  46. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  47. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  48. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  49. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  50. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  51. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  52. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  53. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  54. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  55. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  56. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  57. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  58. -1 OR 2+581-581-1=0+0+0+1 -- 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  59. -1 OR 2+485-485-1=0+0+0+1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  60. -1' OR 2+769-769-1=0+0+0+1 -- 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  61. -1' OR 2+775-775-1=0+0+0+1 or 'LHdodxiP'=' 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  62. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  63. 1agATDYEO 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  64. the 作者
    Windows 10 · Google Chrome

    1LtA3GptO

    2022-04-28 回复
  65. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  66. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  67. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  68. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  69. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  70. the 作者
    Windows 10 · Google Chrome

    555'"()&%

    dFyf(9185)

    2022-04-28 回复
  71. the9909741 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  72. the9360834 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  73. acu10827<s1﹥s2ʺs3ʹuca10827 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  74. the 作者
    Windows 10 · Google Chrome

    555'"()&%

    BCCu(9268)

    2022-04-28 回复
  75. the9286294 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  76. the9266394 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复

备案号: 浙ICP备2021040483号