String
字符串是引用数据类型,但是它的值在创建后不可以更改。
String str = "abc";
char data[] = {'a', 'b', 'c'};
String str = new String(data);
两种简单的构造方法
String str = "abc";
String str_1 = new String(str);
System.out.println(str.equals(str_1));
char[] c = {'a','b','c'};
String str = new String(c);
System.out.println(str);
常用的字符串方法
- length isEmpty
- public char charAt(int index) 返回char指定索引处的值
- public int codePointAt(int index) 该处字符对应的ASCII码值或unicode值
- public void getChars(int srcBegin, int srcEnd,char[] dst, int dstBegin)方法将字符从字符串复制到目标字符数组。
String str = "abcde";
char[] c = new char[10];
str.getChars(0, 3, c, 0);
for(char s:c) {
System.out.println(s);
}
- public String concat(String str)
将指定的字符串连接到该字符串的末尾。 - public boolean equals(Object anObject) 当且仅当该参数不是null并且是String对象,字符内容相同时返回true
- public boolean equalsIgnoreCase(String anotherString) 忽略大小写的比较
- public int compareTo(String anotherString)
从左向右依次比较两个字符串对象的内容。字符不同则返回字符差值;字符相同依次向后比较,直到字符不同或者字符串结束。字符相同则返回长度差。差是左减右,字符差是ascll差值。相同返回0. - public boolean startsWith(String prefix)
- public boolean endsWith(String suffix)
- 获取索引
- public int indexOf(int ch) 返回指定字符第一次出现的字符串内的索引。
- public int indexOf(int ch,int fromIndex)从指定索引处向后搜索,再作判定。
- public int lastIndexOf(int ch)返回指定字符的最后一次出现的字符串中的索引。
- public int lastIndexOf(int ch,int fromIndex) 从指定索引处向后搜索,再作判定。
同样的,方法参数还可以是字符串对象。
- public String substring(int beginIndex,int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。 - public String concat(String str) 将指定的字符串连接到该字符串的末尾。
- public String replace(char oldChar,char newChar) 替换字符串字符
- public boolean matches(String regex) true/false
- public boolean contains(CharSequence s)
String str1 = "abd";
System.out.println(str1.contains("ab"));
- public String replaceAll(String regex, String replacement)
成功则返回替换的字符串,失败则返回原始字符串。
例子来源
public class Test {
public static void main(String args[]) {
String Str = new String("www.google.com");
System.out.print("匹配成功返回值 :" );
System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));
System.out.print("匹配失败返回值 :" );
System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));
}
}
- public String replace(CharSequence target,CharSequence replacement)
- split
- join
- public char[] toCharArray() 将此字符串转换为新的字符数组。
- public static String valueOf(char[] data)
返回char数组参数的字符串char形式。 字符数组的内容被复制; 字符数组的后续修改不会影响返回的字符串。
String str1 = "abd";
char[] c = str1.toCharArray();
for(char s : c) {
System.out.println(s);
}
- public static String valueOf(char[] data)
返回char数组参数的字符串char形式。 字符数组的内容被复制; 字符数组的后续修改不会影响返回的字符串。
char[] c = {'a','b','c','d'};
String str = null ;
str = String.valueOf(c);
System.out.println(str);
System.arraycopy() 和 Arrays.copyOf
- Arrays.copyOf(原来的数组,新长度)方法返回的数组是新的数组对象,该拷贝不会影响原来的数组。
- arraycopy(Object src,int srcPos, Object dest, int destPos,int length);
int[] a = new int[] { 1, 2, 3 };
int[] b = Arrays.copyOf(a, 5);
System.out.println(b.length);
for (int i : b) {
System.out.println(i);}
int[] a = new int[] { 1, 2, 3 };
int[] b = new int[6] ;
System.arraycopy(a, 0, b, 0, 3);
for (int i : b) {
System.out.println(i);
}
StringBuffer StringBuilder
- StringBuffer 线程安全
- StingBuilder 速度快,线程不安全
- 方法
- append
- reverse
- delete(int start, int end) 左包含右不包含
- replace(int start, int end, String str)
StringBuffer sb = new StringBuffer("nishizhu");
sb.append(" hahaha");
sb.replace(0, 1, "wo");
System.out.println(sb);
