String类
文章目录
需要注意的是java把String类定义为final类,因此用户不能扩展String类,即String类不可以有子类
构造String对象
String常量也是对象,使用双引号括起的字符序列,java把用户程序中的String常量放入常量池。因为String常量是对象,所以也有自己的引用和实体
可以使用String类声明对象并创建对象,例如:
String s = new String ("we are students");
String t = new String ("we are students");
对象变量s中存放着引用,表明自己的实体位置,即new运算符首先分配内存空间并在内存空间中放入字符序列,然后计算出引用,将引用赋值给字符串对象s后。
凡是new运算符构造出的对象都不在常量池中,尽管s和t的实体是相同的,都是’we are students’,但是二者的引用是不同的
另外,用户无法输出String对象的引用(即对存储位置):
System.out.println(s);
输出的是对象的实体,即字符串"we are students"
也可以用一个已创建的String对象创建另一个String对象
String s = new String("we are students");
String tom = new String(s);
System.out.println(s);// we are students
System.out.print(tom);// we are students
String类还有两个较常见的构造方法
-
String(char []) 用一个字符数组a创建一个String对象,例如
char a[] = {'j','a','v','a'}; String s = new String(a);
上述相当于
String s = new String("java");
-
String(char a[], int starIndex, int count) 提取字符数组a中的一部分字符创建一个String对象
starIndex——起始位置
count——个数
例子:
char a[] = {'零','一','二','三','四','五','六'}; String s = String (a, 0, 2);
相当于
String s = new String("零一")
-
引用String常量
String常量是对象,因此可以把String常量的引用赋值给一个String对象
String s1, s2; s1 = "你好"; s2 = "你好";
字符串的并置
String对象可以用“+”进行并置运算,即首尾相连接得到一个新的String对象
例子:
String you = "你";
String hi = "好";
String testOne;
testOne = you + hi; //你好
需要注意的是:参与并置运算的String对象,只要有一个变量,那么java就会在动态区存放所得到的新String对象的实体和引用。
import java.io.*;
class test
{
public static void main (String[] args) throws java.lang.Exception
{
String hello = "你好";
String testOne = "你" + "好";
System.out.println(hello == testOne); //true
System.out.println("你好" == testOne); //true
System.out.println("你好" == hello); //true
String you = "你";
String hi = "好";
String testTwo = you + hi;
System.out.println(hello == testOne);// false
}
}
testTwo存放着引用BCD5在动态区,其他字符串引用在常量池中,hello和testOne引用的是常量池的"你好",所以两者不能相等
String类的常用方法
-
public int length()
String类中的length()方法用来获取一个String对象的字符序列的长度
String s = "哈哈"; int n1; n1 = s.length(); // 2
-
public boolean equals(String s)
String对象调用equals(String s)方法比较当前String对象的字符序列是否与参数s指定的String对象的字符序列相同
String tom = new String ("天道酬勤"); String boy = new String ("知心朋友"); String jerry = new String ("天道酬勤"); System.out.println(tom == jerry);//false,存放的引用不同 System.out.println(tom.equals(boy));//false System.out.println(tom.equals(jerry));//true
-
public boolean startsWith(String s)、public boolean endWith(String s)方法
String对象调用startsWith(String s)方法,判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列
public static void main(String[] args) { String tom = new String ("天道酬勤"); String boy = new String ("知心朋友"); System.out.println(tom.startsWith("天"));// true }
String对象调用startsWith(String s)方法,判断当前String对象的字符序列后缀是否是参数指定的String对象s的字符序列
public static void main(String[] args) { String tom = new String ("天道酬勤"); String boy = new String ("知心朋友"); System.out.println(boy.endWith("友"));// true }
-
public int compareTo(String s)方法
String对象调用compareTo(String s)方法,按字典与参数指定的String对象s的字符序列比较大小
- 相同,返回0
- 小于s,返回负值
- 大于s,返回正值
public static void main(String[] args) { String str = "bbc"; String s1 = "ac"; String s2 = "ca"; System.out.println(str.compareTo(s1));//1 System.out.println(str.compareTo(s2));//-1 }
-
public boolean contains(String s)
String对象调用contains方法判断当前String对象的字符序列是否包含参数s的字符序列
String s = "students"; s.contains("stu");//true s.contains("w");//false
-
public int indexOf(String s)和public int lastIndexOf(String s)方法
不同点:
indexOf(String s)从当前str对象的字符序列0索引位置开始检索首次出现s的字符序列的位置,并返回该位置,如果没有检索到,该方法返回的值为-1
lastIndexOf(String s)从当前str对象的字符序列0索引位置开始检索最后一次出现s的字符序列的位置,并返回该位置,如果没有检索到,该方法返回的值是-1
新增知识点:indexOf(String s, int startpoint)方法是一个重载方法,参数startpoint的值用来指定检索的开始位置
String tom = "I am a good cat"; tom.indexOf("a"); //值为2 tom.indexOf("a", 7); //值为13 tom.indexOf("w", 2) //值为-1
-
public String substring (int startpoint)
字符串对象调用该方法获得一个新的String对象,新的String对象的字符序列是复制当前String对象的字符序列的startpointg位置至最后位置上的字符所得到的字符序列。
String对象调用substring(int start, int end)方法获得一个新的String对象,新的String对象的字符序列是复制当前String对象的字符序列的start 位置至end-1位置上的字符所得的字符序列
String tom = "我喜欢篮球"; String str = tom.substring(1, 3); //喜欢
-
public String trim()
String对象调用方法trim()得到一个新的String对象,这个新的Srting对象的字符序列是当前String对象的字符序列去掉前后空格的字符序列
字符串与基本数据的相互转换
字符串转化为基本类型
byte类型 | Byte.parseByte(String s) |
---|---|
short | Short.parseShort(String s) |
long | Long.parseLong(String s) |
float | Float.parseFloat(String s) |
double | Double.parseDouble(String s) |
基本类型转化为String类型
String.valueOf(byte n);
String.valueOf(int n);
String.valueOf(long n);
String.valueOf(float n);
String.valueOf(double n);
对象的字符串表示
Object类友一个public String toString()方法,一个对象通过调用该方法可以获得该对象的字符串表示。一个对象调用toString()方法返回的String对象的字符序列的一般形式为
创建对象的类的名字@对象的引用的字符串表示
字符串与字符数组、字节数组
字符串与字符数组
-
String类也提供了将String对象的字符序列放到数组的方法:public void getChar(int start, int end, char c[], int offset)
String对象调用getChars()方法将当前String对象的字符序列中的一部分字符复制到参数c指定的数组中,将字符序列中从start到end-1位置上的字符复制到数组c中,并从数组的offset处存放这些字符。需要主要提的是,必须保证数组c能容纳下要被复制的字符
public static void main(String[] args) { char [] a , c; String s = "今天是个好日子"; a = new char[4]; s.getChars(0,4, a, 0); System.out.println(a);//今天是个好日子 }
-
public char[] toCharArray()
将String对象的字符序列的全部字符存放在一个字符数组中的方法
public static void main(String[] args) { char [] a , c; c = "你真好看".toCharArray(); for (int i = 0; i < c.length; i++) { System.out.print(c[i]);// 你真好看 } }
字符串与字节数组
String类的构造方法String(byte[])用指定的字节数组构造一个String对象。
String(byte[], int offset, int length)构造方法用指定的字节数组的一部分,即从数组的起始位置offset开始取length个字节,构造一个String
String.getBytes(),直接将当前String对象的字符序列存放到字节数组中,并返回数组的引用
字符串的加密算法
使用一个String对象password的字符序列作为另一个String对象sourceString的字符序列进行加密,操作如下:
将password的字符序列放到一个字符数组中:
char [] p = password.toCharArray();
假如数组p的长度为n,那么就将待加密的sourceString的字符序列按顺序以n个字符为一组(最后一组中的字符个数可小于n),对每一组中的字符用数组p的对应字符做加法运算。
例如,某组中的n个字符是a0 、a1····an-1,那么按如下得到该组字符的加密结果:
c0 = (char)(a0 + p[0]) c1=(char)(a1 + p[1])
上述加密算法的解密算法就是是对密文做减法运算
password相等于密钥,用于加密scoureString
正则表达式及字符串的替换于分解
String对象调用public vollean matches(String regex)方法可以判断当前String对象的字符序列是否和参数regex指定的正则表达式匹配
元字符
元字符 | 在正则表达式中的写法 | 意义 |
---|---|---|
. | . | 代表任何一个字符 |
\d | \ \d | 代表0~9中的任何一个数字 |
\D | \ \D | 代表任何一个非数字字符 |
\s | \ \s | 代表空格类字符,’\t’,’\n’等 |
\S | \ \S | 代表非空个类字符 |
\p{Lower} | \ \p{Lower} | 小写字母 |
\p{Upper} | \ \p{Upper} | 大写字母 |
\p{Digit} | \ \p{Digit} | 数字字符 |
[a,b,c]:代表a、b、c中的任何一个
[^a,b,c]:代表除a、b、c中的任何字符
[a-zA-Z]:代表因为字母(包含大写和小写)中的任何一个
[a-d]:代表a~d中的任何一个
另外,中括号里允许嵌套中括号,可以进行交、并、差运算
[a-d[m-p]]:代表a~d 或者 m~p中的任何字符(并)
[a-z&&[def]]:代表d、e或f中的任何一个(交)
[a-f&& [ ^bc]]:代表a、d、e、f(差)
限定符
带限定修饰符的模式 | 意义 |
---|---|
X? | X出现0次或者1次 |
X* | X出现0次或者多次 |
X+ | X出现1次或者多次 |
X{n} | X恰好出现n次 |
X{n,} | X至少出现n次 |
X{n,m} | X出现n次至m次 |
XY | X的后缀是Y |
X|Y | X或Y |
字符串的替换
String对象调用public String replaceAll(String regex, String replacement)方法返回一个新的String对象,这个新的String对象的字符序列把当前String对象的字符序列中所有和参数regex匹配的子字符串序列,用参数replacement的字符序列替换后得到的字符序列
String str = "12hello567bird".replacreAll("[a-zA-z]+", "你好");
字符序列的分解
String类提供一个实用的方法,public String[] split(String regex),String对象调用该方法时,使用参数指定的正则表达式regex作为分割标记分解除当前String对象的字符序列中的单词,并将分解出的单词存放在String数组中
public static void main(String[] args) {
System.out.println("请输入文本");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String regex = "\\d"; //正则表达式
String words[] = str.split(regex);
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
}