java 的字符串处理相关函数

String类的成员函数

substring:从下标a(取),到下标b(不取)

String str="we are students and he is a techer";
System.out.println(str.substring(2,10));

equals:返回布尔值

str1.equals(str2)

equalsIgnoreCase():忽略字符大小写的字符串比较

str1.equalsIgnoreCase(str2)

字符串查找

public class test {
 
	public static void main(String[] args) {
		String str="我们一起数到6吧!";
		System.out.println(str.indexOf("一"));
                System.out.println(str.indexOf("6"));
                System.out.println(str.startsWith("我"));
                System.out.println(str.endsWith("!"));
	}
}
输出结果:

2
6
true
true

遍历字符串的每一位字符(String类不能直接取下标)


String s="abcde";
	    for(int i=0;i<s.length();i++)
        {
	    		char c=s.charAt(i);
	    		System.out.print(c+" ");//输出a b c d e,获取字符串
        }
 
	    String[] s1={"a","b","c","d","e"};
	    for(int i=0;i<s1.length;i++)
	    {
 System.out.print(s1[i]+" ");//输出a b c d e,获取字符串数组
	    }

indexof函数返回下标(找不到返回-1) startwith endwith 返回boolean

str.indexOf(','),表示在整个str字符串中检索!

int n3 = str.indexOf(',',n1);表示从n1开始(n1也算)检索!

split函数(返回String数组)

public static void main(String[] args) {
              String str="good good study, day day up";
	      String[] strarray=str.split(" ");
	      for (int i = 0; i < strarray.length; i++)
	          System.out.println(strarray[i]);
	}

String类静态函数


format按格式串构造数组

System.out.println(String.format("%02d:%02d:%02d",hh,mm,ss));

StringBuffer


public class Str {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer("good");
		sb.append(" study");
		System.out.println(sb);
		sb.reverse();
		System.out.println(sb);
		sb.delete(1,3);
		System.out.println(sb);
		sb.insert(3, "hello");
		System.out.println(sb);
		sb.insert(0, "11");
		System.out.println(sb);
		sb.replace(1, 2, "hello");
		System.out.println(sb);
	}
 
}
输出结果:
good study
yduts doog
yts doog
ytshello doog
11ytshello doog
1helloytshello doog

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wow_awsl_qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值