Java学习第12天--String类

1.String类

String str = “abc”;和 String str = new String(“abc”);的区别(一个在常量池中,一个在堆中)

常量池的概念:方法区的一部分,字符串在常量池中保存一份

使用构造方法创建的对象都是在堆中,如果直接使用字符串常量创建对象:new String(String original)先在常量池中创建对象,然后在堆中创建对象;如果是其它构造方法创建的字符串,先在堆中创建出来,并在常量池保存一个副本(如果常量池中没有的话)

package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		//先在常量池中找“abc”,没有先创建“abc”,再在堆中赋值
		
		String str1 = "abc";
		String str2 = new String("abc");
		String str3 = "abc";
		//str1指向常量池的地址 ,str2指向堆中的地址
		System.out.println(str1 == str2);
		//System.out.println(str1 == str3);
		//str4、str5指向的变量都在常量池中
		//String str4 = "hello";
		//String str5 = "hello";
		//System.out.println(str4 == str5);
		
	}

}

2.String类的常用构造方法

(1)、String(char[] ch) :char型数组构造方法

public class StringDemos {

	public static void main(String[] args) {
		char[] chs = {'a','b','c'};
		String str = new String(chs);
		System.out.println(str);
	}

(2)、String(char[] ch,int offset,int count):用一个字符数组的一部分,从某个地方开始(offset),要多少个(count)

package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		char[] chs = {'a','b','c','d','e','f'};
		String str = new String(chs,2, 3);
		System.out.println(str);
	}

}

(3)、String(byte[] bys,int off,int len):给一个字节数组,从某一个位置开始(off),选定多长(len)

//将字节转换成字符数组,使用默认的解码,将字节数组解码成字符数组

package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		byte[] byt = {97,98,99,100,101};
		//将byt字节数组全部转换成字符数组
		//String str = new String(byt);
		//从byt字节数组索引2的位置开始,要3个,如果取得长度超出byt的长度,
		//会出现StringIndexOutOfBoundException(字符串索引越界异常)
		String str = new String(byt,2,3);
		System.out.println(str);
	}

}

以上方法不必刻意记忆,在Eclipse中,需要使用上述方法时,只需 new String “+” Alt +“/” 此时所有String类构造方法全部显示  

3、String类常见方法

(1)、int length(); --获取字符串中字符的个数,返回字符串长度,返回值类型int 

(2)、       char charAt(int index):返回指定索引处的char值,返回值类型char

package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		//返回字符串长度,返回值类型int型
		String str = "abcdez迎新";//中文占的字符个数也是1位
		System.out.println(str.length());
		
	}

}
package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		//返回指定索引处的char值
		String str = "abc寂寞";
		System.out.println(str.charAt(4));
		
	}

}

练习:统计大小写字符个数 

package com.nim03;

public class StringDemos {

	public static void main(String[] args) {
		// 练习计算大小写字符的个数,一个中文就是一个字符
		String str = "abcABCw中";
		int lower = 0;
		int upper = 0;
		int other = 0;
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
            // 判断
			if ('a' <= ch && ch <= 'z') {
				lower++;

			} else if ('A' <= ch && ch <= 'Z') {
				upper++;
			} else {
				other++;
			}
		}
		System.out.println("小写字母的个数是 :" + lower);
		System.out.println("大写字母的个数是 :" + upper);
		System.out.println("其它字符的个数是 :" + other);

	}

}

(3)、char[] toCharArray();把字符串转换成字符数组

调用某个方法时,不知道返回值类型,怎么调用呢?在Eclipse中,先写方法,然后按住Ctrl+2,在Eclipse的右下角会出现如下图片,然后按一下键盘上的小写l,会自动补全返回值类型和返回值名字,返回值名字可以自己更改。都用这种方法实现

package com.nim03;

public class StringDemo2 {

	public static void main(String[] args) {
		// toCharArray()
		String str = "abc中文";
		//自动补齐方法的返回值类型和变量名
		char[] chs = str.toCharArray();
		for (int i = 0; i < chs.length; i++) {
			System.out.println(chs[i]);
		}

	}

}
package com.nim03;

public class StringDemo2 {

	public static void main(String[] args) {
		// toCharArray()
		String str = "abcEDFG中文";
		//自动补齐方法的返回值类型和变量名
		char[] chs = str.toCharArray(); 
		int lower = 0;
		int upper = 0;
		int others = 0;
		for (int i = 0; i < chs.length; i++) {
			//用toCharArray方法改写统计大小写案例
			if('a' <= chs[i] && chs[i]<= 'z'){
				lower++;
			}else if('A'<= chs[i] && chs[i] <= 'Z'){
				upper++;
			}else{
				others++;
			}
		}
		System.out.println("小写字母的个数是:"+ lower);
		System.out.println("大写字母的个数是:"+ upper);
		System.out.println("其他字符的个数是:"+ others);

	}

}

 (4)、int indexOf(String str);返回子串在当前字符串中第一次出现的索引,如果在此字符串中没有此子串,返回-1

package com.nim03;

public class StringDemo3 {

	public static void main(String[] args) {
		// int indexOf(String str)
		String str = "acbabdcab";
		int index = str.indexOf("ab");
		System.out.println("子字符串ab在str字符串中第一次出现的索引是:"+index);
	}

}

(5)、boolean startsWith(String str); 以str开头返回true,不以str开头返回false

package com.nim03;

public class StringDemo3 {

	public static void main(String[] args) {
		String str = "acbabdcab";
		//startsWith(String str)
		boolean b = str.startsWith("acd");
		System.out.println(b);
	}

}

(6)、boolean endsWith(String str);以str结尾返回true,不以str结尾返回false

package com.nim03;

public class StringDemo3 {

	public static void main(String[] args) {
		String str = "acbabdcab";
		boolean bol = str.endsWith("ab");
		System.out.println(bol);
	}

}

(7)、String[] split(String reg);根据给定正则表达式拆 的匹配分字符串

package com.nim03;

public class StringDemo3 {

	public static void main(String[] args) {
		//split(String str)
		String str = "abcdesabdeab";
		String[] chs = str.split("b");
		for (int i = 0; i < chs.length; i++) {
			System.out.print(chs[i] + " ");
		}
	}

}
package com.nim03;

public class StringDemo3 {

	public static void main(String[] args) {
	// 统计由四个字母组成的单词,打印单词以及个数
		String str = "The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run."

				+ "This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped. "

				+ "In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target."
				+ "In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or "
				+ "enhancing the fundamental behavior of the class.";
		String[] words = str.split(" ");
		int count = 0;
		for (int i = 0; i < words.length; i++) {
			if (words[i].length() == 4) {
				System.out.println(words[i]);
				count++;
			}

		}
		System.out.println("字母个数是4的单词个数是:" + count);

	}

}

(8)、String substring(int beginIndex);从指定索引处返回字符串

             String substring(int beginIndex, int endIndex); [beginIndex,endIndex) 前包后不包

package com.nim03;

public class StringDemo4 {

	public static void main(String[] args) {
		// substring(int beginIndex)
		String str = "andbegkde";
		//String s = str.substring(3);
		//substring(int beginIndex, int endIndex)
		String s2 = str.substring(2, 5);//[2,5)
		System.out.println(s2);

	}

}

4、String类部分方法

“==”和equals方法的区别

==对于引用数据类型,比较的是地址值。equals方法首先比较的是地址值,若地址值不同在比较字符序列(首先判断两个字符串的串的长度是否相等,相等在逐个比较字符是否相等)

package com.nim03;

public class StringDemo4 {

	public static void main(String[] args) {
        //equals方法和==
		String str = "abc";
		String str2 = new String("bcd");
		System.out.println(str == str2);
		System.out.println(str.equals(str2));

	}

}

猜你喜欢

转载自blog.csdn.net/chengming320/article/details/81073320