一、基本语法:
---------------------String类------------------------
String类:
字符串是一个特殊的对象。
字符串一旦初始化就不可以被改变。(字符串最大特点)
String str1 = "abc" 与String str2 = new String("abc")有什么区别???
String str1 = "abc";str1是一个类类型变量,"abc"是一个对象。
String str2 = new String("abc");String类复写了object类中的equals方法,
该方法用于判断字符串是否相同。
str1和str2区别:
str1在内存中有一个对象。str2在内存中有两个对象。
String str3 = "abc" 问:
str1==str2.-----false 不同对象。
str1==str3.-----true 字符串已经在内存中常量池内存在,str3初始化时发现已经
存在,就不再独立开辟空间创建字符串。
String类适用于描述字符串事物。提供了多个方法对字符串进行操作。
常见的操作有哪些?
1、获取:
1.1字符串中包含的字符数,也就是字符串的长度。int length();
(数组的length是属性,字符串length();是方法)
1.2根据位置获取位置上的某个字符。char CharAt(int index);
当访问到字符串中不存在的角标时会发生StringIndexOfBoundsException。
1.3根据字符获取该字符在字符串中的位置。
int indexOf(int ch);返回的是ch在字符串中第一次出现的位置。
int indexOf(int ch,int fromIndex);从fromIndex指定位置开始,获取ch在字符串中出现的位置。
如果没有找到,返回值为-1。
int indexOf(String str);返回的是str在字符串中第一次出现的位置。
int indexOf(String str,int fromIndex);从fromIndex指定位置开始,获取str在字符串中出现的位置。
int lastIndexOf(int ch);反向索引。(从右往左查询,但是角标不变)
int lastIndexOf(int ch, int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
2、判断:
2.1字符串中是否包含某一个字符串。
boolean contains(String str);
特殊之处:indexOf(String str):可以索引str第一次出现位置,如果返回-1,表示该
str不在字符串中存在。所以,也可以用于判断对指定内容是否包含。
if(str.indexOf("aa"!=-1));
该方法既可以判断,又可以获取出现的位置。
2.2字符中是否有内容。
boolean isEmpty();原理就是判断长度是否为零。
2.3字符串是否是以指定内容开头。
boolean startWith(String str);
2.4字符串是否是以指定内容结尾。
boolean endsWith(String str);
2.5判断字符串内容是否相同。(复写了object中的equals方法)
boolean equals(String str);
2.6判断内容是否相同,并忽略大小写。
boolean equalsIgnoreCase();
3、类型转换:
3.1将字符数组转换成字符串
构造函数:String(char[])
String(char[],offset,count);将字符数组中的一部分转换成字符串
静态方法:static String copyValueOf(char[]);
static String copyValueOf(char[] data,int offset,int count);
static String ValueOf(char[]);
3.2将字符串转换成字符数组(****重点******)
char[] toCharArray();
3.3将字节数组转换成字符串
String(byte[])
String(byte[],offset,count);将字节数组中的一部分转
3.4将字符串转换成字节数组
byte[] getBytes();
3.5将基本数据类型转换成字符串
static String ValueOf(double);
static String valueOf(int);
eg:3+""; == String.value(3);
特殊:字符串和字节数组在转换过程中,是可以指定编码表的
4、替换:
String replace(char oldChar,char newChar);
(替换后产生新字符串,因为字符串初始化后就不会改变。如果替换不存在,返回的是原串)
5、切割:
String[] split(regex);
6、子串:获取字符串中的一部分。
String substring(begin);---从指定位置开始到结尾。
String substring(begin,end); ---包含头,不包含尾。
如果角标不存在,会出现字符串角标越界异常。
7、大小写转换、去除空格、比较:
7.1将字符串转换成大写或者小写
String toUpperCase();
String toLowerCase();
7.2将字符串两端的多余空格去除
String trim();
7.3对两个字符串进行自然顺序的比较
int compareTo(String); 返回值:相同为0;大于为正数;小于为负数。
代码示例一:
public class TestString {
public static void main(String[] args) {
String str = "123 abc 456abcrrrr";
String s1 = "abc";
String s2 = "Abc";
System.out.println("字符串长度:" + s1.length());
System.out.println("返回第1个位置的字符:" + s1.charAt(0));
System.out.println("查找字符q在字符串中出现的位置:" + str.indexOf('q'));
System.out.println("查找字符6在字符串中出现的位置:" + str.indexOf('6'));
System.out.println("查找子字符串 abc 在字符串中出现的位置:" + str.indexOf(s1));
System.out.println("查找子字符串 abc 在字符串中出现的位置:" + str.indexOf(s1,7));
System.out.println("判断两个字符串是否相等:" + s1.equals(s2)); //false
System.out.println("判断两个字符串是否相等(忽略大小写):" + s1.equalsIgnoreCase(s2)); //true
String s3 = "Hellh Whrld.";//不可变的常量
System.out.println("把字符串中h 替换成 o : " + s3.replace('h', 'o'));
System.out.println("s3: " + s3);
s3 = s3.replace('h', 'o');//重新赋值
String s4 = "abcderdfabcgdfaaaaa";
System.out.println("把字符串中abc 替换成 111 : " + s4.replaceAll("abc", "111"));
String s5 = "kari.zhang";
System.out.println("判断字符串是否以k开头: " + s5.startsWith("k"));//true
System.out.println("判断字符串是否以k结尾: " + s5.endsWith("k"));//false
String s6 = "aBcDEfg1234";
System.out.println("把字符串中小写字母转成大写:" + s6.toUpperCase());
System.out.println("把字符串中大写字母转成小写:" + s6.toLowerCase());
String s7 = "123456789abcdefgh";
System.out.println("字符串截取: " + s7.substring(6));
System.out.println("字符串截取: " + s7.substring(6, 12));//789abc
String s8 = " abcd fdfdf ";//trim, 去掉头尾空格
System.out.println("s8: " + s8);
System.out.println("去掉头尾空格:" + s8.trim());
}
}
代码示例二:
public class TestString2 {
public static void main(String[] args) {
String str = "a,b,c;d,e,g";
String[] s1 = str.split(";");//String[] s10 = new String[10];
for(int i=0; i<s1.length; i++) {
System.out.println(s1[i]);
}
int i1 = 10;
boolean b = true;
// String s2 = i1;
String s2 = String.valueOf(i1);
String s3 = String.valueOf(b);
System.out.println("s2:" + s2);
String s4 = 10 + "";
}
}
代码示例三:
public class TestString3 {
public static void main(String[] args) {
String str = "abdfdfer";
System.out.println("=====字符串转字节数组=====");
byte[] b = str.getBytes();
for(int i=0; i<b.length; i++) {
System.out.println(b[i]);
}
// System.out.println((int)'a');
System.out.println("=====字符串转字符数组=====");
char[] c = str.toCharArray();
for(int i=0; i<c.length; i++) {
System.out.println(c[i]);
}
}
}