String用法3

String类的获取功能:
    int length()获取字符串的长度
    char charAt(int index)返回指定索引处的字符
    int indexOf(int ch)
    返回指定字符第一次出现的字符串内的索引。
    int indexOf(String str)
    返回指定子字符串第一次出现的字符串内的索引。。
    int indexOf(int ch,int fromIndex)
   返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。 
    int indexOf(int h,int 4)即从第四个位置开始找h知道找到位置,没有返回-1
    int indexOf(String str,int fromIndex)
    返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索。
    String substring(int start)
    从指定位置处截取字符串,包括开始截取的位置,截取到末尾,不存在就报错;
    String substring(int start,int end)
    截取字符串的一部分出来
    截取的串从start位置开始截取,截取到end-1的位置结束
    左闭右开 [,)  含头不含尾。
public class StringDemo7 {
    public static void main(String[] args) {
        String s = "helloworld";

        //int length() 获取字符串的长度
        System.out.println("字符串s的长度为:"+s.length());//10
        System.out.println("==================================");
        //char charAt(int index) 返回指定索引处的字符
        //0<=index<=length()-1
        System.out.println(s.charAt(4));//o
        System.out.println(s.charAt(0));//h
        //StringIndexOutOfBoundsException
//        System.out.println(s.charAt(100));
        System.out.println(s.charAt(9));//d

        System.out.println("==================================");
        //public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引。
        //需求:获取o在字符串中第一次出现的位置
        System.out.println(s.indexOf('o'));
        //如果此字符串中没有此类字符,则返回-1 。
        System.out.println(s.indexOf(97));

        System.out.println("==================================");
        //public int indexOf(String str)返回指定子字符串第一次出现的字符串内的索引。
        //返回的是小串的第一个字符在大串中出现的索引位置
        //owo
        System.out.println(s.indexOf("owo"));
        //如果大串中不存在小串,返回-1
        System.out.println(s.indexOf("owe"));
        System.out.println(s.indexOf("qwer"));

        System.out.println("==================================");
        // public int indexOf(int ch,int fromIndex)
        // 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
        //如果找到了,返回的是字符在整个字符串中的索引
        System.out.println(s.indexOf("l",4)); // 8
        System.out.println(s.indexOf("l",1000)); // -1
        System.out.println(s.indexOf("p",4)); // -1
        System.out.println(s.indexOf("p",1000)); // -1

        System.out.println("==================================");
        // int indexOf(String str,int fromIndex)
        System.out.println("==================================");
        //helloworld
        //String substring(int start) 从指定位置处截取字符串,包括开始截取的位置,截取到末尾
        //如果给的索引值不存在,报错
        System.out.println(s.substring(3)); // loworld
//        System.out.println(s.substring(100)); // StringIndexOutOfBoundsException

        System.out.println("==================================");
        //String substring(int start,int end)
        //截取字符串的一部分出来
        //截取的串从start位置开始截取,截取到end-1的位置结束
        //左闭右开 [,)  含头不含尾。
        System.out.println(s.substring(5,8)); // wor
//        System.out.println(s.substring(1,20)); //StringIndexOutOfBoundsException





    }
}
String类的转换功能:
       1. byte[] getBytes()
        public byte[] getBytes()使用平台的默认字符集将此String编码为字节序列,
        将结果存储到新的字节数组中。
       2.char[] toCharArray()将字符串转成字符数组
       3.static String valueOf(char[] chs)将字符数组转成字符串
       4.  static String valueOf(int i)将int类型的数据转成字符串
       5. String toLowerCase()将字符串中的内容全部转小写
       6. String toUpperCase()将字符串中的内容全部转大写
       7. String concat(String str)将小括号中str的字符串拼接到大字符串的后面
       8. public String[] split(String regex)
       String[] strings = s.split(regex:" ",limit:4);以空格为分割点,限制分割次数为4
        **********
public class StringDemo8 {
    public static void main(String[] args) {
        String s = "HelloWorLD";

        //public byte[] getBytes()使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
        //将字符串转成字节数组
        byte[] bytes = s.getBytes();
//        System.out.println(bytes);
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i]);
        }
        //72 101 108 108 111 87 111 114 76 68

        System.out.println();
        System.out.println("=================================");
        //char[] toCharArray()
        //将字符串转成字符数组
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }

        //增强for循环,后面上到集合的时候,我们会讲解
//        for(char c : chars){
//            System.out.println(c);
//        }
        System.out.println("=================================");
        //static String valueOf(char[] chs)
        //将字符数组转成字符串
        String s1 = String.valueOf(chars);
        System.out.println(s1);

        System.out.println("=================================");
        //static String valueOf(int i) 数据库
        //将int类型的数据转成字符串
        String s2 = String.valueOf(100); // 100 --> "100"
        System.out.println(s2); //100

        System.out.println("=================================");
        //String toLowerCase()
        //将字符串中的内容全部转小写
        String s3 = s.toLowerCase();
        System.out.println(s3); //helloworld

        System.out.println("=================================");
        //String toUpperCase()
        String s4 = s.toUpperCase();
        System.out.println(s4); //HELLOWORLD

        System.out.println("=================================");
        //String concat(String str)
        //将小括号中str的字符串拼接到大字符串的后面
        String s5 = s.concat("hadoop");
        System.out.println(s5);

        System.out.println("=================================");
        //public String[] split(String s)
        String s6 = "hello world hello java world";
        //需求:求出字符串中的每一个单词
        String[] strings = s6.split( " ");//以空格分割      
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }


    }
}

 

String类的其他功能:
        替换功能
            String replace(char old,char new)
            将新的字符串替换字符串中指定的所有字符,并返回一个替换后的字符串
            String replace(String old,String new)
            将字符串中旧的小串用新的小串替换,返回一个替换后的字符串
        去除字符串两空格
            String trim()
        按字典顺序比较两个字符串
            int compareTo(String str)
            int compareToIgnoreCase(String str)
public class StringDemo9 {
    public static void main(String[] args) {
        String s = "hellowrodldajavahadoopowollohelloowo";

        //String replace(char old,char new)
        //将新的字符串替换字符串中指定的所有字符,并返回一个替换后的字符串
        String s1 = s.replace('l', 'q');
        System.out.println(s);
        System.out.println(s1);

        System.out.println("====================================");
        //String replace(String old,String new)
        //将字符串中旧的小串用新的小串替换,返回一个替换后的字符串
        String s2 = s.replace("owo", "===");
        System.out.println(s);//hellowrodldajavahadoopowollohelloowo
        System.out.println(s2);//hellowrodldajavahadoop===llohello===
        System.out.println();
        String s3 = s.replace("owo", "@@@@");
        System.out.println(s);//hellowrodldajavahadoopowollohelloowo
        System.out.println(s3);//hellowrodldajavahadoop@@@@llohello@@@@
        System.out.println();
        //如果被替换的字符串不存在会是什么情况呢?返回的是原本的字符串
        String s4 = s.replace("qwer", "LOL");
        System.out.println(s);//hellowrodldajavahadoopowollohelloowo
        System.out.println(s4);//hellowrodldajavahadoopowollohelloowo

        System.out.println("====================================");
        //String trim() 去除字符串两边的若干个空格
        String s5 = "   hello world  ";
        System.out.println(s5);
        System.out.println(s5.trim());//hello world

        System.out.println("====================================");
        //int compareTo(String str)  //比较字符串是否相同,如果相同返回0
        String s6 = "hello"; // h的ASCII码值是 104
        String s7 = "hello";
        String s8 = "abc";   // a的ASCII码值是 97
        String s9 = "qwe";   // q的ASCII码值是 113
        System.out.println(s6.compareTo(s7)); // 0
        System.out.println(s6.compareTo(s8)); // 7
        System.out.println(s6.compareTo(s9)); // -9

        String s10 = "hel";
        System.out.println(s6.compareTo(s10)); // 2



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值