String 常用方法,String 追加方法,String 替换方法,String 截取方法,String 比较方法,String 转换方法

String 常用方法,String 追加方法,String 替换方法,String 截取方法,String 比较方法,String 转换方法

1.字符串长度-lenght()方法

String s = "青春无悔";
int a = s.lenght();
System.out.println("字符长度为:" + a);
//运行结果:字符长度为:4

2.比较两个字符串是否相等(区分大小写)-equals()方法

String s = "青春无悔";
String s1 = "青春无悔";
boolean ok = s.equals(s1);
System.out.println("判断结果为:" + ok);
//运行结果:判断结果为:true

String s = "abc";
String s1 = "aBc";
boolean ok = s.equals(s1);
System.out.println("判断结果为:" + ok);
//运行结果:判断结果为:false

3.比较两个字符串是否相等(忽略大小写)-equalsIgnoreCase()方法

String s = "青春无悔";
String s1 = "青春无悔";
boolean ok = s.equalsIgnoreCase(s1);
System.out.println("判断结果为:" + ok);
//运行结果:判断结果为:true

String s = "abc";
String s1 = "aBc";
boolean ok = s.equalsIgnoreCase(s1);
System.out.println("判断结果为:" + ok);
//运行结果:判断结果为:true

4.将字符串转换成小写-toLowerCase()方法

String s = "AbCDe";
String s1 = s.toLowerCase();
System.out.println("转换结果为:" + s1);
//运行结果:转换结果为:abcde

5.将字符串转换成大写-toUpperCase()方法

String s = "AbCDe";
String s1 = s.toUpperCase();
System.out.println("转换结果为:" + s1);
//运行结果:转换结果为:ABCDE

6.链接两个字符串-concat()方法或‘+’号

String s = "青春无悔";
String s1 = "青春无悔";
String ok = s.concat(s1);
System.out.println("链接结果为:" + ok);
//运行结果:链接结果为:青春无悔青春无悔

String s = "abc";
String s1 = "aBc";
String ok = (s+s1);
System.out.println("链接结果为:" + ok);
//运行结果:链接结果为:abcaBc

7.查找一个字符或(字符串)在某个字符串中第一次出现的位置-indexOf()

String s = "青春无悔";
int a = s.indexOf('春');
System.out.println("查找结果为:" + a);
//运行结果:查找结果为:1

8.查找一个字符或(字符串)在某个字符串中最后一次出现的位置-lastIndexOf()

String s = "青春无悔";
int a = s.lastIndexOf("无悔");
System.out.println("查找结果为:" + a);
//运行结果:查找结果为:2

9.截取到字符串从指定的位置开始到最后一个位置(包含该开始位置的字符)-substring()

String s = "青春无悔";
String a = s.substring(1);
System.out.println("截取结果为:" + a);
//运行结果:截取结果为:春无悔

10.提取两个下标之间的字符串-substring()

String s = "青春无悔";
String a = s.substring(1,3);
System.out.println("截取结果为:" + a);
//运行结果:截取结果为:春无

11.忽略字符串前后空格-trim()

String s = "         青   春   无   悔        ";
String a = s.trim();
System.out.println("截取结果为:" + a);
//运行结果:截取结果为:青   春   无   悔
//想忽略全部空格请使用替换方法

12.从字符串中取出指定位置的字符-charAt()

String s = "青春无悔";
char c = s.charAt(2);
System.out.println("提取结果为:" + c);
//运行结果:提取结果为:无

13.返回值为char数组类型,将字符串变成一个字符数组-toCharArray()

String s = "青春无悔";
char c[] = s.toCharArray(); 
System.out.print("结果为:");
for (int i = 0; i < c.length; i++){
	System.out.print(c[i]+",");
}
//运行结果:结果为:青,春,无,悔,

14.根据给定的正则表达式的匹配来拆分此字符串,形成一个新的String数组-split()

String s = "青春无悔";
String[] a = s.split("春");
System.out.print("结果为:");
for (int i = 0; i < a.length; i++){
	System.out.print(a[i]+",");
}
//运行结果:结果为:青,无悔

15.判断一个字符串里面是否包含指定的内容-contains()

String s = "青春无悔";
boolean ok = s.contains("春");
System.out.print("结果为:"+ok);
//运行结果:结果为:true

16.测试此字符串是否以指定的前缀开始-startsWith()

String s = "青春无悔";
boolean ok = s.startsWith("青");
System.out.print("结果为:"+ok);
//运行结果:结果为:true

17.测试此字符串是否以指定的后缀结束-endsWith()

String s = "青春无悔";
boolean ok =s.endsWith("无悔");
System.out.print("结果为:"+ok);
//运行结果:结果为:true

18.将第一次出现的某个内容替换成指定的内容-repalceFirst()

String s = "青春无悔";
String s1 = s.repalceFirst("青","春");
System.out.print("结果为:"+s1);
//运行结果:结果为:春春无悔

19.将字符串1中存在的字符串2删除b个或(按照字符串2进行拆分b次)-split()

String s = "青 春 无 悔 ";

String[] a = s.split(" ",2);
System.out.print("结果为:");
for (int i = 0; i < a.length; i++){ //数组长度为2
	System.out.print(a[i]+",");
}
//运行结果:结果为:青,春 无 悔 

20.转换为String类型-toString()或‘+“”’

int s = 1;

String a = s.toString();
String b = s+"";
System.out.print("结果为:"+(a+b));
//运行结果:结果为:11

21.将字符串2接到字符串1后面-append()

String s = "青春";
String s1 = "无悔";
String s2 = s.append(s1);
System.out.print("结果为:"+s2);
//运行结果:结果为:春春无悔

22.在指定的位置插入指定的值(参数可以是任何数据类型)-insert()

String s = "青春无悔";
String s1 = s.insert(2,"哈哈");
System.out.print("结果为:"+s1);
//运行结果:结果为:春春哈哈无悔

String s2 = s.insert(2,"哈哈哈哈",2);
System.out.print("结果为:"+s2);
//运行结果:结果为:春春哈哈无悔
//insert()功能是很强大的

22.将字符串中出现的指定字符替换掉(参数可以是任何数据类型)-replace()

String s = " 青 春 无 悔 ";
String s1 = s.replace(" ","哈");
System.out.print("结果为:"+s1);
//运行结果:结果为:哈春哈春哈无哈悔哈

String s2 = s.replace(" ","哈哈");
System.out.print("结果为:"+s2);
//运行结果:结果为:哈哈春哈哈春哈哈无哈哈悔哈哈
  • 10
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值