java中String类的练习

来源:互联网 发布:淘宝上买官换机的技巧 编辑:程序博客网 时间:2024/04/28 05:52

 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());

    }

}

0 0
原创粉丝点击