今天,俺们继续来进行String类的研究哈。
第七个方法:
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
这个方法在脚标小于0的时候会抛出异常,否则就会返回该字符串脚标所对应的字符.
第八个方法:
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
}
这个方法我开始看没看懂,后来测试了一下才知道,这个方法是返回字符串对应脚标在unicode中对应的编码,A对应的编码是65,如下测试所知:
下面是我的例子
public static void main(String[] args) {
String string = new String("ABC");
int test = string.codePointAt(0);
System.out.println(test);
}
第九个方法:
public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
}
这个方法很奇怪哦,不知道写这个方法的意义在哪里。这个方法主要就是返回字符串中传入脚标前一位字符所对应的unicode的编码
public static void main(String[] args) {
String string = new String("ABC");
int test = string.codePointBefore(1);
System.out.println(test);
}
我这边测试的例子是:65.和第八个方法传入0是一样的.
第十个方法
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);
}
这个方法感觉没啥子意义哦,这个方法就是返回字符串首脚标和末脚标之间对应的unicode的数量.
下面是我的例子:
public static void main(String[] args) {
String string = new String("0000");
int test = string.codePointCount(0, 4);
System.out.println(test);
}
这个结果是4
第十一个方法
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > value.length) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, value.length,
index, codePointOffset);
}
这个方法我试了一下,感觉是返回字符串的长度的。
第十二个方法
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
这个方法是返回字符串的字节数组哦。
第十三个方法
<pre class="html" name="code"> public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
这个方式是String里很重要的一个方法哦。传入一个object对象,如果传入的对象等于调用该方法的对象,那么就返回true,如果传入对象是String类型,那么就比较字符串的每一个,如果字符串的每一位都和调用该方法的的每一位相等,那么就返回为true,否则返回false,这个方法重写了object类的equals方法,是string类最为重要的方法之一
第十四个方法:
public boolean contentEquals(StringBuffer sb) {
synchronized (sb) {
return contentEquals((CharSequence) sb);
}
}
这个方法是比较内容的,传入一个StringBuffer的字符传,值得注意的是,这个方法内部因为实现了锁,所以是线程安全的.
下面是我测试的例子:
<pre class="html" name="code"> public static void main(String[] args) throws UnsupportedEncodingException {
String string = new String("0ABCD");
StringBuffer sb = new StringBuffer();
sb.append("0ABCD");
System.out.println(string.contentEquals(sb));
}
第十五个方法:
<pre class="html" name="code"> public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
这个方法很有意思哦,是无视大小写比较字符串的哦,咱们可以细细的查看一下。首先如果传入的参数和调用该方法的参数相等,就返回为true,如果不等于,就返回
(anotherString != null)&& (anotherString.value.length == value.length)&& regionMatches(true, 0, anotherString, 0, value.length).
而只要传入的字符串满足不为空而且长度相等而且满足regionMatches这个方法,就OK.
第十六个方法:
<pre class="html" name="code"> public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
这个方法是比较字符串的。首先传入一个字符串参数,比较调用该方法的字符串和传入的字符串的差,这个查是比较单个字符的,如果相等,就一直往后比较,直到结束,如果每一位都相等,就返回为0,所以在比较字符串相等的时候,也可以用这个方法.
下面是我的例子:
<pre class="html" name="code">public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("1".compareTo("2"));
}