String类的substring方法

下列程序的输出是什么?

class A { public static void main(String[] a) {
    String v = “base”;      v.concat(“ball”);
    v.substring(1,5);       System.out.println(v);  }
}

分析:由于String是不可变的,当执行v.concat("ball")时,v本身还是指向“base”,当再执行v.substring(1,5)时,会报出界异常

如下为concat的源码:可以看到,最后返回的是新的字符串

public String concat(String str) {
        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);
    }

如下是substring源码:

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);
}

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);
}
时间: 2024-08-24 17:07:50

String类的substring方法的相关文章

String类的substring方法bug

今天再看JDK源码的时候看到了String类的不同版本的实现方式的不同,主要是substring这个方法,JDK6里面的实现方式是: 很明显可以看到,调用String对象的substring方法后指向的对象地址并没有发生改变,只是改变的是偏移量,这样的话在GC阶段就有可能造成内存泄露了. 还好查了一下资料JDK7解决了这个问题,于是赶紧查看了JDK7的源码: 这个里面是通过内存复制的方式重新指向了一个新的地址,解决了内存泄露的隐患

String类的substring()方法

截取字符串,在java语言中的用法 1.  public String substring(int beginIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符,一直到此字符串末尾. 参数:beginIndex - 开始处的索引(包括), 返回:指定的子字符串, 异常:如果 beginIndex 为负或大于此 String 对象的长度,则抛出IndexOutOfBoundsException 例  :"unhappy".substring(2)

关于JAVA的String类的一些方法

一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2.StringBuffer类的capacity()方法与String类的 length()的方法类似,但是她测试是分配给StringBuffer的内存空间的大小,而不是当前被使用了的内存空间. 3.如果想确定字符串中指定字符或子字符串在给定字符串的位置,可以用 indexOf()和lastIndexO

【java】String类的基本方法

Java的String类基本方法 一.构造函数 函数 返回值 作用 String(byte[] bytes) String 通过byte数组构造字符串对象 String(char[] chars) String 通过char数组构造字符串对象 String(String string) String 拷贝一个值为string的字符串对象 String(StringBuffer buffer) String 通过StringBuffer构造字符串对象 二.String类的基本方法 函数 返回值 作

java.lang.String 类的所有方法

java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点). int codePointBefore(int index) 返回指定索引之前的字符(Unicode 代码点). int codePointCount(int beginIndex, int endIndex) 返回此 String 的指定文本范围中的 Un

了解String类的intern()方法

相信绝大多数的人不会去用String类的intern方法,打开String类的源码发现这是一个本地方法,定义如下: public native String intern(); 文档告诉我们该方法返回一个字符串对象的内部化引用. public String intern()返回字符串对象的规范化表示形式. 一个初始时为空的字符串池,它由类 String 私有地维护. 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确

Servlet 中为多项选择题判分---String类的indexOf()方法妙用

首先来看一下String类的indexOf()方法的用法: 1 public class FirstDemo1 { 2 /** 3 *API中String的常用方法 4 */ 5 // 查找指定字符串是否存在 6 public static void main(String[] args) { 7 String str1 = "abcdefghijklmnabc"; 8 // 从头开始查找是否存在指定的字符 9 System.out.println(str1.indexOf("

ASP.Net string 类的扩展方法 [转]

string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<>     //累加 All<>        //是否都满足条件 Any<>        //是否有一个满足条件 AsEnumerable<>  // AsParallel<>    // AsQueryable<>    // Average<>      //平均值 Cast<>

String类的intern()方法

public native String intern(); 这个是在String类源码中申明的一个方法,是一个本地方法,返回的是一个String对象的引用.先看一下代码: String s1="123"; String s2 = new String("123"); System.out.println(s1==s2);//false System.out.println(s1==s2.intern());//true s1==s2结果为false,这个不再详述.