------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
String类适用于描述字符串事物。
那么它就提供了多个方法对字符串进行操作。
String s1 = “abc”;//s1是一个类类型变量,“abc”是一个对象。
//字符串最大的特点:一旦被初始化就不可以被改变。
Stirng s2 = new String(“abc”);
s1和s2有什么区别?
s1在内存中一个对象。
s2在内存中有两个对象。
字符串常见的操作有哪些?
1获取。
1.1字符串中包含的字符数,也就是字符串的长度。
int length();获取长度(注:字符串的length一定要加括号)
1.2根据位置获取某个位置上的字符。
charcharAt(int index);
1.3根据字符获取该字符在字符串中的位置。
int indexOf(intch);返回的是ch在字符串中第一次出现的位置。
int indexOf(intch, int fromIndex);从fromIndex指定位置开始获取ch
int indexOf(Stringstr);返回的str在字符串中第一次出现的位置。
int indexOf(Stringstr, int fromIndex);从fromIndex指定位置开始获取str在字符串中出现的位置
intlastIndexOf(int ch);
代码演示:
public class Lianxi
{
public static void method_get()
{
String str = "abcdasfefdsff";
//长度
sop(str.length());
//根据索引获取字符
sop(str.charAt(4));//当访问到字符串中不存在的角标时会发生StringIndexOutOfBoundsException
//根据字符获取索引
sop(str.indexOf("a"));//如果没有找到返回-1。
//反向索引一个字符出现的位置
sop(str.lastIndexOf("a"));//从右往左查找a,但是角标顺序依然是从左往右数的
}
public static void main(String[] args)
{
method_get();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
2判断
2.1字符串中是否保函某一个子串。
booleancontains(str);
特殊之处:indexOf(str);可以索引str第一次出现位置,如果返回-1表示该str不在字符串中存在。所以也可以使用于对指定字符串判断是否包含。
2.2字符串中是否有内容。
boolean isEmpty();原理就是判断长度是否为0。
2.3字符串是否是以指定内容开头。
boolean startsWith(str);
2.4字符串是否是以指定内容结尾。
boolean endsWith(str);
2.5判断字符串的内容是否相同,复写Object类中的equals方法
boolean equals(str)
2.6判断内容是否相同,并忽略大小写。
booleanequalsIgnoreCase();
代码演示:
public class Lianxi
{
public static void method_is()
{
String str = "ArrayDemo.java";
//判断文件名称是否是Array单词开头。
sop(str.startsWith("Array"));
//判断文件名称是否是。java的文件。
sop(str.endsWith(".java"));
//判断文件名中是否包含Demo。
sop(str.contains("Demo"));
}
public static void main(String[] args)
{
method_is();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
3转换
3.1将字符数组转成字符串
构造函数:String(charp[])
String(char[],offset,count):将字符数组中的一部分转成字符串。
静态方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data, intoffset, int count)
static String valueOf(char[]);
3.2将字符串转成字符数组
char[]toCharArray()
3.3将字节数组转成字符串
String(byte[])
String(byte[],offset,count):将字节数组中的一部分转成字符串。
3.4将字符串转成字节数组
Byte[]getBytes()
3.5将基本数据类型转成字符串。
StringvalueOf(int)
StringvalueOf(double)
特殊地方:字符串和字节数组在转换过程中,是可以指定编码表的。
代码示例:将字符数组中的某一段转换成字符串。
public class Lianxi
{
public static void method_tans()
{
char[] arr = {'a','b','c','d','e','f'};
String s = new String(arr,1,3);//3代表个数
sop("s="+s);
}
public static void main(String[] args)
{
method_tans();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
String replace(oldchar,newchar)
代码演示:
public class Lianxi
{
public static void method_replace()
{
String s = "hellojava";//一旦字符串被初始化,就不会被改变
//String s1 = s.replace("a", "n");//如果要替换的字符不存在,返回的是原来的字符串。
String s1 = s.replace("java","world");
sop("s="+s);
sop("s1="+s1);
}
public static void main(String[] args)
{
method_replace();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
5切割
String[]split(regex);
代码演示:将连在一起的三个名字“zhangsan,lisi,wangwu”进行分割
public class Lianxi
{
public static void method_split()
{
String s = "zhangsan,lisi,wanwu";
String[]arr = s.split(",");
for(int x=0; x<arr.length; x++)
{
sop(arr[x]);
}
}
public static void main(String[] args)
{
method_split();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
输出结果:
zhangsan
lisi
wanwu
6子串。获取字符串中的一部分。
String substring(begin)
String substring(begin,end)
代码演示:
public class Lianxi
{
public static void method_sub()
{
String s = "abcdef";
sop(s.substring(2));//从指定位置开始到结尾,如果角标不存在,会出现字符串角标越界异常
sop(s.substring(2,4));//包含头,不包含尾
sop(s.substring(0,s.length()));//获取整段字符串
}
public static void main(String[] args)
{
method_sub();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
7转换,去除空格,比较
7.1将字符串转成大写或者小写
StringtoUpperCase()
StringtoLowerCase()
7.2将字符串两端的空格去除。
String trim()
7.3对两个字符串进行自认顺序的比较
int comparetTo(string)
代码演示:
public class Lianxi
{
public static void method_7()
{
String s = " HelloJava ";
sop(s.toLowerCase());//全部转换成小写
sop(s.toUpperCase());//全部转换成大写
sop(s.trim());//去掉字符串两端的空格
String s1 = "acc";
String s2 = "aaa";
sop(s1.compareTo(s2));
}
public static void main(String[] args)
{
method_7();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
StringBuffer是字符串缓冲区。(是一个容器)
特点: 1长度是可以变化的。
2可以直接操作多个数据类型
3最终会通过toString方法变成字符串
它具备:C ceate U update R read D delete
1存储
StringBufferappend() 将指定数据作为参数添加到已有数据的结尾处
StringBufferinsert(index,数据) 可以将数据插入到指定index位置
代码演示:
public class Lianxi
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
sb.append("abc").append(true).append(34);
sb.insert(1,"qq");
sop(sb.toString());//打印结果为:aqqbctrue34
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
2删除
StringBufferdelete(start,end)删除缓冲区中指定部分的数据,包含头不包含尾
StringBufferdeleteCharAt(index)删除指定角标位置的字符
代码演示:
public class Lianxi
{
public static void main(String[] args)
{
method_del();
}
public static void method_del()
{
StringBuffer sb = new StringBuffer("abcde");
sb.delete(1, 3);
sop(sb.toString());//结果是:ade
sb.deleteCharAt(2);
sop(sb.toString());//结果为:abde
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
3获取
char charAt(intindex)//根据角标位置获取字符
int indexOf(Stringstr)//根据字符串获取位置
int lastIndexOf(String str)//返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索
int length()//返回字符串的长度
String substring(int start,int end)//用来截取一段字符串的方法
4修改(也可以称为替换)
StringBufferreplace(start,end,string)
void setCharAt(intindex, char ch)
代码示例:
public class Lianxi
{
public static void main(String[] args)
{
method_update();
}
public static void method_update()
{
StringBuffer sb = new StringBuffer("abcde");
sb.replace(1,4, "java");//替换某一部分字符
sb.setCharAt(2, 'k');//替换某一位字符
sop(sb.toString());
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
5反转
StringBufferreverse();
JDK1.5版本之后出现了StringBuilder.
StringBuffer是线程同步
StringBuilder是线程不同步
它的出现有三个因素:
1提高效率
2简化书写
3提高安全性
开发中建议使用StringBuilder
基本数据类型对象包装类
最常见的作用是:
用于基本数据类型和字符串类型之间做转换。
基本数据类型转成字符串。
基本数据类型+“”
基本数据类型.toString(基本数据类型);
如:Integer.toString(34);//将34整数变成“34”
字符串转成基本数据类型
xxx a = Xxx.parseXxx(String);
int a = Integer.parseInt(“123”);
double b = Double.parseDouble(“12.34”);
boolean b = Boolean.parseBoolean(“true”);
Integer I = new Integer(“123”);
int num = i.intValue();
十进制转成其它进制
toBinaryString();
toHexString();
toOctalString();
其它进制转成十进制
parseInt(string,radix);
基本数据类型对象包装类在JDK1.5版本后出现的新特性
代码示例:
public class Lianxi
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
//Integer x = new Integer(4);
Integer x = 4;//自动装箱。//相当于上面一行的代码(简写)
x= x+2;//x+2:x进行了自动拆箱,变成了int类型和2进行加法运算。
//再将和进行装箱赋给x
Integer m = 128;
Integer n = 128;
sop("m==n:"+(m==n));//结果为false,因为m和n超出了byte的数值范围
Integer a = 127;
Integer b = 127;
sop("a==b:"+(a==b));//结果为true。因为a和b指向了同一个Integer对象
//因为当数值在byte范围内时,对于新特性,如果该数值已经存在
//则不会再开辟新的空间
}
}