TypechoJoeTheme

Misty rain的博客

统计

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

2019-07-24
/
70 评论
/
17,460 阅读
/
正在检测是否收录...
07/24

前言:本篇主要介绍java中对字符串的操作,字符串在java中非常普遍,如果运用不当则会导致很严重的错误,为了避免这些不必要的错误,我们必须熟悉字符串这个重要的概念。

什么可以被称作字符串?
由多个字符连接起来的通常被称作为字符串,在java中通常将字符串作为对象来处理。字符串必须在“之内”,例如"123",这个123代表的就是字符串,而不是数字,被String修饰。
创建字符串--
之前说的字符串作为对象处理,对象都是new出来的,所以:String str=new String("123");
亦或采用字符串常量来创建,String str="123"
字符串连接--

    String str=new String("123");
    String str1="123";
    int i=1,j=2;
    
    System.out.println(str+str1);
    System.out.println(i+"不等于"+j);

字符串用+号来连接 输出123123 1不等于2,除了连接字符串,也同样连接int类型的。
知道了字符串,我们接下来就要会操作字符串--
获取字符串长度:

    String str=new String("abcdefj");
    System.out.println(str.length());

输出7,说明这个字符串长度为7
再例如,我们要寻找某个字符在整个字符串的位置,也就是下标。注意:字符串包括集合数组(以后会涉及到),他们的下标都是从0开始的而不是1:

    String str=new String("abcdefj");
    System.out.println(str.indexOf("c"));
    System.out.println(str.charAt(2));

这俩者分别代表了c的位置2 和下标为2的字符c ,一个返回int型 一个char (这里提及到了返回值,顾名思义就是运算或者其他方法返回的类型值 后面还会有更详细的解释)
利用substring截取字符串:

substring 有2个重载 1个和2个参数-

    String str=new String("abcdefj");
    System.out.println(str.substring(2));
    System.out.println(str.substring(2,5));

第一个方法取2(包括下标2)以后的所有字符,第二个取2到第五个字符中间的所有字符,不包括5,也就是234 /cde。

字符串去除空格:
在使用字符串的时候 空格会影响到整个字符串,这时候要有一定的策略才能避免这种低端错误(因为空格也是会影响到字符串的长度的)

    String str=new String("abcdefj   ");
    System.out.println(str.length());
    System.out.println(str.trim().length());

输出 10 7 .trim 就是去除空格的方法

字符串替换:

    String str=new String("abcdefj");
    System.out.println(str.replace(oldChar, newChar));

2个参数 oldchar假设为a newchar为b 则输出 bbcdefj。

最后要说的是比较字符串的方法:

    String str1="11";
    String str2="22";
    if(str1.equals(str2)||!str1.equals(str2)){
        System.out.println("相同");
    }else{
        System.out.println("不相同");
    }

俩者有一则成立则输出相同 。

ok, 有疑问或者不足的地方多指教,一起努力,共同进步。

下篇预告:字符串相关介绍及使用(下)

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

Misty rain的博客

本文链接:

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

评论 (70)
  1. Misty rain 作者
    Windows 10 · Google Chrome

    写的不错,哈哈~

    2021-12-17 回复
  2. kjh 作者
    Windows 10 · Google Chrome

    张哥 我试试换个邮箱是啥效果

    2021-12-17 回复
  3. 1 作者
    Windows 10 · Google Chrome

    1

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

    1

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

    1

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

    1

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

    1

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

    1

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

    1

    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

    1

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

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

    '+response.write(9838240*9891401)+'

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

    to@example.com>%0d%0abcc:009247.351-84435.351.e3bca.19109.2@bxss.me

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

    ${10000182+9999775}

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

    "+response.write(9838240*9891401)+"

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

    -1; waitfor delay '0:0:15' --

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

    )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

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

    HttP://bxss.me/t/xss.html?%00

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

    "+"A".concat(70-3).concat(22*4).concat(120).concat(65).concat(119).concat(89)+(require"socket"
    Socket.gethostbyname("hitex"+"gamekxmc45a74.bxss.me.")[3].to_s)+"

    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

    pnaAUcv1') OR 170=(SELECT 170 FROM PG_SLEEP(15))--

    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. 1 作者
    Windows 10 · Google Chrome

    1

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

    1

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

    1

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

    1

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

    1

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

    1

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

    555

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

    1

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

    555

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

    1

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

    1

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

    1

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

    1

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

    看的我热血沸腾啊https://www.jiwenlaw.com/

    2024-09-23 回复
  69. tnvwennaah 作者
    Windows 10 · Google Chrome

    想想你的文章写的特别好https://www.237fa.com/

    2024-10-01 回复
  70. aiwwqexlrq 作者
    Windows 10 · Google Chrome

    看的我热血沸腾啊https://www.ea55.com/

    2024-10-04 回复

备案号: 浙ICP备2021040483号