语言:Java

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

    /**
* Returns a string that is a substring of this string. The
* substring begins with the character at the specified index and
* extends to the end of this string. <p>
* Examples:
* <blockquote><pre>
* "unhappy".substring(2) returns "happy"
* "Harbison".substring(3) returns "bison"
* "emptiness".substring(9) returns "" (an empty string)
* </pre></blockquote>
*
* @param beginIndex the beginning index, inclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if
* {@code beginIndex} is negative or larger than the
* length of this {@code String} object.
*/
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
} /**
* Returns a string that is a substring of this string. The
* substring begins at the specified {@code beginIndex} and
* extends to the character at index {@code endIndex - 1}.
* Thus the length of the substring is {@code endIndex-beginIndex}.
* <p>
* Examples:
* <blockquote><pre>
* "hamburger".substring(4, 8) returns "urge"
* "smiles".substring(1, 5) returns "mile"
* </pre></blockquote>
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if the
* {@code beginIndex} is negative, or
* {@code endIndex} is larger than the length of
* this {@code String} object, or
* {@code beginIndex} is larger than
* {@code endIndex}.
*/
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}

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

    /**
* Returns the {@code char} value at the
* specified index. An index ranges from {@code 0} to
* {@code length() - 1}. The first {@code char} value of the sequence
* is at index {@code 0}, the next at index {@code 1},
* and so on, as for array indexing.
*
* <p>If the {@code char} value specified by the index is a
* <a href="Character.html#unicode">surrogate</a>, the surrogate
* value is returned.
*
* @param index the index of the {@code char} value.
* @return the {@code char} value at the specified index of this string.
* The first {@code char} value is at index {@code 0}.
* @exception IndexOutOfBoundsException if the {@code index}
* argument is negative or not less than the length of this
* string.
*/
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}

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(" ")  -- 必须是双引号

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

倒叙插入字符

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

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

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

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

 StringBuilder sb = new StringBuilder();
for(int i=0;i<100;i++){
sb.append(Integer.toString(i));
}
sb.reverse();
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. 【优秀的设计绘图软件】Affinity Designer Beta for Mac 1.7

        [简介] Affinity Designer 1.7 beta 中文版本,具有全新的UI界面和功能,支持最新的 macOS Mojave 10.14 系统,Affinity Designer是 ...

  2. CodeForces1073E 数位dp+状压dp

    http://codeforces.com/problemset/problem/1073/E 题意 给定K,L,R,求L~R之间最多不包含超过K个数码的数的和. 显然这是一道数位dp,在做的过程中会 ...

  3. Mysql连接查询、子查询、联合查询 整理

    连接查询 连接语法的基本形式 from 表1  [连接方式]  join 表2  [on 连接条件]: 交叉连接 语法: from  表1  [cross]  join  表2  ;      //可 ...

  4. cmd命令对java程序进行编译时出现:编码GBK的不可映射字符

    原因:由于JDK是国际版的,在编译的时候,如果我们没有用-encoding参数指定JAVA源程序的编码格式,则java.exe首先获得我们才做系统默认采用的编码格式,也即在编译JAVA程序时,若我们不 ...

  5. python 函数动态参数,名称空间,global,nonlocal

    ##################################总结######################################动态参数 *args:位置参数动态传参,接收到的是元 ...

  6. angular,vue,react的基本语法—双向数据绑定、条件渲染、列表渲染、angular小案例

    基本语法: 1.双向数据绑定 vue 指令:v-model="msg" react constructor(){ this.state{ msg:"双向数据绑定" ...

  7. Java高并发秒杀API之业务分析与DAO层

    根据慕课网上关于java高并发秒杀API的课程讲解用maven+ssm+redis实现的一个秒杀系统 参考了codingXiaxw's blog,很详细:http://codingxiaxw.cn/2 ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 I Characters with Hash(模拟)

    https://nanti.jisuanke.com/t/31461 题意 一个hash规则,每个字母映射成一个两位数,求给的字符串最后的编码位数,要求去除最终结果的前导零 分析 按题意模拟就是了 # ...

  9. 自定义类使用泛型and方法使用泛型

    使用泛型的自定义类,泛型可以使用任意的数据类型,在创建对象的时候确定是什么数据类型,创建对象的时候不使用泛型,那就默认是Object类型 格式: 使用泛型的自定义类 package cn.zhuobo ...

  10. 细说java系统之动态代理

    代理模式 在深入学习动态代理之前,需要先掌握代理模式.只有深刻理解了代理模式的应用,才能充分理解Java动态代理带来的便利. 在生活中存在许多使用"代理模式"的场景,比如:村里的张 ...