1.String类中的length()方法:
public int length() {
return value.length;
}
length方法返回的是字符串的长度,长度等于字符串中的数字Unicode code units 。
2.Sting类的isEmpty()方法:
public boolean isEmpty() {
return value.length == 0;
}
判断对象是否为空,返回一个布尔值。当字符串的长度为0是返回true,当字符串的长度不为0时返回false。
示例:
package com.ftl813;
public class Test {
public static void main(String[] args) {
String str=new String();
String str1="";
String str2=null;
String str3="abcd";
System.out.println(str.equals(str1));
System.out.println(str.equals(str2));
System.out.println(str.length());
System.out.println(str1.length());
// System.out.println(str2.length());
System.out.println(str3.length());
System.out.println(str.isEmpty());
System.out.println(str1.isEmpty());
// System.out.println(str2.isEmpty());
System.out.println(str3.isEmpty());
}
}
解析:
String str=new String(); // 和String str1=""的作用相同,在内存里分配了一块区域,里面放了一个 "" 空字符串。
String str1=""; //在内存里分配了一块区域,里面放了一个 "" 空字符串。
String str2=null; //在内存里分配了一块区域,里面放了一个 null。
String str ; //没有在内存里给分配相应的区域,必须要赋值才可以使用。
3.String类中的charAt(int index)方法:
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
根据传入的index值(索引)返回该位置上的字符。String的底层结构是用数组存储的index的值实际上就是数组的下标。
index的值在0到value.length-1之间
示例:
package com.ftl813;
public class Test {
public static void main(String[] args) {
String str3="abcd";
System.out.println(str3.charAt(2));
}
}
输出:c
解析:a,b,c,d对应的索引分别是0,1,2,3
4.String类中的codePointAt(int index)方法:
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
}
返回指定索引处的字符(Unicode代码点)。 该索引指的是char
值(Unicode码单位),范围从0
到length()
- 1
。
如果在给定索引处指定的char
值处于高代理范围内,则以下指数小char
值String
的长度,并且下列指数的char
值处于低代理范围,则补码代码相应的这个代理对被退回。 否则,返回给定索引处的char
值。
示例:
package com.ftl813;
public class Test {
public static void main(String[] args) {
String str=new String();
String str1="";
String str2=null;
String str3="abcd";
System.out.println(str3.codePointAt(0));
System.out.println(str3.codePointAt(2));
}
}
输出:97
99
解析:根据索引查找到索引位置的字符,然后返回该字符的ASCII码。
5.String类中的codePointBefore(int index)方法:
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代码点)。 索引指的是char
值(Unicode代码单位),范围从1
到length]\
如果
char
的(index - 1)
为(index - 1), (index - 2)
为char
为(index - 2)
为char
为(index - 2)
,为高代数范围,则返回替代对的补码。 如果char
在值index - 1
是未配对的低代理或一个高代理,则返回所述替代值。
package com.ftl813;
public class Test {
public static void main(String[] args) {
String str3="abcd";
System.out.println(str3.codePointBefore(1));
System.out.println(str3.codePointBefore(2));
}
}
输出:97
98
解析:根据索引查找到索引位置前一位的字符,然后返回该字符的ASCII码。
6.String类中的codePointCount(int beginIndex, int endIndex)方法:
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);
}
返回此String
指定文本范围内的Unicode代码点数。 文本范围始于指定beginIndex
并延伸到char
在索引endIndex - 1
。
因此,文本范围的长度(在char
秒)为endIndex-beginIndex
。 文本范围内的非配对替代品,每一个代码点都是一样的。
以下是在网上找的解析:码点,我译为“码位值”。每个码位值实际上代表一个真正unicode字符。即unicode字符集上的码位值。
为什么要这些码位相关的方法?源自1个java的char字符并不完全等于一个unicode的字符。
char采用UCS-2编码是一种淘汰的UTF-16编码,最多65536种形态,也远少于当今unicode拥有11万字符的需求。java只好对后来新增的unicode字符用2个char拼出1个unicode字符。导致String中char的数量不等于unicode字符的数量。
codePointCount()就是准确计算unicode字符的数量,而不是char的数量。