【Java String类详解+案例分析】

String 类

String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例,java程序中所有的双引号字符串,都是String类的对象。

String str = “abc”;

String的一些主要的构造方法用于创建String类的对象

String str1 = ”hello“;

String str4 = ‘’hello‘’;

String(String Original),把字符串封装成字符串对象;
String str2 = new String(“hello”);

String(char[] value),把字符数组的数据封装成字符串对象;
char[] chs = {‘h’,‘e’,‘l’,‘l’,‘o’};   
  String str3 = new String(chs);

String类的创建与赋值

针对创建的String类的对象,通过构造方法创建的String对象,存放在java虚拟机的堆区中,堆区里存放的是字符串常量的地址,字符串常量存放在堆区的字符串常量池中;通过直接赋值所创建的对象直接是方法区中的常量池中的字符串常量。str1与str2显然是不 = = 的 ,( = =)比较的是字符串的地址,(str1==str4)是true因为在常量池中,可以允许有重复的数据。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZAIn2AMG-1659010516828)(F:\java基础\javase笔记\day13\2.png)]

String类中的方法

String类方法中有判断功能的方法:
返回值方法名简介
booleanequals(Object anObject)将此字符串与指定对象进行比较。
booleanequalsIgnoreCase(String anotherString)将此 String与其他 String比较
booleanendsWith(String suffix)测试此字符串是否以指定的后缀结尾。
booleanstartsWith(String prefix)测试此字符串是否以指定的前缀开头。
booleanstartsWith(String prefix, int toffset)测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
booleanisEmpty()返回 true如果,且仅当 length()0
booleancontains(CharSequence s)当且仅当此字符串包含指定的char值序列时才返回true。
booleancontentEquals(StringBuffer sb)将此字符串与指定的StringBuffer进行对比 。
intcompareTo(String anotherString)按字典顺序比较两个字符串。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0
static StringvalueOf(boolean b)方法中还可以是其他基本数据类型返回 boolean参数的字符串 boolean形式。
static StringcopyValueOf(char[] data)用法相当于 上面的valueof(char[])返回 char参数的字符串 char形式。

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "zuoshuaiqi";
		String s2 = "zuoshuai";
		String s3 = "";
		char[] arr = {'z','s','q'};
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("zuo");
		
		System.out.println(s1.equals(s2)); //false
		System.out.println(s1.equalsIgnoreCase(s2));//false
		
		System.out.println(s1.endsWith("qi")); //true
		System.out.println(s1.startsWith("zuo")); //true
		
		System.out.println(s1.startsWith("u", 5)); //true
		
		System.out.println(s3.isEmpty()); //true
		
		System.out.println(s1.contains("p")); //false
		
		System.out.println(s1.contentEquals(stringBuffer));//false
		
		System.out.println(s1.compareTo(s2)); //2
		
		System.out.println(String.valueOf("今天很开心"));// 今天很开心
		
		System.out.println(String.copyValueOf(arr)); //zsq
	}

}
String类方法中有转换功能的方法:
返回值方法名简介
char[]toCharArray()将此字符串转换为新的字符数组。
StringtoLowerCase()将所有在此字符 String使用默认语言环境的规则,以小写。
StringtoUpperCase()将所有在此字符 String使用默认语言环境的规则大写。
StringtoString()此对象(已经是字符串!)本身已被返回。

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "zuoshuaiqi";
		String s2 = "ZSQ";

		char[] arr = s1.toCharArray();
		System.out.println(arr); //zuoshuaiqi
		System.out.println(s1.toUpperCase()); //ZUOSHUAIQI
		System.out.println(s2.toLowerCase()); //zsq
 		System.out.println(s1.toString()); //zuoshuaiqi
	}

}
String类方法中去除空格和按照指定符号分割功能的方法:
返回值方法名简介
Stringtrim()返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
String[]split(String regex)将此字符串按照输入的字符分割。
String[]split(String regex, int limit)将这个字符串拆分为新的字符串数组,按照下标选择位置

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "  zuoshuaiqi  ";
		String s4 = "zuo!shuai!qi";
		
		System.out.println(s1.length()); //14
		String s2 = s1.trim(); 
		System.out.println(s2); //zuoshuaiqi
		System.out.println(s2.length()); //10
		
		String[] s3 = s4.split("!");
		for (int i = 0; i < s3.length; i++) {
			System.out.println(s3[i]);  //zuo
										//shuai
										//qi
		}
		String[] s5 = s4.split("!", 9);
		for (int i = 0; i < s5.length; i++) {
			System.out.println(s5[i]);
		}
	}

}

String类方法中截取字符串功能的方法:
返回值方法名简介
Stringsubstring(int beginIndex)返回一个字符串,从传入参数开始的位置到最后
Stringsubstring(int beginIndex, int endIndex)返回一个字符串,从beginIndex处开始截取到endIndex处结束,不包含下标为endIndex的字符;

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "zuoshuaiqi";
		System.out.println(s1.substring(3)); //shuaiqi
		System.out.println(s1.substring(3, 8)); //shuai
		
	}

}
String类方法中字符串拼接功能的方法:
返回值方法名简介
Stringconcat(String str)将指定的字符串连接到该字符串的末尾。

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "zuoshuaiqi";
		String s2 = "tsy";
		System.out.println(s1.concat(s2)); //zuoshuaiqitsy
	}

}
String类方法中获取字符串长度,字符在字符串出现的位置功能的方法:
返回值方法名简介
intlength()返回此字符串的长度。
charcharAt(int index)返回 char指定索引处的值。
intindexOf(int ch)返回指定字符第一次出现的字符串内的索引。
intindexOf(int ch, int fromIndex)返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
intindexOf(String str)返回指定子字符串第一次出现的字符串内的索引。
intindexOf(String str, int fromIndex)返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
intlastIndexOf(int ch)返回指定字符的最后一次出现的字符串中的索引。
用法与indexof用法一样这里不做解释

案例:

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = "zuoshuaiqinpi";
		String str3 = "uuuzouuzo";
		String str4 = "zo";
		int length = str1.length();
		System.out.println(length); // 13
		    
		//打印下标为2的字符
		System.out.println(str1.charAt(2)); // o

		System.out.println(str1.indexOf("i")); //7

		System.out.println(str1.indexOf("i",8));// 9

		System.out.println(str3.indexOf(str4));//3
		
		System.out.println(str1.lastIndexOf("i")); //12
		
		System.out.println(str1.lastIndexOf("i",8)); //7
		
	}

}
String类方法中字符串中字符替换功能的方法:
返回值方法名简介
Stringreplace(char oldChar, char newChar)用新的字符代替旧的字符
StringreplaceAll(String regex, String replacement)用新的字符串代替旧的字符串
StringreplaceFirst(String regex, String replacement)用新的字符串代替旧的字符串的第一个字符串
static Stringformat(String format, Object... args)使用指定的格式字符串和参数返回格式化的字符串。

案例:

public abstract class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "zuoshuaiqi";
		System.out.println(s1.replace("shuai", "chou")); //zuochouqi
		
		String s2 = "aaaa";
		String s3 = "bbb";
		//System.out.println(s2.replaceAll(s2, s3)); //bbb
		
		System.out.println(s2.replaceFirst("a", s3)); //bbbaaa
	}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值