美文网首页IT修真院_JAVA
原来String是这样的(下)

原来String是这样的(下)

作者: LeeZer | 来源:发表于2017-07-03 23:21 被阅读0次

前言

回顾

我们讲到了String的equals和==的区别:

equals根据你编写的方法体来进行比较,而==是根据比较的引用地址是否相同来比较的。

主题

今天我们来讲讲String类的其他.方法在源码中的解读.

例子

publicclassTest{

publicstaticvoidmain(String[] args){

String a1=newString("abc");

String a2=newString("abc");

System.out.println(a1.isEmpty());

System.out.println(a1.length());

System.out.println(a1.charAt(1);

System.out.println(a1.substring(2,3));

}

}

String的几个方法

这里我们介绍了String的几个简单的方法。

.isEmpty();

.length();

.charAt();

.subString();

我们查看源码就可以知道:String方法的几个构造器

.isEmpty();

/**

* Returns {@codetrue} if, and only if, {@link#length()} is {@code0}.

*

*@return{@codetrue} if {@link#length()} is {@code0}, otherwise

* {@codefalse}

*

*@since1.6

*/

publicbooleanisEmpty(){

returnvalue.length ==0;

}

isEmpty直接判断传进来的值长度是否为0

.length();

/**

* Returns the length of this string.

* The length is equal to the number of Unicode

* code units in the string.

*

*@returnthe length of the sequence of characters represented by this

*          object.

*/

publicintlength(){

returnvalue.length;

}

返回字符串的长度

.charAt();

/**

* Returns the {@codechar} value at the

* specified index. An index ranges from {@code0} to

* {@codelength() - 1}. The first {@codechar} value of the sequence

* is at index {@code0}, the next at index {@code1},

* and so on, as for array indexing.

*

*

If the {@codechar} value specified by the index is a

* surrogate, the surrogate

* value is returned.

*

*@paramindex  the index of the {@codechar} value.

*@returnthe {@codechar} value at the specified index of this string.

*            The first {@codechar} value is at index {@code0}.

*@exceptionIndexOutOfBoundsException  if the {@codeindex}

*            argument is negative or not less than the length of this

*            string.

*/

publiccharcharAt(intindex){

if((index <0) || (index >= value.length)) {

thrownewStringIndexOutOfBoundsException(index);

}

returnvalue[index];

}

charAt这里我们可以看到,类型为char,定义了一个下标

如果下标index<0或者>=字符的长度抛出StringIndexOutOfBoundsException

否则返回value[index] 这里我们返回的就是vlaue[1] 答案为b

.subString();

**

* 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}.

*

* Examples:

*

* "hamburger".substring(4, 8) returns "urge"

* "smiles".substring(1, 5) returns "mile"

*

*

* @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);

}

subString方法有两个参数,一个起始指标,一个结束指标

判断beginIndex < 0抛出StringIndexOutOfBoundsException,同样的结束index大于长度抛出StringIndexOutOfBoundsException

然后定义一个sbuLen,为endIndex - beginIndex,判断subLen小于0抛出StringIndexOutOfBoundsException

返回如果(beginIndex == 0) && (endIndex == value.length)返回对象本身,否则该数组和beginIndex、subLen`构成新的对象返回

这里讲的就是String基本的几个方法。这里还有一个小彩蛋就是&和&&的区别

大家可以回忆一下.

&:方法无论前值是否为true,都要判断后面的逻辑表达式

&&:会形成短路,前面为false的时候就不执行后面逻辑

结尾附上String的基本方法:

/**

* Initializes a newly created {@codeString} object so that it represents

* an empty character sequence.  Note that use of this constructor is

* unnecessary since Strings are immutable.

*/

publicString(){

this.value ="".value;

}

/**

* Initializes a newly created {@codeString} object so that it represents

* the same sequence of characters as the argument; in other words, the

* newly created string is a copy of the argument string. Unless an

* explicit copy of {@codeoriginal} is needed, use of this constructor is

* unnecessary since Strings are immutable.

*

*@paramoriginal

*        A {@codeString}

*/

publicString(String original){

this.value = original.value;

this.hash = original.hash;

}

这就是String构成其实是一个数组,有value和hash两个属性,大家可以多多理解和想象一下.

相关文章

  • 原来String是这样的(下)

    前言 回顾 我们讲到了String的equals和==的区别: equals根据你编写的方法体来进行比较,而==是...

  • 震惊!原来String类是这样的(上)

    前言 我们在学习Java中通常有使用的String这个类,可是我们也不会有多少人去看过它吧?接下来是我一些对Str...

  • 原来梦想是这样绘制的(下)

    当你按照车日路模型绘制完自己的人生梦想时就可以了吗?答案是否定的。我们还有很多需要考虑的问题点: 关于“车” 一:...

  • 原来你是这样的《游西湖》下

    悲惨的结局,你看这是一段孽缘吗? 那杭州美景盖世无双 西湖岸奇花异草四季清香 那春游苏堤桃红柳绿 夏赏荷花映满了池...

  • 意外之城(下):原来你是这样的兰州

    兰州:意外吃到牛肉面 在大众巷吃完炸酱面和浆水面,心满意足地准备回酒店。刚走了几步,竟然在街角处惊现一家24小时营...

  • 原来是这样的

    我对自己的臀部形态不满意,所以我会用更多的训练动作来达到丰臀大臀的效果,好多年了都没有达到效果,也问了臀部练的...

  • 原来我是这样的

    https://mp.weixin.qq.com/s/OkCuTvymFpZ5mqgNxn6KlA 此链接是我的个...

  • 原来我是这样的

    窗外雾缭绕,有点小冷,所以就不想起,躺在床上,听着纯音乐,也还不错,无事做,就在想,我是怎样的我? 总的来说,...

  • 原来是这样的

    原来话是不可以乱说的! 不管对什么人,话都是不可以乱说的 原来话是要在脑子里过一遍才可以说的! 说出的话都是有目有...

  • 原来我是这样的

    今天有给老师发在我心中认为的他的状况,想要很快联系他,如果他不是很OK的话,我想我可以让他来西安,我可以让我们学校...

网友评论

    本文标题:原来String是这样的(下)

    本文链接:https://www.haomeiwen.com/subject/mrxthxtx.html