常用对象
String、StringBuffer和StringBuilder
String类的特点:字符串对象一旦被初始化就不会改变。
示例1:
public class TestDemo
{
public static void main(String[] args)
{
String s = "abc";
s="nba";
System.out.println("s="+s);//结果nba
}
}
分析:"abc"字符串对象并没有改变,只是引用变量s指向了新创建的字符串对象"nba"。
示例2:
public class TestDemo
{
public static void main(String[] args)
{
String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);//true
}
}
分析:字符串创建的时候,有一个字符串常量池,s1创建后,"abc"放入其中。s2创建的时候,"abc"已经存在于字符串常量池中,故引用变量s2直接指向了已经存在的"abc"字符串对象,故s1==s2为true。
示例3:
public class TestDemo
{
public static void main(String[] args)
{
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1==s2);//false
}
}
分析:s1创建后,是在字符串常量池创建了一个"abc"字符串对象。而s2是在堆内存中创建了另一个"abc"对象。所以,两个对象不是同一个对象。
示例4:
public class TestDemo
{
public static void main(String[] args)
{
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1.equals(s2));//true
}
}
分析:String类覆写了Object中的equals方法,建立了String类自己判断字符串对象是否属于相同的依据。只比较字符串内容,不比较地址。
Stirng类部分方法
获取字符串的个数 int length()
public class TestDemo
{
public static void main(String[] args)
{
String s = "abc";
System.out.println(s.length());//3
}
}
根据位置获取字符char charAt(int index);
public class TestDemo
{
public static void main(String[] args)
{
String s = "abcde";
System.out.println("char:"+s.charAt(2));//c
}
}
根据字符获取在字符串中所在的位置 int indexOf(int ch);
indexOf方法参数f类型为int是为了既可以支持字符,也可以支持字符在ASCII码中对应的数字。
从指定位置开始查找ch第一次出现的位置。
int indexOf(int ch,int fromIndex);
int indexOf(String str);
int indexOf(String str,int fromIndex);
根据字符串获取在字符串中第一次出现的位置。
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
示例:
public class TestDemo
{
public static void main(String[] args)
{
String s = "abcdae";
System.out.println( "index:" + s.indexOf('a' ));//0
System.out.println( "index:" + s.indexOf('k' ));//-1
System.out.println( "lastIndex:" + s.lastIndexOf('a' ));//4
}
}
获取字符串中的一部分字符串,也叫子串
String substring(int beginIndex,int endIndex);
String substring(int beginIndex);
示例:
public class TestDemo
{
public static void main(String[] args)
{
String s = "abcdae";
System.out.println(s.substring(2));//cdae
System.out.println(s.substring(2,4));//cd 包含首不包含尾
}
}
将字符串变成字符串数组(字符串的切割)
String[] split(String regex);涉及到正则表达式。
示例:
public class TestDemo
{
public static void main(String[] args)
{
String s= "张三,李四,王五";
String[] arr = s.split(",");
for(String str : arr)
{
System.out.println(str);
}//["张三","李四","王五"]
}
}
public class TestDemo
{
public static void main(String[] args)
{
String s= "张三.李四.王五";
String[] arr = s.split("\\.");//点在正则表达式中是特殊符号,需要转义。
for(String str : arr)
{
System.out.println(str);
}
}
}
将字符串变成字符数组
char[] toCharArray();
public class TestDemo
{
public static void main(String[] args)
{
String s= "张三.李四.王五";
char[] arr = s.toCharArray();
for(char str : arr)
{
System.out.println(str);
}
/*
张
三
.
李
四
.
王
五
*/
}
}
将字符串变成字节数组
byte[] getBytes();
public class TestDemo
{
public static void main(String[] args)
{
String s = "ab你" ;
byte [] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++)
{
System.out.println(bytes[i]);
}
/*
97
98
-60
-29
*/
}
}
将字符串中的字母转成大小写
String toUpperCase();大写
String toLowerCase();小写
public class TestDemo
{
public static void main(String[] args)
{
System.out.println( "Abc".toUpperCase());//ABC
System.out.println("AAb".toLowerCase());//aab
}
}
在字符串中的内容进行替换
String replace(char oldCh,char newCh);
String replace(String s1,String s2);
public class TestDemo
{
public static void main(String[] args)
{
String s1 = "java";
String s2 = s1.replace('a','o');//jovo
String s3 = s1.replace('k','o');//java //replace方法如果没有找到要替换的内容,则返回的还是原字符串。
}
replace方法如果没有找到要替换的内容,则返回的还是原字符串。
去除字符串两端空格
String trim();
public class TestDemo
{
public static void main(String[] args)
{
System.out.println( "-" + " ab c " .trim() + "-");//-ab c-
}
}
将字符串进行连接
String concat(String str);
public class TestDemo
{
public static void main(String[] args)
{
System.out.println( "abc".concat("kk" ));//abckk
System.out.println( "abc" + "kk" );//abckk
}
}
concat效果与"+"连接符效果一致,但是效果更高一些。
判断
两个字符串内容是否相同
boolean equals(Object obj);
boolean equalsIgnoreCase(String str);忽略大小写比较字符串内容。
public class TestDemo
{
public static void main(String[] args)
{
String s = "abc";
System.out.println(s.equalsIgnoreCase("ABC" ));//true
}
}
字符串中是否包含指定字符串
boolean contains(String str);
public class TestDemo
{
public static void main(String[] args)
{
String s = "abc";
System.out.println(s.contains( "bc"));//true
}
}
字符串是否以指定字符串开头,是否以指定字符串结尾
boolean startsWith(String str);
boolean endsWith(String str);
public class TestDemo
{
public static void main(String[] args)
{
String str = "ArrayDemo.java";
System.out.println(str.startsWith( "Array"));//true
System.out.println(str.endsWith( "java"));//true
}
}
比较
int compareTo(String str); 如果参数字符串等于此字符串,则返回值0;如果此字符串按字典顺序小于字符串参数,则返回一个小于0的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于0的值。
public class TestDemo
{
public static void main(String[] args)
{
System.out.println( "abc".compareTo("aqz" ));//-15
}
}
"abc"和"aqz"两个字符串比较,'a'和'a'相等,'b' - 'q' = -15,'c'和'z'也就没有必要比较。
返回字符串对象的规范化表示形式
String intern();
当调用intern方法时,如果池已经包含一个等于此String对象的字符串(用equals(Object)方法确定),则返回池中的字符串。否则,将此String对象添加到池中,并返回此String对象的引用。
public class TestDemo
{
public static void main(String[] args)
{
String s1 = new String("abc" );//堆中
String s2 = s1.intern();//如果字符串常量池中没有 把s1添加到字符串常量池中,并返回对象的引用
System.out.println(s1 == s2);//false
}
}
StringBuffer
StringBuffer:就是字符串缓冲区,用于存储数据的容器。
特点:
1. 长度是可变的。
2. 可以存储不同类型数据。
3. 最终要转成字符串进行使用。
P.S.
StringBuffer的字符串缓冲区初始容量为16个字符,其实质还是数组。
StringBuffer既然是一个容器对象,应该具备什么功能呢?
1、添加
StringBuffer append(data);
StringBuffer insert(index,data);
public class TestDemo
{
public static void main(String[] args)
{
//创建缓冲区对象
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = sb1.append(4);
System.out.println(sb1);//4
System.out.println(sb2);//4
System.out.println(sb1 == sb2);//true
System.out.println(sb1.append(5));//45
System.out.println(sb1.append(6).append(7));//4567
sb1.insert(1, "haha");//4haha567
}
}
2、删除
StringBuffer delete(int start,int end);包含头,不包含尾。
StringBuffer deleteCharAt(int index):删除指定位置的元素。
public class TestDemo
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd" );
sb.delete(1,3);
System.out.println(sb);//ad
}
}
3、查找
char charAt(int index);
int indexO(String str);
int lastIndexOf(String str);
4、修改
StringBuffer replace(int start,int end,String str);
void setCharAt(int index,char ch);
public class TestDemo
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd" );
sb.replace(1,3,"nba");
System.out.println(sb);//anbad
sb.setCharAt(2, 'q');
System.out.println(sb);//anqad
}
}
5、其他方法
void setLength(int newLength);设置字符序列的长度
public StringBuffer reverse();将字符序列用其反转形式取代
public class TestDemo
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd" );
sb.setLength(2);
System.out.println(sb);//ab
System.out.println( "len" + sb.length());//2
sb.setLength(10);
System.out.println(sb);//ab
System.out.println( "len" + sb.length());//10
System.out.println(sb.reverse());// ba
}
}
P.S.
1、使用setLength设置StringBuffer中字符序列的长度。
如果小于已有字符序列的长度,相当于清除缓冲区中的一部分内容。
如果大于已有字符序列的长度,相当于扩充缓冲区,扩充部门内容用空格字符填充。
2、当创建的StringBuffer内容长度大于16,将会新创建一个新数组,长度比旧数组要长。然后把就数组的内容拷贝到新的数组,超出旧数组长度范围的内容将会放在新数组现在内容的后面,也可以通过StringBuffer(int capacity);构造函数自己设置StringBuffer缓冲区长度。
StringBuilder
jdk1.5以后出现了功能和StringBuffer一模一样的对象,就是StringBuilder。
不同的是:
StringBuffer是线程同步的,通常用于多线程。
StringBuilder是线程不同步的,通常用于单线程,它的出现能够提高程序效率。
故StringBuilder多用于多个线程是不安全的,如果需要这样的同步,则建议使用StringBuffer。
P.S.
JDK一般升级目的:
1、简化书写。
2、提高效率。
3、增加安全性。
示例:将一个int数组变成字符串。
class StringBuilderTest
{
public static void main(String[] args)
{
int[] arr = {3,1,5,4,8};
String s = arrayToString(arr);
System.out.println(s);//[3,1,5,4,8]
}
}
public static String arrayToString(int[] arr)
{
StringBuilder sb = new StringBuilder();
sb.append( "[");
for(int i = 0; i < arr.length; i++)
{
if(i != arr.length - 1)
sb.append(arr[i]).append( ",");
else
sb.append(arr[i]).append( "]");
}
return sb.toString();
}
基本数据类型对象包装类
为了方便操作基本数据类型值,将其封装成了对象,在对象中定义了属性和行为丰富了该数据的操作。
用于描述该对象的类就称为基本数据类型对象包装类。
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
该包装对象主要用于基本类型和字符串之间的转换
基本类型-->字符串
1. 基本类型数值+""
2. 用String类中的静态方法valueOf(基本类型数值);
3. 基本类型.toString(基本数据类型); 如Integer.toString(34);//"34"
字符串-->基本类型
1. 使用包装类中的静态方法xxx parseXxx("xxx");
int parseInt(String str);
long parseLong(String str);
boolean parseBoolean(String str);
只有Character没有parse方法。
2. 如果字符串被Integer进行对象的封装,可使用另一个非静态的方法,intValue。将一个Integer对象转成基本数据类型值。
3. 用Integer的静态方法valueOf(String s);
整数具备不同的进制体现。
十进制-->其他进制方法:
String toBinaryString(int i);//转换成二进制
String toOctalString(int i);//转换成8进制
String toHexString(int i);//转换成16进制
String toString(int i,int radix);//转换成其它进制
其他进制-->十进制方法:
int parseInt(String s,int radix);
class WrapperDemo
{
public static void main(String[] args)
{
System.out.println(Integer.parseInt("110",2));//6
System.out.println(Integer.parseInt("3c",16));//60
}
}