String 类的方法
1. equals() 是判断两个对象的内容
== 判断内存地址,看是否是同一对象
2. charAt(int index) 返回指定位置的char值(0~length-1)
3. toCharArray() 将此字符串转换为一个新的字符数组。
4. indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
5. startsWith(String prefix) 测试此字符串是否以指定的前缀开始。返回值为Boolean型
endsWith(String prefix) 测试此字符串是否以指定的后缀结束。返回值为Boolean型
6. toLowerCase() 将String转化为小写,如果字符串中没有应该被转换的字符,则将原字符返回;
toUpperCase() 将String转化为大写
例:编写一个函数把字符串反转
public class StringDemo {
public static void main(String[] args) {
System.out.println(reverseString("abcdefg"));
}
public static String reverseString(String src){
if (src==null || src.length() < 2){
return src;
}
String temp="";
for(int i=src.length()-1;i>=0;i--){
temp=temp+src.charAt(i);
}
return temp;
}
}
7. split(String reg) 根据给定正则表达式的匹配拆分此字符串,返回值为字符串数组 String[]
package stringdemo;
public class StringDemo {
public static void main(String[] args) {
String str1="sb,1993,7,13";
String[] arr=str1.split(",");
if(arr != null && arr.length >0){
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
}
8. substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
substring(int beginIndex , int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的beginIndex 处开始,直到索引endIndex-1处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
beginIndex
- 起始索引(包括)。endIndex
- 结束索引(不包括)。
9. getBytes() 使用平台的默认字符集将此 String
编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
getBytes(Charset charset) 使用给定的 charset 将此 String
编码到 byte 序列,并将结果存储到新的 byte 数组。
String(byte[] bytes,Charset charset) String类的一个构造方法,通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
package stringdemo;
import java.io.UnsupportedEncodingException;
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String str="a中b国c";
byte[] bytes=str.getBytes("iso8859-1");
System.out.println(new String(bytes,"iso8859-1"));
bytes=str.getBytes("gbk");
System.out.println(new String(bytes,"gbk"));
bytes=str.getBytes("gb2312");
System.out.println(new String(bytes,"gb2312"));
}
}
StringBuffer类
StringBuffer 类是一个类似于String的字符串缓冲区,可以对字符串进行增删。该类加了同步,是线程安全的,构造方法:
1. StringBuffer() 构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
2. StringBuffer(int capacity) 构造一个不带字符,但具有指定初始容量的字符串缓冲区。
3. StringBuffer(String str) 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
StringBuffer类的方法
1. append(char c) 将 char 参数的字符串表示形式追加到此序列。
2. toString() 返回此序列中数据的字符串表示形式
package stringdemo;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
buffer.append("abc");
buffer.append("def");
buffer.append("ghi");
System.out.println(buffer.toString());
}
}
3. lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引,返回值为int。(此方法可用于提取文件的后缀名)如:
package stringdemo;
public class LastIndexOfDemo {
public static void main(String[] args) {
String str = "d:/downloads/data/a.txt";
String temp = str.substring(str.lastIndexOf("."));
System.out.println(temp);
}
}
4. deleteCharAt(int index) 移除此序列指定位置的 char
。
StringBuilder类
一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比StringBuffer 要快。