Java面向对象-- String 类 常用方法及基本使用

首先来学习基本使用Jdk api chm文档:

点击 左上角-显示:

 

1, char chartAt(int index) 返回指定索引处的char值

这里的index 是从0开始的;

package com.xuyigang1234.chp02.string;

public class Demo01 {
    public static void main(String[] args) {
        String name="小白";
        char name1=name.charAt(1);
        System.out.println("name1为:"+name1);
        String aa="我爱学习";
        for(int i=0;i<aa.length();i++) {
            System.out.println(aa.charAt(i)+" ");
        }
        
    }

}

2,int length() 返回字符串的长度;

3,int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。

package com.java1234.chap03.sec08;
 
public class Demo06 {
 
    public static void main(String[] args) {
        // indexOf方法使用实例
        String str="abcdefghijdklmoqprstuds";
        System.out.println("d在字符串str中第一次出现的索引位置:"+str.indexOf('d'));
        System.out.println("d在字符串str中第一次出现的索引位置,从索引4位置开始:"+str.indexOf('d',4));
    }
}

4,String substring(int beginIndex)   返回一个新的字符串,它是此字符串的一个子字符串。

package com.java1234.chap03.sec08;
 
public class Demo07 {
 
    public static void main(String[] args) {
        // substring方式读取
        String str="不开心每一天";
        String str2="不开心每一天,不可能";
        String newStr=str.substring(1);
        System.out.println(newStr);
        String newStr2=str2.substring(1, 6);
        System.out.println(newStr2);
    }
}

5,public String toUpperCase()  String 中的所有字符都转换为大写

package com.java1234.chap03.sec08;
 
public class Demo08 {
 
    public static void main(String[] args) {
        String str="I'm a boy!";
        String upStr=str.toUpperCase(); // 转成大写
        System.out.println(upStr);
        String lowerStr=upStr.toLowerCase(); // 转成小写
        System.out.println(lowerStr);
    }
}

 6. 综合练习

package com.xuyigang1234.chp02.string;
/**
 * “ aB2322 3 &*( s2 ”指定字符串,要求去掉前后空格,然后分别统计其中英文字符,
空格,数字和其他字符的个数;
 * */
public class Demo03 {
    public static void main(String[] args) {
        String str = " aB2322 3 &*( s2 ";
        //去掉前后空格
        String newStr=str.trim();
        int yingWen=0;
        int kongGe=0;
        int shuZi=0;
        int qiTa=0;        
        for(int i=0;i<newStr.length();i++) {
            char c=newStr.charAt(i);
            if(c>='a'&&c<='z'||c>='A'&&c<='Z') {
                yingWen++;
            }else if(c>='0'&&c<='9') {
                shuZi++;
            }else if(c==' ') {
                kongGe++;
            }else {
                qiTa++;
            }
        }
        System.out.println("字母的个数为:"+yingWen);
        System.out.println("空格的个数为:"+kongGe);
        System.out.println("数组的个数为:"+shuZi);
        System.out.println("其他的个数为:"+qiTa);
        
    }
}

输出:

字母的个数为:3
空格的个数为:3
数组的个数为:6
其他的个数为:3
7.字符串反转

package com.xuyigang1234.chp02.string;
/**
 * 字符串反转,“abcdefg”,通过编程得到一个新的字符串“gfedcba”
 * */
public class Demo04 {
    public static void main(String[] args) {
        String str="abcdefg";
        String newStr=""; //声明一个空的新字符串
        for(int i=str.length()-1;i>=0;i--) {
             newStr+=str.charAt(i);
        }
        System.out.println("新的字符串为: "+newStr);
    }

}

8、字符串转换长数组

package com.xuyigang1234.chp02.string;
/**
 * 将字符串“1,3,5,7,9”,转换成数组,数组元素是整数元素 1,3,5,7,9
 * */
public class Demo5 {
    public static void main(String[] args) {
        String str = "1,3,5,7,9";
        String newStr=""; //声明一个新的字符串
        int shuZiCount=0; //声明数字个数统计,并默认为零
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)!=',') {
                newStr +=str.charAt(i);
                shuZiCount++;
            }
        }
        int arr[] = new int[shuZiCount]; // 声明动态数组
        for(int j=0;j<arr.length;j++) {
            arr[j]=Integer.parseInt(newStr.charAt(j)+""); //int的包装类的parseInt方法 把字符串转成int类型
        }
        for(int a:arr) {
            System.out.print(a+" ");
        }
    }

}
原文地址:https://www.cnblogs.com/xyg-zyx/p/9824442.html