String类
String:字符串类型,用" "括起来的都是字符串
特点:一旦创建值就不能改变,且值存储在方法区的常量池中。
创建字符串:
String s1 = "李白";
String类常见构造方法:
public String():空构造
public String(byte[ ] bytes):把字节数组转成字符串
public String(byte[ ] bytes,int index,int length):把部分字节数组转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[ ] value):把字符数组转成字符串
public String(char[ ] value,int index,int count):把部分字符数组转成字符串
public String(String original):把字符串常量值转成字符串,新创建的字符串是该参数字符串的副本
//例:
public class StringDemo{
public static void main(String args[]){
char[ ] a = { 'g', 'o', 'o', 'd'};
String a = new String(helloArray);
System.out.println(a);
}
}
字符串长度:length( )方法获取字符串长度
连接字符串:
使用 concat() 方法。string1.concat(string2);
使用’+'操作符来连接字符串。"Hello," + " java" + "!"
String类常见方法:
判断功能:
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""。
获取功能:
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
lastIndexOf系列:用法与indexOf一样
public int lastIndexOf(String str): 返回指定字符串在此字符串中最后一次出现处的索引。
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
转换功能:
public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
例:
char[] a = { 'g', 'o', 'o', 'd'};
String s1=String.valueOf(a);
System.out.println(s1);
public static String valueOf(int i): 把int类型的数据转成字符串。
(String类的valueOf方法可以把任意类型的数据转成字符串。)
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
其他功能:
替换功能:
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
去除字符串两空格:
public String trim() 去除两端空格
按字典顺序比较两个字符串:
public int compareTo(String str)
会对照ASCII 码表,从第一个字母进行减法运算,返回的就是这个减法的结果;
如果前面几个字母一样,则根据两个字符串的长度进行减法运算,返回的就是这个减法的结果;
如果连个字符串一摸一样,返回的就是0。
public int compareToIgnoreCase(String str) 与上面一样,忽略大小写的比较 。
StringBuffer类
StringBuffer:提供了字符串缓冲去,相当于一种容器。长度可变。
构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
常见方法:
添加功能:
public int capacity(): 返回当前容量,理论值
public int length(): 返回长度,实际值
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str): 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
删除功能:
public StringBuffer deleteCharAt(int index): 删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end): 删除从指定位置开始指定位置结束的内容,并返回本身
替换、反转功能:
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
public StringBuffer reverse(): 字符串反转
截取功能:
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
(注意:返回值类型不再是StringBuffer本身)
String与StringBuffer互相转换:
String转StringBuffer:
1、通过构造方
String s="hello";
StringBuffer s1=new StringBuffer(s);
2 、用append()方法
String s="hello";
StringBuffer s2=new StringBuffer();
s2.append(s);
~
StringBuffer转String:
1、通过构造方法
StringBuffer s=new StringBuffer("hello");
String s1=new String(s);
Systrm.out.println( s1);
2、用toString()方法
String s2=s.toString();
Systrm.out.println( s2);
3、用substring方法
String s3=s.substring(0,s.length());
Systrm.out.println( s3);
String、StringBuffer与StringBuilder的区别:
String:值不可变,每次修改操作会生成新的String对象,浪费内存空间;
StringBuffer:值可变,效率的,线程安全;
StringBuilder:值可变,效率高,线程不安全。
2791

被折叠的 条评论
为什么被折叠?



