黑马程序员技术交流社区

标题: 新学的 [打印本页]

作者: 路过的馒头    时间: 2019-3-31 22:12
标题: 新学的
package com.itheima;

public class FindTest {
    public static void main(String[] args) {

        // 获取某个字符串在另外一个字符串中第一次出现的索引的位置
        //                             参数:  String
        //                         返回值类型: int
        String str = new String("ABC");
        String ss1 = "C";
        int index01 = ss1.compareTo(str);//ss1字符串在str字符串第一次出现的索引的位置
        System.out.println("s1字符串在str字符串第一次出现的索引的位置: ");
        System.out.println(index01);//2
        System.out.println("*********************");


//                获得某个字符串指定索引位置的字符
        //参数: int
        //返回值类型: char
        String str2 = new String("ABCDEFG");
        char c = str2.charAt(2);
        System.out.println("字符串指定索引位置的字符: ");
        System.out.println(c);//C
        System.out.println("*********************");

//        把某个字符串从index索引开始截取到最后
        //参数: int
        //返回值类型: String
        String str3 = new String("ABCDE");
        String substring = str3.substring(3);
        System.out.println("字符串从index索引开始截取到最后: ");
        System.out.println(substring);//DE
        System.out.println("*********************");
//                把某个字符串索引start到索引end截取出来
                     //参数:int int
                     //返回值类型: String
        String str4 = new String("ABCDE");
        String substring1 = str4.substring(0, 4);
        System.out.println("字符串索引start到索引end截取出来: ");
        System.out.println(substring1);//ABCD包括开始 不包括结尾
        System.out.println("*********************");
        //        把字符串转化成字符数组
        //参数:  无
        //返回值类型:char[]
        String str5 = new String("ABCD");
        char[] chars = str5.toCharArray();
        System.out.println("把字符串转化成字符数组: ");
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i] + "  ");
        }
        System.out.println();
        System.out.println("*********************");
//
//                把字符串转化成字节数组
//                  参数: (无)
        //返回值类型: byte[]
        String str6 = new String("ABCDE");
        byte[] bytes = str6.getBytes();
        System.out.println("把字符串转化成字节数组: ");
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
        System.out.println();
        System.out.println("*********************");
//        获得字符串的长度
        //参数: 无
        //返回值类型: int
        String str7 = new String("ABCDEFG");
        int length = str7.length();
        System.out.println("字符串的长度");
        System.out.println(length);
        System.out.println("*********************");
//                获取某个字符在字符串中第一次出现的索引
//                  参数: char
        //返回值类型:int
        String str8 = new String("abcdef");
        int c1 = str8.indexOf('c');//2
        System.out.println("字符在字符串中第一次出现的索引:");
        System.out.println(c1);
        System.out.println("*********************");
//        把字符串中的字符'a'全部替换成'b'
//          参数:char/(int char)
        //返回值类型 :String
        String str9 = new String("aaabbcdef");
        String replace = str9.replace('a', 'b');
        System.out.println("字符串中的字符'a'全部替换成'b':");
        System.out.println(replace);
        System.out.println("*********************");
//        判断一个字符串(比如"sjasd")中是否包含某个字符串(比如"ja")
//              参数: String
//                返回值类型:boolean
        String str10 = new String("accabbadd");
        boolean acc = str10.contains("acc");
        System.out.println("是否包含某个字符串: ");
        System.out.println(acc);
        boolean abb = str10.contains("ABB");
        System.out.println(abb);
        System.out.println("*********************");

//        判断一个字符串是否以某个字符串开头
//              参数: String
//        返回值类型:boolean
        String str11 = new String("acabbbbb");
        boolean acb = str11.startsWith("aca");
        System.out.println("是否以某个字符串开头:");
        System.out.println(acb);
        boolean bbb = str11.startsWith("bbb");
        System.out.println(bbb);
        System.out.println("*********************");


//                判断一个字符串是否以某个字符串结尾
//                      参数: String
//                    返回值类型: boolean
        String str12 = new String("aaaaaccbac");
        boolean bac = str12.endsWith("bac");
        System.out.println("是否以某个字符串结尾:");
        System.out.println(bac);
        boolean ccc = str12.endsWith("ccc");
        System.out.println(ccc);
        System.out.println("*********************");

//        把一个字符串中所有的php替换成java
//              参数:String
                //返回值类型:String
        String str13 = new String("php===php===php++mysql");
        String s = str13.replaceAll("php", "java");
        System.out.println("把一个字符串中所有的php替换成java:");
        System.out.println(s);
        System.out.println("*********************");
//                把字符串中所有字母变大写
//              参数: 无
//                返回值类型:String
        String str14 = new String("aaaBBBcccDDDe");
        String s1 = str14.toUpperCase();
        System.out.println("把字符串中所有字母变大写:");
        System.out.println(s1);
        System.out.println("*********************");
//        把字符串中所有字母变小写
//  参数: 无
////                返回值类型:String
        String str15 = new String("aaaBBBcccDDDe");
        String s2 = str15.toLowerCase();
        System.out.println("把字符串中所有字母变小写:");
        System.out.println(s2);
        System.out.println("*********************");
//                怎么把一个字符数组的一部分转化为String类型
//              参数:char[]
//                返回值类型:String
        char[] arr1 = {'a','b','c','d','e'};

    }
}

老师传授的技巧 还是好用的!





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2