Java基础部分——关于String类

     Java提供了String类来创建操作字符串。 String类的值一旦创建便不能改变,每次更改都会new一个新的String对象。

、常用构造函数:

1、String()

初始化一个新创建的String对象,使其表示一个空字符序列。

2、String(byte[] bytes)

通过使用平台的默认字符集解码指定的byte 数组,构造一个新的 String。

3、String(byte[] bytes,String charsetName)

通过使用指定的charset 解码指定的 byte数组,构造一个新的String。

4、String(String original)

初始化一个新创建的String对象,创建的字符串即是传入参数字符串。

二、常用方法:

1、public int length()//返回该字符串的长度

2、public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

3、public String substring(int beginIndex), public String substring(int beginIndex, int endIndex) // 截取字符串

4、public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

5、public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。

6、public boolean equals(Object anotherObject)//比较当前字符串和参数字符串内容,在两个字符串内容相等的时候返回true,否则返回false。

7、public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

8、public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串

9、public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

10、public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1

11、public int indexOf(int ch/String str, int fromIndex)//此方法与上一个方法类似,区别在于该方法从fromIndex位置向后查找

12、public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。

13、public int lastIndexOf(int ch/String str, int fromIndex)//该方法与前一个方法类似,区别于该方法从fromIndex位置向前查找。

14、String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

15、contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。

16、String trim()//截去字符串两端的空格,但对于中间的空格不处理。

17、public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

18、public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。

19、public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

三、字符串与基本类型的转换

1、字符串转换为基本类型

1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)

6)public static double parseDouble(String s)

2、基本类型转换为字符串类型

1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)

四、面试常见题目

1、判断一个字符串在另一个串中出现几次?

public class StringDemo {

	
	public static void main(String args[]) {
		String str2 = "吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮";
		String strtemp = "葡萄";
		int count = count(str2,strtemp);
		System.out.println(count);
	}
	
	public static int count(String str1,String str2) {
		int index = 0;
		int count = 0;
		while((index = str1.indexOf(str2, index)) != -1) {
			index = index + str2.length();
			count++;
		}
		return count;
	}
}

2、计算字符串中每个字符出现个数

	public static void findCharCount(String str) {
		Map<Object,Integer> map = new HashMap<>();
		for(int i = 0; i<str.length(); i++) {
			char ch = str.charAt(i);
			if(map.containsKey(ch)) {
				Integer count = map.get(ch);
				count += 1;
				map.put(ch, count);
			}else {
				map.put(ch, 1);
			}
		}
		for(Object key : map.keySet()) {
			Integer value = map.get(key);
			System.out.println(key+"出现了"+value + "次");
		}
	}

猜你喜欢

转载自blog.csdn.net/m0_37657585/article/details/80715961