美文网首页java基础
java中String类的获取对象

java中String类的获取对象

作者: 小漫画ing | 来源:发表于2019-03-06 18:50 被阅读0次

    String类的获取对象

    package com.manman.cn;
    /*
     * String 类的获取功能
     * int length():获取字符串长度
     * char charAt(int index):获取指定索引位置的字符;
     * int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
     * int indexOf(int ch,int fromIndex):返回指定字符串在此字符串中从指定位置后出现的索引
     * int indexOf(String str,int fromIdex):返回指定字符串在此字符串中从指定位置后出现的索引
     * String substring(int start):截取字符串,默认到末尾
     * String substring(int start,int end):从指定位置开始,到指定位置结束截取字符串;
     */
    public class stringDemo {
        public static void main(String[] args) {
            String string="HelloWorld";
            // int length():获取字符串长度
            System.out.println("string的长度是:"+string.length());
            System.out.println("----------");
            //char charAt(int index):获取指定索引位置的字符;
            System.out.println("charAt:"+string.charAt(5));
            System.out.println("----------");
            //int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
            System.out.println("indexOf:"+string.indexOf("l"));
            System.out.println("----------");
            //int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
            System.out.println("indexOf:"+string.indexOf("World"));
            System.out.println("----------");
            //int indexOf(int ch,int fromIndex):返回指定字符串在此字符串中从指定位置后出现的索引
            System.out.println("indexOf:"+string.indexOf("l",4));
            System.out.println("----------");
            //String substring(int start):截取字符串,默认到末尾
            System.out.println("substring:"+string.substring(5));
            System.out.println("-------");
            //String substring(int start,int end):从指定位置开始,到指定位置结束截取字符串;
            System.out.println("substring:"+string.substring(2, 4));//包含开始,不包含结束
            
        
        }
    }
    
    

    练习

    1、要求:遍历获取字符串中的每一个字符

    package com.manman.cn;
    /*
     * 要求:遍历获取字符串中的每一个字符
     * 分析:
     * 如何拿到每一个字符
     * 如何知道个数
     */
    public class StringTest {
        public static void main(String[] args) {
            String string="HelloWorld";
            for (int i = 0; i < string.length(); i++) {
                System.out.print(string.charAt(i)+" ");
            }
        }
    }
    
    

    2、统计一个字符串中大写字母字符,小写字母字符,数字出现的次数(不考虑其他字符);

    package com.manman.cn;
    /*
     * 统计一个字符串中大写字母字符,小写字母字符,
     * 数字出现的次数(不考虑其他字符);
     * 分析:
     * 定义单个统计变量
     *  bigCount=0;
     *  smallCount=0;
     *  numberCount=0;
     * 遍历字符串,得到每一个字符串
     *  
     * 判断发字符串属于哪种类型
     *  大:bigCount++
     *  小:smallCount++
     *  数字:numberCount++
     */
    public class StringTest2 {
        public static void main(String[] args) {
            String string="HelloWorld123";
            int bigCount=0;
            int smallCount=0;
            int numberCount=0;
            for (int x = 0; x < string.length(); x++) {
                char c=string.charAt(x);
                if(c>='a' && c<='z'){
                    smallCount++;
                }else if(c>='A' &&  c<='Z'){
                    bigCount++;
                }else if(c>='0' && c<='9'){
                    numberCount++;
                }
            }
            System.out.println(bigCount);
            System.out.println(smallCount);
            System.out.println(numberCount);
        }
    }
    
    

    相关文章

      网友评论

        本文标题:java中String类的获取对象

        本文链接:https://www.haomeiwen.com/subject/yoztpqtx.html