字符串常见方法总结:方法的作用、参数、返回值(构造方法可省略)

普通方法

1.字符串比较(equals)

当想要比较两个字符串内容是否相等时,必须使用equals()方法而不能使用==关系运算符

public class Main {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

从表面上看,两个字符串用==和equals()比较都为true,但实际上那只是Java编译器在编译期,会自动把所有相同的字符串当作一个对象放入常量池,s1和s2的引用地址是相同的,结构为true,换一种写法,==比较就会失败。

public class Main {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

2.字符串的搜索

使用indexof()方法可以从字符串的首部进行搜索,当前字符串中指定子字符串的下标,返回值为int类型,如果存在,则返回该子字符串的下标位置,如果不存在,则返回-1;

lastIndexOf()方法是从字符串的尾部进行搜索,返回值与indexOf()方法一致;

startsWith()和endsWith()方法是用于判断字符串是否以指定字符串开头或结尾,返回值为boolean类型;

contains()方法用于查找当前字符串中是否存在指定子字符串,返回值为boolean类型。

public static void main(String[] args) {
	String s1="just do IT do myelf do";
	int v1=s1.indexOf("do");
	System.out.println(v1);
	int v2=s1.indexOf("wo");
	System.out.println(v2);
	int v3=s1.indexOf("do");
	System.out.println(v3);
	int v4=s1.lastIndexOf("su");
	System.out.println(v4);
	System.out.println("------------");
	//从指定位置开始查找"子字符串"
	int v5=s1.indexOf("do",v1+1);
	System.out.println(v5);
}

//从指定位置开始查找"子字符串"
    int v5=s1.indexOf("do",v1+1);

3.截取子字符串

使用substring()方法可以从当前字符串中,截取指定下标区间的子字符串。

"大漠孤烟直".substring(2); // 孤烟直
"大漠孤烟直".substring(0,2); // 大漠

4.去除首尾空白字符

trim()方法可以移除字符串首尾空白字符,空白字符包括空格,\t,\r,\n;

String line="\t\t\r\na\tbc\t";
	//去除字符串"两端"空格换行(空白字符)
	line=line.trim();
	System.out.println(line);//a	bc
	System.out.println(line.length());//4

5.替换字符串

String str="渭城朝雨急急急,客舍青青柳色新";
	str=str.replace('雨', '雾');
	str=str.replace('青', '绿');
	str=str.replace('雨', '霞').replace('青','绿');
   str=str.replace("渭城", "渭城区");
   System.out.println(str);//渭城区朝雾急急急,客舍绿绿柳色新
	String str1="\"You\" (or \"Your\") sha1ll mean; an individu2al,or Legal En4tity exercisin9g. permissions granted by this License.";
	str1=str1.replaceAll("[\"\\,\\.\\;]", "#")//替换标点符号
			.replaceAll("[0-9]", "");//替换所有的数字
	System.out.println(str1);//#You# (or #Your#) shall mean# an individual#or Legal Entity exercising# permissions granted by this License#

6.分割字符串

要分割字符串,使用split()方法,

String line="北京,天津,广州,深圳,曹县";
		//切割字符串
		String[] cityArray=line.split("\\,");
		for(String city:cityArray) {
			System.out.println(city);//北京
//			天津
//			广州
//			深圳
//			曹县
		}
		System.out.println();
		//按照数字,切割字符串
		line="北京0天津1广州2深圳3曹县";
		cityArray=line.split("[0-9]");
		for(String city:cityArray) {
			System.out.println(city);北京
//			天津
//			广州
//			深圳
//			曹县
		}

静态方法

7.拼接字符串

拼接字符串使用静态方法join(),它用指定的字符串连接字符串数组

String s1="北京",s2="天津",s3="广州",s4="户县";
	String result1=String.join("#", s1,s2,s3);
	System.out.println(result1);//北京#天津#广州
	String[] cityArray= {"成都","南京","大连","西安","户县","曹县"};
	String result2=String.join("^_^", cityArray);
	System.out.println(result2);//成都^_^南京^_^大连^_^西安^_^户县^_^曹县
	String result3=s1.join("#", cityArray);//不推荐
	System.out.println(result3);//成都#南京#大连#西安#户县#曹县

8,格式化字符串

字符串提供了format()静态方法,可以传入其他参数,替换占位符,然后生成新的字符串

String result4=String.format("城市1:%s\n城市2:%s\n城市3:%s\n城市4:%s\n", s1,s2,s3,s4);
	System.out.println(result4);
//	城市1:北京
//	城市2:天津
//	城市3:广州
//	城市4:户县
	String result5=String.format("城市1:%s\n城市2:%s\n城市3:%s\n城市4:%s\n",cityArray);
	System.out.println(result5);
//	城市1:成都
//	城市2:南京
//	城市3:大连
//	城市4:西安

9类型转换

要把任意基本类型或引用类型转换为字符串,可以使用静态方法valueof().

int number=23944;
	//不推荐
	//String strNum=number+"";
	//推荐
	String strNum=String.valueOf(number);//使用String类的value方法进行转换
	if(strNum.indexOf("9")>=0) {
		System.out.println("包含9");
	}else {
		System.out.println("不包含9");
	}//包含9
String strNum="5600";
	//转换成整数
	int number1=Integer.parseInt(strNum);
	System.out.println(++number1);//5601
	//转换成double
	strNum="3.1415";
	double number2=Double.parseDouble(strNum);
	System.out.println(number2*10);//31.415000000000003
	//转换成boolean
	String strBool="true";
	boolean bool=Boolean.parseBoolean(strBool);
	System.out.println(bool);//true

	String s="23";

10转换为char[]字符数组

String和char[]类型可以相互转换

String s="巴山楚水麒麟昂蒂";
		//将字符串转换为字符数组
		char[] array=s.toCharArray();
		System.out.println(array);//巴山楚水麒麟昂蒂
		//将字符数组转换成字符串
		String content=new String(array);
		System.out.println(content);//巴山楚水麒麟昂蒂

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值