语言:Java

String.substring(int , int ) -- 截取某个区间的string

  1. /**
  2. * Returns a string that is a substring of this string. The
  3. * substring begins with the character at the specified index and
  4. * extends to the end of this string. <p>
  5. * Examples:
  6. * <blockquote><pre>
  7. * "unhappy".substring(2) returns "happy"
  8. * "Harbison".substring(3) returns "bison"
  9. * "emptiness".substring(9) returns "" (an empty string)
  10. * </pre></blockquote>
  11. *
  12. * @param beginIndex the beginning index, inclusive.
  13. * @return the specified substring.
  14. * @exception IndexOutOfBoundsException if
  15. * {@code beginIndex} is negative or larger than the
  16. * length of this {@code String} object.
  17. */
  18. public String substring(int beginIndex) {
  19. if (beginIndex < 0) {
  20. throw new StringIndexOutOfBoundsException(beginIndex);
  21. }
  22. int subLen = value.length - beginIndex;
  23. if (subLen < 0) {
  24. throw new StringIndexOutOfBoundsException(subLen);
  25. }
  26. return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
  27. }
  28.  
  29. /**
  30. * Returns a string that is a substring of this string. The
  31. * substring begins at the specified {@code beginIndex} and
  32. * extends to the character at index {@code endIndex - 1}.
  33. * Thus the length of the substring is {@code endIndex-beginIndex}.
  34. * <p>
  35. * Examples:
  36. * <blockquote><pre>
  37. * "hamburger".substring(4, 8) returns "urge"
  38. * "smiles".substring(1, 5) returns "mile"
  39. * </pre></blockquote>
  40. *
  41. * @param beginIndex the beginning index, inclusive.
  42. * @param endIndex the ending index, exclusive.
  43. * @return the specified substring.
  44. * @exception IndexOutOfBoundsException if the
  45. * {@code beginIndex} is negative, or
  46. * {@code endIndex} is larger than the length of
  47. * this {@code String} object, or
  48. * {@code beginIndex} is larger than
  49. * {@code endIndex}.
  50. */
  51. public String substring(int beginIndex, int endIndex) {
  52. if (beginIndex < 0) {
  53. throw new StringIndexOutOfBoundsException(beginIndex);
  54. }
  55. if (endIndex > value.length) {
  56. throw new StringIndexOutOfBoundsException(endIndex);
  57. }
  58. int subLen = endIndex - beginIndex;
  59. if (subLen < 0) {
  60. throw new StringIndexOutOfBoundsException(subLen);
  61. }
  62. return ((beginIndex == 0) && (endIndex == value.length)) ? this
  63. : new String(value, beginIndex, subLen);
  64. }

String.charAt(int) -- String中某个位置的字符

  1. /**
  2. * Returns the {@code char} value at the
  3. * specified index. An index ranges from {@code 0} to
  4. * {@code length() - 1}. The first {@code char} value of the sequence
  5. * is at index {@code 0}, the next at index {@code 1},
  6. * and so on, as for array indexing.
  7. *
  8. * <p>If the {@code char} value specified by the index is a
  9. * <a href="Character.html#unicode">surrogate</a>, the surrogate
  10. * value is returned.
  11. *
  12. * @param index the index of the {@code char} value.
  13. * @return the {@code char} value at the specified index of this string.
  14. * The first {@code char} value is at index {@code 0}.
  15. * @exception IndexOutOfBoundsException if the {@code index}
  16. * argument is negative or not less than the length of this
  17. * string.
  18. */
  19. public char charAt(int index) {
  20. if ((index < 0) || (index >= value.length)) {
  21. throw new StringIndexOutOfBoundsException(index);
  22. }
  23. return value[index];
  24. }

String.length() -- 可以直接获得string的长度

String.indexOf(char) -- 找到返回index,找不到返回-1

相关重载:

String.indexOf(String)

String.indexOf(String,int fromIndex)

String.indexOf(int unicode)

String.indexOf(int unicode,int fromIndex)

String.split(" ")  -- 必须是双引号

  1. //必须是双引号!!!不能是单引号
  2. String[] splitArray = s.split(" ");

倒叙插入字符

如果需要倒叙插入字符,有两种方法:

方法一:复杂度O(n2),背离StringBuilder()初衷

  1. StringBuilder sb = new StringBuilder();
  2. for(int i=0;i<100;i++){
  3. sb.insert(0, Integer.toString(i));
  4. }

方法二:复杂度下降为O(n)

  1. StringBuilder sb = new StringBuilder();
  2. for(int i=0;i<100;i++){
  3. sb.append(Integer.toString(i));
  4. }
  5. sb.reverse();
  6. return result.toString();

String.isEmpty()

String默认只有String.isEmpty()函数,没有isNullOrEmpty()函数。

手写代码 - java.lang.String/StringBuilder 相关的更多相关文章

  1. 手写代码 - java.util.Arrays 相关

    1-拷贝一个范围内的数组 Arrays.copyOfRange( array, startIndex, endIndex); include startIndex... exclude endInde ...

  2. 手写代码 - java.util.List 相关

    1-ArrayList 访问元素,不能使用ArrayList[0]形式!!!! 必须使用ArrayList.get(0);

  3. 能不能自己写个类,也叫java.lang.String?

    可以,但在应用的时候,需要用自己的类加载器去加载,否则,系统的类加载器永远只是去加载jre.jar包中的那个java.lang.String.由于在tomcat的web应用程序中,都是由webapp自 ...

  4. java.lang.String & java.lang.StringBuilder

    java.lang.String & java.lang.StringBuilder String 成员方法 作用 public charAr(int index) 返回给定位置的代码单元 p ...

  5. 【Java面试题】53 能不能自己写个类,也叫java.lang.String?

    可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...

  6. 能否自己也写一个类叫做java.lang.String?

    这次的随笔很逗吧~没错,我们的确也可以自己在创建一个包java.lang,然后在 相应的包下面创建一个对应的类String,但是在每次jre运行的时候,我们都回去加载原来默认的java.lang.St ...

  7. java.lang.String中的replace方法到底替换了一个还是全部替换了。

    你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了. 但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串 ...

  8. Java源码学习 -- java.lang.String

    java.lang.String是使用频率非常高的类.要想更好的使用java.lang.String类,了解其源代码实现是非常有必要的.由java.lang.String,自然联想到java.lang ...

  9. 从源码分析java.lang.String.isEmpty()

    今天在写代码的时候用到了java.lang.String.isEmpty()的这个方法,之前也用过,今天突发奇想,就看了看源码,了解了解它的实现方法,总结出来,大家可以交流交流. 通常情况下,我们使用 ...

随机推荐

  1. 递归思维判断数组a[N]是否为一个递增数组

    递归的方法:记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束: bool fun( int a[], int n ) { ) { return true; } ) { ] ...

  2. gulp入门指南

    1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: $ npm install --save-dev ...

  3. 关于串session

    关于session: 最近在用IE浏览器调试不同用户所拥有的权限功能,出现了串session.串session,主要的流程结构如下: 解决方法: 在IE快捷方式上点击鼠标右键>属性>快捷方 ...

  4. linux下测试磁盘的读写IO速度-简易方法

    linux下测试磁盘的读写IO速度-简易方法 参考资料:https://blog.csdn.net/zqtsx/article/details/25487185 一:使用hdparm命令 这是一个是用 ...

  5. JavaSE_坚持读源码_Class对象_Java1.7

    Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类.虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class类 ...

  6. Hbase balancer RSgroup shell 脚本

    #!/bin/bashTMP_FILE=tmp_groupsGROUPS_FILE=groups.txtecho "list_groups" | hbase shell > ...

  7. 来自Composer中文网安装composer指南

    如果有需要安装composer的童鞋可以参考这个文章配置自己的composer https://pkg.phpcomposer.com/#how-to-install-composer 这里可能会出很 ...

  8. mysql导入导出sql文件,source导入速度慢的解决办法

    1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql2.导出一个表mys ...

  9. Silverlight中字典的使用

    通过值搜索字典中的项: FristOfDefault返回序列中满足条件的第一个元素:如果未找到这样的元素,则返回默认值.

  10. Slider绑定事件,初始化NullPointerException错误

    最近刚刚接触Silverlight,随便在网上找了一个入门的博文http://www.cnblogs.com/Terrylee/archive/2008/03/07/Silverlight2-step ...