1 串的介绍
概念:串是由零个或多个字符串组成的有限序列,又名叫字符串。
例如:str = “abc”,当字符串由零个字符组成时,被称为空串,即 str = null
2 字符串的比较
说到字符串的比较,想到的只有“==”,其实字符串也可以使用"<","<=",">=",">"等符号来比较,当然使用这些符号来比较时主要是根据字符串中每个字符的ASCII来比较
,再举个例子:
String str1 = "abc";
String str2 = "abcd";
String str3 = "abb";
// str1对应的ASCII是 97+98+99
// str2对应的ASCII是 97+98+99+100
// str3对应的ASCII是 97+98+98
//因此,三个字符串比较时结果为 str2 > str1 > str3
3 Java中字符串(String)的理解和使用
3.1 数据结构
3.2 String类
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
......
}
首先String是一个带有final修饰的类,因此它不可以被继承,一旦实例化就是不可变的
。
3.3 构造方法
//空构造方法,默认为 “”
public String() {
this.value = "".value;
}
//实例化String,包括值和哈希值
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
//将char数组转为字符串
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
//将char数组以索引形式转为String
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
//将byte数组转为字符串
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
//将StringBuffer转为String(线程安全)
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
//将StringBuffer转为String
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
//还有几个构造方法在这里就不做讲解啦......
3.4 常用方法
3.4.1 length()
返回字符串长度,包括空格
public int length() {
return value.length;
}
3.4.2 isEmpty()
判断字符串是否为空
public boolean isEmpty() {
return value.length == 0;
}
3.4.3 getBytes()
将字符串转换为byte数组
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
3.4.4 equals(Object)
将字符串和任意类型作比较
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;
}
3.4.5 equalsIgnoreCase(String)
将两个字符串忽略大小写比较
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
3.4.6 indexOf(int)
根据索引查看字符串中单个字符
public int indexOf(String str) {
return indexOf(str, 0);
}
3.4.7 substring(int,int)
根据索引范围截取字符串
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);
}
3.4.8 split(String)
根据某个字符串将该字符串变分为字符串数组
public String[] split(String regex) {
return split(regex, 0);
}
3.4.9 toLowerCase()和toUpperCase()
将字符串全部变为小写或大写
3.4.10 trim()
将字符串两侧的空格删除
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
4 传统的字符串模式匹配方法
/**
* 经典字符串模式匹配
*
* @return
*/
public static int strcmp1() {
final String source = "ABCDEFG";
final String pattern = "BCD";
char[] s = source.toCharArray();
char[] p = pattern.toCharArray();
int i = 0;
int j = 0;
if (source.length() < pattern.length()) {
//如果主串长度小于模式串,直接返回-1,匹配失败
return -1;
} else {
while (i < source.length() && j < pattern.length()) {
//如果i,j位置上的字符匹配成功就继续向后匹配
if (s[i] == p[j]) {
++i;
++j;
} else {
//i回到主串上一次开始匹配下一个位置
i = i - (j - 1);
//j重置 模式串重新进行匹配
j = 0;
}
}
if (j == pattern.length()) {
//匹配成功
return i - j;
} else {
//匹配失败
return -1;
}
}
}
5 KMP算法进行字符串模式匹配
/**
* KMP算法模式匹配
*
* @return
*/
public static int strcmp2() {
final String source = "ABCDEFG";
final String pattern = "BCD";
int i = 0;
int j = 0;
char[] s = source.toCharArray();
char[] p = pattern.toCharArray();
int[] next = getNext(p);
while (i < s.length && j < p.length) {
if (j == -1 || s[i] == p[j]) {
++i;
++j;
} else {
//如果j != -1且当前字符匹配失败,则令i不变,
//j = next[j],即让pattern模式串右移j - next[j]个单位
j = next[j];
}
}
if (j == p.length) {
return i - j;
} else {
return -1;
}
}
private static int[] getNext(char[] p) {
int[] next = new int[p.length];
int k = -1;
int j = 0;
next[0] = -1;
while (j < p.length - 1) {
if (k == -1 || p[j] == p[k]) {
++k;
++j;
next[j] = k;
} else {
k = next[k];
}
}
return next;
}
完~