java.lang.String类的使用
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有子类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间,下面简单的熟悉一下其常用的API
java.lang.String char charAt (int index) 返回index所指定的字符 String concat(String str) 将两字符串连接 boolean endsWith(String str) 测试字符串是否以str结尾 boolean equals(Object obj) 比较两对象 char[] getBytes 将字符串转换成字符数组返回 char[] getBytes(String str) 将指定的字符串转成制服数组返回 boolean startsWith(String str) 测试字符串是否以str开始 int length() 返回字符串的长度 String replace(char old ,char new) 将old用new替代 char[] toCharArray 将字符串转换成字符数组 String toLowerCase() 将字符串内的字符改写成小写 String toUpperCase() 将字符串内的字符改写成大写 String valueOf(Boolean b) 将布尔方法b的内容用字符串表示 String valueOf(char ch) 将字符ch的内容用字符串表示 String valueOf(int index) 将数字index的内容用字符串表示 String valueOf(long l) 将长整数字l的内容用字符串表示 String substring(int1,int2) 取出字符串内第int1位置到int2的字符串
1.构造方法//直接初始化String str = 'abc';//使用带参构造方法初始化char[] char = {’a’,’b’,’c’};String str1 = new String('abc');String str2 = new String(str);String str3 = new String(char);2.求字符串长度和某一位置字符
String str = new String('abcdef');int strlength = str.length();//strlength = 7char ch = str.charAt(4);//ch = e3.提取子串
用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数: 1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。 2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
String str1 = new String('abcdef');String str2 = str1.substring(2);//str2 = 'cdef'String str3 = str1.substring(2,5);//str3 = 'cde'4.字符串比较
1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnoreCase(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。
String str1 = new String('abc');String str2 = new String('ABC');int a = str1.compareTo(str2);//a>0int b = str1.compareToIgnoreCase(str2);//b=0boolean c = str1.equals(str2);//c=falseboolean d = str1.equalsIgnoreCase(str2);//d=true5.字符串链接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于'+'
String str = 'aa'.concat('bb').concat('cc');//相当于String str = 'aa'+'bb'+'cc';6.字符串中单个字符查找
1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
String str = 'I really miss you !';int a = str.indexOf(’a’);//a = 4int b = str.indexOf('really');//b = 2int c = str.indexOf('gg',2);//c = -1int d = str.lastIndexOf(’s’);//d = 6int e = str.lastIndexOf(’s’,7);//e = 77.大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
String str = new String('abCD');String str1 = str.toLowerCase();//str1 = 'abcd'String str2 = str.toUpperCase();//str2 = 'ABCD'8.字符串中字符的替换
1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
String str = 'asdzxcasd';String str1 = str.replace(’a’,’g’);//str1 = 'gsdzxcgsd'String str2 = str.replace('asd','fgh');//str2 = 'fghzxcfgh'String str3 = str.replaceFirst('asd','fgh');//str3 = 'fghzxcasd'String str4 = str.replaceAll('asd','fgh');//str4 = 'fghzxcfgh'9.其他方法
1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。
String str = ' a bc ';String str1 = str.trim();int a = str.length();//a = 6int b = str1.length();//b = 4
2)boolean statWith(String prefix)或boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
String str = 'abcdef';boolean a = str.statWith('ab');//a = trueboolean b = str.endWith('ef');//b = true
3)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
String str = 'abcdef';str.contains('ab');//truestr.contains('gh');//false
4)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
String str = 'abc def ghi';String[] str1 = str.split(' ');//str1[0] = 'abc';str1[1] = 'def';str1[2] = 'ghi';10.类型转换 字符串转基本类型 java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
public static byte parseByte(String s)public static short parseShort(String s)public static short parseInt(String s)public static long parseLong(String s)public static float parseFloat(String s)public static double parseDouble(String s)
int n = Integer.parseInt('12');float f = Float.parseFloat('12.34');double d = Double.parseDouble('1.124'); 基本类型转字符串 String类中提供了String valueOf()放法,用作基本类型转换为字符串类型
static String valueOf(char data[])static String valueOf(char data[], int offset, int count)static String valueOf(boolean b)static String valueOf(char c)static String valueOf(int i)static String valueOf(long l)static String valueOf(float f)static String valueOf(double d)
//将char ’8’ 转换为int 8String str = String.valueOf(’8’);int num = Integer.parseInt(str); 进制转换 使用Long类中的方法得到整数之间的各种进制转换的方法:
Long.toBinaryString(long l)//二进制Long.toOctalString(long l)//十进制Long.toHexString(long l)//十六进制Long.toString(long l, int p)//p作为任意进制
二、String特性这一部分介绍String的一些特性,涉及到字符串常量池、String.intern()以及我们经常遇到的“==”和“equals()”问题。 下面我们将通过不同的例子来解释:
例子1:
String a = 'Hello World!'; String b = 'Hello World!'; String c = new String('Hello World!'); String d = 'Hello'+' '+'World!';System.out.println(a == b);//trueSystem.out.println(a == c);//falseSystem.out.println(a == d);//true
我们应该明白:
首先String不属于8种基本数据类型,String是一个对象。 因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性。 在这里,我们先不谈堆,也不谈栈,只先简单引入常量池这个简单的概念。 常量池(constant pool)指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据。它包括了关于类、方法、接口等中的常量,也包括字符串常量。 Java会确保一个字符串常量只有一个拷贝。 因为例子中的a和b都是字符串常量,它们在编译期就被确定了,所以 a==b为true;而'Hello'和' '以及'World!'也都是字符串常量,当一个字符串由多个字符串常量连接而成时,它自己肯定也是字符串常量,所以d也同样在编译期就被解析为一个字符串常量,所以d也是常量池中'Hello World!'的一个引用。所以我们得出a==b==d; 用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String()创建的字符串不放入常量池中,它们有自己的地址空间。例子2:
String a = 'HelloWorld'; String b = new String('HelloWorld'); String c = 'Hello'+ new String('World'); System.out.println( a == b );//falseSystem.out.println( a == c );//falseSystem.out.println( b == c );//false
例子2中a还是常量池中”HelloWorld”的引用,b因为无法在编译期确定,所以是运行时创建的新对象”HelloWorld”的引用,c因为有后半部分new String(“World”)所以也无法在编译期确定,所以也是一个新创建对象”HelloWorld”的引用,明白了这些也就知道为何得出此结果了。 **PS: ** String.intern(): 再补充介绍一点:存在于.class文件中的常量池,在运行期被JVM装载,并且可以扩充。String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()方法时,Java查找常量池中是否有相同Unicode的字符串常量,如果有,则返回其的引用,如果没有,则在常量池中增加一个Unicode等于str的字符串并返回它的引用,看例3就清楚了。
例子3:
String a = 'Hello'; String b = new String('Hello'); String c = new String('Hello'); System.out.println( a == b );//falseSystem.out.println( “**********” ); b.intern(); c = c.intern(); //把常量池中'Hello'的引用赋给c System.out.println( a == b);//false虽然执行了b.intern()但没有赋值给bSystem.out.println( a == b.intern() );//true System.out.println( a == c ); //true
例子4:关于equals()和==: equals()是比较两个对象的值是否相等,这个对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true;而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的引用。
例子5:String是不可变的 : 这一说又要说很多,大家只要知道String的实例一旦生成就不会再改变了,比如说: String str=”aa”+”bb”+” “+”cc”; 就是有4个字符串常量,首先”aa”和”bb”生成了”aabb”存在内存中,后”aabb”又和” “ 生成 ”aabb “存在内存中,最后又和生成了”aabb cc”,并把这个字符串的地址赋给了str,就是因为String的“不可变”产生了很多临时变量,这也就是为什么建议用StringBuffer的原因了。
三、StringBuffer和StringBuiler我们对String、StringBuffer、StringBuiler先有一个简单的认识。String是不可变字符串,StringBuffer和StringBuilder是长度可变的字符串,区别是前者是线程安全的,后者是线程不安全的,同样后者的效率也会更高。
StringBuffer 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。
例如,如果 z 引用一个当前内容为 'start' 的字符串缓冲区对象,则此方法调用 z.append('le') 会使字符串缓冲区包含 'startle',而 z.insert(4, 'le') 将更改字符串缓冲区,使之包含 'starlet'。
通常,如果 sb 引用 StringBuilder 的一个实例,则 sb.append(x) 和 sb.insert(sb.length(), x) 具有相同的效果。
还有delete删除方法 deleteCharAt(int index) delete(int start ,int end)
以上就是java.lang.String类的使用的详细内容,更多关于java.lang.String的资料请关注好吧啦网其它相关文章!
相关文章: