蒟蒻的Java入门之旅——String、StringBuffer和StringBuilder

一、样例API

1.String:

(1).codePointAt(int index)方法:
该方法可用于输出字符串中对应序号上字符的ASCII码的值。

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		System.out.println(a.codePointAt(1));//输出对应字符的Ascii码值
	}

}

(2).codePointCount(int begin,int end):
该方法与length()类似,用于计算字符串长度。它们的区别在于,length()用于检测的是字符串中的代码单元,但是codePointCount()是用于检测代码点的。在一般情况下两者是可以通用的。
代码点与代码单元点这(转载)

String中的length()方法:

public int length() {
    
    
        return value.length >> coder();
    }

string中的codePointCount()方法:

public int codePointCount(int beginIndex, int endIndex) {
    
    
        if (beginIndex < 0 || beginIndex > endIndex ||
            endIndex > length()) {
    
    
            throw new IndexOutOfBoundsException();
        }
        if (isLatin1()) {
    
    
            return endIndex - beginIndex;
        }
        return StringUTF16.codePointCount(value, beginIndex, endIndex);
    }

使用样例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		System.out.println(a.codePointCount(0,3));
	}

}

(3).contentEquals(StringBuffer sb)方法:
该方法用于比较String中的串值是否与StringBuffer中的串值相同。
方法代码:

    public boolean contentEquals(StringBuffer sb) {
    
    
        return contentEquals((CharSequence)sb);
    }

实现示例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		String b=new String("234");
		StringBuffer c = new StringBuffer( "234");
		System.out.println(a.contentEquals(c));
		System.out.println(b.contentEquals(c));
	}

}

结果:
在这里插入图片描述

2.StringBuffer
(1). deleteCharAt(int index):
该方法用于删除字符串值中指定序号位置上的字符(0号位开始)。
方法代码:

public synchronized StringBuffer deleteCharAt(int index) {
    
    
        toStringCache = null;
        super.deleteCharAt(index);
        return this;
    }

使用样例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "234");
		System.out.println(c);
		c.deleteCharAt(2);
		System.out.println(c);
	}

}

输出结果:
在这里插入图片描述
(2).replace(int start, int end, String str):
该方法用于使用String类的值替代指定区间内StringBuffer的值,这里优肯会出现StringIndexOutOfBoundsException 异常。

方法代码:

public synchronized StringBuffer replace(int start, int end, String str) {
    
    
        toStringCache = null;
        super.replace(start, end, str);
        return this;
    }

使用的实例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String a=new String("12");
		System.out.println(c);
		c.replace(0,2,a);
		System.out.println(c);
	}

}

输出结果为:
在这里插入图片描述
(3).indexOf(String str) 方法:
该方法的功能是用于字符串的比较,返回的是str对象的值在StringBuffer对象中第一次出现的位置。
方法代码:

  public int indexOf(String str) {
    
    
        // Note, synchronization achieved via invocations of other StringBuffer methods
        return super.indexOf(str);
    }

实例代码:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String a=new String("12");
		//System.out.println(c);
		//c.replace(0,2,a);
		System.out.println(c.indexOf(a));
	}

}

输出结果

-1//由于a在c中无法匹配

3.StringBuilder
(1).toString()方法:
该方法用于将StringBuild类转化为String类。
方法代码:

  public String toString() {
    
    
        // Create a copy, don't share the array
        return isLatin1() ? StringLatin1.newString(value, 0, count)
                          : StringUTF16.newString(value, 0, count);
    }

实例使用:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String b=c.toString();
		System.out.println(b);
	}

}

(2).appendCodePoint(int codePoint) 方法:
该方法用于增加StringBuild对象中的代码点。
方法代码:

public StringBuilder appendCodePoint(int codePoint) {
    
    
        super.appendCodePoint(codePoint);
        return this;
    }

4.三者的异同概要
修改性:由于String类中封装的数据类型是常字符数组,因此String类对象的值一旦创建边不可更改(修改后便会产生新的对象)。而StringBuffer和StringBuild中封装的是普通字符数组,所以可以任意修改对象的值。对于StringBuffer和StringBuild,前者是线程安全的,在并发程序中可被同时访问,而后者则不可。
同时,在传递参数时只有String的引用在被传参调用的方法中进行修改后仍不可被改变,原因是修改后形参是指向了一个新的对象,而不是对通过消息对原对象进行修改
5.字符编码与解码概要
将本地字符集转换为机器可识别格式码的过程称为编码,相反的则为解码。
编码规则介绍:
ASCII码:英文字母(不分大小写)占一个字节的空间,中文汉字占两个字节的空间。

UTF-8编码:一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。

Unicode编码:一个英文等于两个字节,一个中文(含繁体)等于两个字节。

解码编码示例:

public class Test{
    
    
	public static void main(String[] args) throws UnsupportedEncodingException{
    
    		
		String s=new String("中文");
		System.out.println(s);
		byte[] s1=s.getBytes();//转换成字节码
		System.out.println(s1);
		s=new String(s1,"GB2312");//重新编码
		System.out.println(s);
	}

}

了解更多点这(本部分为转载)

猜你喜欢

转载自blog.csdn.net/HiphopHarris/article/details/109333807