java中String类常用方法(字符串截取)

一.String类

构造方法

new String()
new String(char[] arr)
new String(byte[] bs)

常用方法

1. 长度 length()

  String mun="123456";
        System.out.println(mun.length());//获取字符串长度

2. 拼接 str.concat(str2)

 String str1="123";
        String str2="456";
        String str3=str1.concat(str2);
        System.out.println(str3);//字符串的拼接str.concat(str2)

3. 格式化

 //方式一
System.out.printf("大家好,我的名字叫:%s,我今年:%d岁了,我的存款有:%f %n","曹操",36,999.99);
//方式二
String s = String.format("大家好,我的名字叫:%s,我今年:%d岁了,我的存款有:%f","曹操",36,999.99);
System.out.println(s);

4. charAt(index)返回指定索引处的字符

//        charAt(index)返回指定索引处的字符
        String num="0123456789";
        System.out.println(num.charAt(5));//输出num字符串中下标为5的字符

5. indexOf(str)返回指定字符串在此字符串中第一次出现处的索引

//        indexOf(str)返回指定字符串在此字符串中第一次出现处的索引

        String num1="abcdecdea";
        System.out.println(num1.indexOf("de"));

6. compareTo(str2)比较两个字符串

//        compareTo(str2)比较两个字符串
        System.out.println(num1.compareTo(num));

7. equals()和equalsIgnoreCase()

//        equals()和equalsIgnoreCase()
        System.out.println(num1.equals(num));
        System.out.println("num1".equals("num1"));//判断字符串是否相同
        System.out.println(num.equalsIgnoreCase(num));

8. getBytes()使用默认字符集将字符串变为字节数组,IO流中会用到

//        getBytes()使用默认字符集将字符串变为字节数组,IO流中会用到--char[]
        System.out.println(Arrays.toString( num.getBytes()));

9. toCharArray() 字符串转换为字符数组

//        toCharArray() 字符串转换为字符数组
        System.out.println(Arrays.toString( num.toCharArray()));

10. 截取:subString()

//        截取:subString()
        System.out.println(num.substring(0,2));//(开始下标,结束下标)

11. 转换成大小写toLowerCase() toUpperCase()

//        转换成大小写toLowerCase() toUpperCase()
        System.out.println(num1.toLowerCase());//小写
        System.out.println(num1.toUpperCase());//大写

12. 截取前后空白 trim()

//        截取前后空白 trim()
        String arr="12 34 56";
        System.out.println(arr.trim());

13. 替换:replace()

//        替换:replace()
        System.out.println(arr.replace("34","78"));

14. 分割:split()

//分割:split()
		STring arr="12 23 34 45"
        System.out.println(Arrays.toString(arr.split(" ")));//以空格分割字符串

15. 正则匹配 matches(String reg) 返回boolean

//        正则匹配 matches(String reg) 返回boolean
        System.out.println(arr.matches("12"));

案例

1.编写一个程序,让用户输入一个字符串,随即在控制台输出这个字符串中的 英文字母个数,数字个数。

实现方法一:


import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入:");
        String num=sc.next();
        char a2[]=num.toCharArray();
        int count1=0;//计数器  字母
        int count2=0;//计数器  数字
        for (int i = 0; i < num.length(); i++) {
            if(Character.isLetter(a2[i])){
                count1++;
            }else if(Character.isDigit(a2[i])){
                count2++;
            }
        }
        System.out.printf("字母:%d \n数字:%d \n字符:%d",count1,count2,(num.length()-count1-count2));
    }
}

实现方法二:

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String s=sc.next();
        int count1=0;//字符串
        int count2=0;//数字
        int count3=0;//其他
        for (int i = 0; i <s.length() ; i++) {
            char k=s.charAt(i);
            if(k>='a' && k<='z' || k>='A' && k<= 'Z'){
                count1++;
            }else if(k>='0' && k<='9'){
                count2++;
            }else{
                count3++;
            }
        }
        System.out.printf("字母:%d \n数字:%d\n字符:%d",count1,count2,count3);
    }
}

2.编写一个程序,让用户先后输入两个字符串,一个长的,一个短的,例如:“how do you do”和“do”;然后输出短的那个字符串在长的字符串中出现的次数。

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        String num1=sc.next();
        System.out.print("请输入第二个字符串:");
        String num2=sc.next();
        int len1=num1.length();//第一个字符串长度
        int len2=num2.length();//第二个字符串长度
        int count=0;
        for(int i=0;i<len1-len2;i++){
            String z=num1.substring(i,i+len2);
            if (z.equals(num2)) {
                count++;
            }
        }
        System.out.println(num2+"在"+num1+"中,出现:"+count+"次");
    }
}
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值