java中String类的练习

本文详细介绍了Java中String类的各种常用方法,包括字符串长度获取、字符检索、子串判断、转换操作、替换及切割等,并通过实例展示了这些方法的应用。

 String类:

 * 1获取:

 * 1-1 字符串的包含的字符数,也就是字符串的长度

 *  int length();

 * 1-2 根据位置获取字符在字符串中的位置

 *  char charAt(int index);

 * 1-3 根据字符获取在字符串中的位置

 *  int indexof(char a)

 *  int indexof(String substring int fromIndex)

 *  int lastIndexof(char a)倒着数字符串中第一个字符出现的角标

 *  2 判断

 *  2-1 字符串中是否含有一个字串;

 *  boolean contains(String);

 *  2-2 字符串是否以指定内容开头;

 *  boolean startsWith(String);

 *  2-3 字符串是否以指定内容结尾;

 *  boolean endsWith(String);

 *  2-4 字符串内容是否相同;

 *      boolean equals(String);

 *  2-5 字符串内容是否相同忽略大小写;

 *      boolean equalsIgnoreCase(String);

 *  3 转换

 *  3-1 将字符数组转换成字符串;

 *      构造函数(char[]);

 *      构造函数(char[],offset,count);

 *  3-2 将字符串转换成字符数组;

 *      char[] toCharArray();

 *  3-3 将基本数据类型转换成字符串;

 *      static String valueof(int);

 *      static String valueof(double);

 *      3+"";

 *  3-4 将字符串转换成大写或小写;

 *      String toUpperCase();

 *      String toLowerCase();

 *  4 替换

 *      String replace(oldChar,newChar);

 *  5 切割

 *      String[] split(String);

 *  6 字串

 *      获取字串的一部分;

 *      String substring(startindex);

 *      String substring(startindex,endindex);获得的是endindex-1;

 *   */

public class Demo1 {

    public static void sop(Object obj)

    {

        System.out.println(obj);

    }

    public static void main(String[] args) {

        String s1=new String("abcd,efsv");

        String s2="abcddedg";

        String s3="abcddedG";

        String s4=String.valueOf(23);

        sop(s1.length());

        sop(s1.charAt(3));

        sop(s1.indexOf('a'));

        sop(s1.indexOf("cd"));

        sop(s1.indexOf('d', 1));

        sop(s1.indexOf("de", 1));

        sop(s1.lastIndexOf('d'));

        sop(s1.contains("cde"));

        sop(s1.contains("a"));

        sop(s1.endsWith("fsv"));

        sop(s1.startsWith("abc"));

        sop(s2.equalsIgnoreCase(s3));

        char[] arr=s1.toCharArray();

        for(int x=0;x<arr.length;x++)

        {

            sop(arr[x]);

        }

        sop(new String(arr));

        sop(new String(arr,2,3));

        sop(s4);

        sop(s1.replace('a', 'd'));

        sop(s1.replace("a", "hello"));

        String[] arr1=s1.split("e");

        for(int x=0;x<arr1.length;x++)

        {

            sop(arr1[x]);

        }

        String s5="ddesgesd";

        sop(s5.substring(1));

        sop(s5.substring(1, 3));

        sop(s5.toUpperCase());

        String s6="SDFSFSF";

        sop(s6.toLowerCase());

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值