本文共 6475 字,大约阅读时间需要 21 分钟。
1. String类的概述
java.lang.String类代表字符串。类String中包含用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取字符串以及创建具有翻译为大写或小写的所有字符的字符串副本。 特点:String类是不可改变的,所以你一旦创建了String对象,那它的值就无法改变了。 2. 知识点汇总 知识点一:String类的创建方法一:实例化字符串最简单的方法如下:
//代码中遇到字符串常量时,这里的值是“abc”,编译器会使用该值创建一个String对象 String str = "abc";
方法二:用构造函数创建字符串
这种方法直接调用String类中的构造方法,在String类中存在以下构造方法:public String(String original)
所以用构造函数实例化字符串的方法如下:
String str = new String("abc");
下面分析一下两种方法的不同,先用两种方法实例化几个String对象:
//方法1的实例化String s1 = "abc";String s2 = "abc";String s3 = "abc";//方法2的实例化String s4 = new String("abc");String s5 = new String("abc");
不同之处在于:
先明白一个重要概念,即一个字符串就是一个String类的匿名对象,而匿名对象就是已经开辟了堆内存空间的并可以直接使用的对象。 对于方法一来说,实际上就是把一个在堆中开辟好的堆内存空间使用权给了str对象,实际上使用这种方式的一个好处就是,如果一个字符串已经被一个对象名称引用,则以后再有相同的字符串被别的对象声明引用时,就不会重新开辟空间。例如:pubblic class StringDemo{ public static void main(String args){ Stirng str1 = "hello"; Stirng str2 = "hello"; Stirng str3 = "hello"; System.out.println("str1==str2——>" + (str1==str2)); //true System.out.println("str1==str3——>" + (str1==str3)); //true System.out.println("str2==str3——>" + (str2==str3)); //true }}
从上面程序运行结果就可以看出,这三个字符串指向的堆内存地址空间都是同一个,这也就是String使用直接赋值的方式之后,只要是以后声明的字符串内容相同,则不会开辟新空间。
对于方法二来说,如果使用new关键字,不管如何都会开辟一个新的空间,但是此时此空间的内容还是hello,所以说上面的这个方法实际上开辟了两个空间,一个是存储对象str的栈内存空间,一个是存储字符串hello的堆内存空间。pubblic class StringDemo{ public static void main(String args){ String str = new String("hello"); }}
一个简单的图对比两个方法的不同:
(图画的有点难看,见谅一下!) 知识点二:String类的常用方法 在String类中提供了大量的操作方法,下面列举一些常用的方法:1.判断功能的方法
public boolean equals(Object anObject):将此字符串与指定对象进行比较public boolean equalsIgnoreCase(String anotherString):将此字符串与指定对象进行比较,忽略大小写
方法演示示例:
public class String_Demo01 { public static void main(String[] args) { // 创建字符串对象 String s1 = "hello"; String s2 = "hello"; String s3 = "HELLO"; // boolean equals(Object obj):比较字符串的内容是否相同 System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println(s1.equalsIgnoreCase(s2)); // true System.out.println(s1.equalsIgnoreCase(s3)); // true System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); }}
2.获取功能的方法
public int length () :返回此字符串的长度。public String concat (String str) :将指定的字符串连接到该字符串的末尾。public char charAt (int index) :返回指定索引处的 char值。public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
方法演示示例:
public class String_Demo02 { public static void main(String[] args) { //创建字符串对象 String s = "helloworld"; // int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length()); System.out.println("‐‐‐‐‐‐‐‐"); // String concat (String str):将将指定的字符串连接到该字符串的末尾. String s = "helloworld"; String s2 = s.concat("**hello itheima"); System.out.println(s2);// helloworld**hello itheima // char charAt(int index):获取指定索引处的字符 System.out.println(s.charAt(0)); System.out.println(s.charAt(1)); System.out.println("‐‐‐‐‐‐‐‐"); // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1 System.out.println(s.indexOf("l")); System.out.println(s.indexOf("owo")); System.out.println(s.indexOf("ak")); System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start):从start开始截取字符串到字符串结尾 System.out.println(s.substring(0)); System.out.println(s.substring(5)); System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start,int end):从start到end截取字符串。含start,不含end。 System.out.println(s.substring(0, s.length())); System.out.println(s.substring(3,8)); }}
3.转换功能的方法
public char[] toCharArray () :将此字符串转换为新的字符数组。public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
方法演示示例:
public class String_Demo03 { public static void main(String[] args) { //创建字符串对象 String s = "abcde"; // char[] toCharArray():把字符串转换为字符数组 char[] chs = s.toCharArray(); for(int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // byte[] getBytes ():把字符串转换为字节数组 byte[] bytes = s.getBytes(); for(int x = 0; x < bytes.length; x++) { System.out.println(bytes[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // 替换字母it为大写IT String str = "itcast itheima"; String replace = str.replace("it", "IT"); System.out.println(replace); // ITcast ITheima System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); }}
4.分割功能的方法
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
方法演示示例:
public class String_Demo03 { public static void main(String[] args) { //创建字符串对象 String s = "aa|bb|cc"; String[] strArray = s.split("|"); // ["aa","bb","cc"] for(int x = 0; x < strArray.length; x++) { System.out.println(strArray[x]); // aa bb cc } }}
3.小案例
拼接字符串:定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串,格式参照如下:[word1#word2#word3]public class StringTest1 { public static void main(String[] args) { //定义一个int类型的数组 int[] arr = { 1, 2, 3}; //调用方法 String s = arrayToString(arr); //输出结果 System.out.println("s:" + s); } /* * 写方法实现把数组中的元素按照指定的格式拼接成一个字符串 * 两个明确: * 返回值类型:String * 参数列表:int[] arr */ public static String arrayToString(int[] arr) { // 创建字符串s String s = new String("["); // 遍历数组,并拼接字符串 for (int x = 0; x < arr.length; x++) { if (x == arr.length ‐ 1) { s = s.concat(arr[x] + "]"); } else { s = s.concat(arr[x] + "#"); } } return s; }}
统计字符个数:键盘录入一个字符,统计字符串中大小写字母及数字字符个数
public class StringTest2 { public static void main(String[] args) { //键盘录入一个字符串数据 Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串数据:"); String s = sc.nextLine(); //定义三个统计变量,初始化值都是0 int bigCount = 0; int smallCount = 0; int numberCount = 0; //遍历字符串,得到每一个字符 for(int x=0; x='A'&&ch<='Z') { bigCount++; }else if(ch>='a'&&ch<='z') { smallCount++; }else if(ch>='0'&&ch<='9') { numberCount++; }else { System.out.println("该字符"+ch+"非法"); } } //输出结果 System.out.println("大写字符:"+bigCount+"个"); System.out.println("小写字符:"+smallCount+"个"); System.out.println("数字字符:"+numberCount+"个"); }}
上面内容也算是自己学习的一个总结,新手小白,错误之处望指正,感谢。
转载地址:http://lagki.baihongyu.com/