java字符串训练与学习

package com.itheima;

import java.util.StringTokenizer;

public class 字符串训练学习 {
    public static void main(String[] args) {
        String s = new String("HelloWorld");
        System.out.println(s);
        System.out.println(s.length());
        //创建字符串
        //String s = new String("字符串")
        //用.length()方法来获取字符串长度

        //用String中equals的方法来判定两个字符串是否相同
        String str1 = new String("Hello");
        String str2 = new String("hello");
        String str3 = new String("Hello");
        System.out.println(str1.equals(str2)); //布尔类型 false
        System.out.println(str1.equals(str3)); //布尔类型 true

        //用stratsWith(s)方法查看字符串前缀是否为字符串s endsWith()方法是后缀
        String str4 = new String("own");
        String str5 = new String("os");
        String str6 = new String("n");
        System.out.println(str4.startsWith(str5));//布尔值
        System.out.println(str4.endsWith(str6));

        // 字符串调用regionMatches(int firstStart,String other,int ortherStart,int length)方法,
        // 从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,
        // 并将这个子串和参数other指定的一个子串进行比较,
        // 其中,other指定的子串是从参数othertStart指定的位置开始,从other中取长度为length的一个子串。
        // 如果两个子串相同该方法就返回true,否则返回false。
        String str7 = new String("Helloworld");
        boolean sign = str7.regionMatches(0, str1, 0, 5);
        System.out.println(sign);

        String strs1 = new String("hey,bro");
        String strs2 = new String("hey,bro");
        String strs3 = new String("Hey,bro");
        String strs4 = new String("heys");
        String strs6 = new String("hey");
        String strs5 = new String("o");
        boolean st = strs1.equals(strs2);
        boolean st1 = strs1.startsWith(strs4);
        boolean st2 = strs1.startsWith(strs6);
        boolean st3 = strs1.endsWith(strs5);
        System.out.println(st);
        System.out.println(st1);
        System.out.println(st2);
        System.out.println(st3);

        boolean op = strs1.regionMatches(0, strs4, 0, 4);
        System.out.println(op);
        System.out.println(strs1.length());

        /*字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s指定的字符串比较大小。
        如果当前字符串与s相同,该方法返回值0;
        如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值*/
        String er1 = new String("anle");
        String er2 = new String("aode");
        System.out.println(er1.compareTo(er2));
        //字符串的比较大小
        //字典序排列 就是按照26个字母的排列顺序 排列字符串,从比较每个字符串开头的字母,以此类推
        //例:apple , bol
        //就是先比较a和b在26字母里的排列顺序 a比b小 那就直接返回负值
        //例:azle , aode
        //开都字母都一样,直接比较第二个字母 z的排列顺序大于aode 返回正值


        /*字符串调用方法indexOf (String s)从当前字符串的头开始检索字符串s,并返回首次出现s的位置。
        如果没有检索到字符串s,该方法返回的值是-1。
        字符串调用indexOf(String s ,int startpoint)方法从当前字符串的startpoint位置处开始检索字符串s,并返回首次出现s的位置。
        如果没有检索到字符串s,该方法返回的值是-1。
        字符串调用lastIndexOf (String s)方法从当前字符串的头开始检索字符串s,并返回最后出现s的位置。
        如果没有检索到字符串s,该方法返回的值是-1。*/
        String se1 = new String("HelloWorld");
        String se2 = new String("ld");
        System.out.println(se1.indexOf(se2,6));
        System.out.println(se1.lastIndexOf("l"));

        /*字符串对象调用该方法获得一个当前字符串的子串,
        该子串是从当前字符串的startpoint处截取到字符串的末尾所得到的字符串。
        字符串对象调用substring(int start ,int end)方法获得一个当前字符串的子串,
        该子串是从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的字符。*/
        String sq1 = new String("HelloWorld");
        System.out.println(sq1.substring(5,7));
        //用的是索引截取字串


        /*字符串对象s调用该方法可以获得一个串对象,
        这个串对象是通过用参数newString指定的字符串替换s中由oldString指定字符串而得到的字符串。*/
        //例子 srt = "longl" , st2 = srt.replaceAll("l","L") -> LongL
        String sc1 = new String("WWWWorld");
        String sc2 = sc1.replaceAll("W","w");
        System.out.println(sc2);


        /*一个字符串s通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串。 */
        String co1 = new String(" hello ");
        System.out.println(co1);
        String co2 = co1.trim();
        System.out.println(co2);

        //用java.lang.一些数字的数据类型 可以将数字形式的字符串转换为相应类型的数字
        //整型 integer
        String number1 = new String("125");
        int num1 = Integer.parseInt(number1);
        System.out.println(num1);
        //浮点型
        String number2 = new String("125.254");
        //浮点型默认为double型
        double num2 = Double.parseDouble(number2);
        System.out.println(num2);
        //还有long short byte float 等等


        //toString(),一个对象通过调用该方法可以获得该对象的字符串表示。
        int sp1 = 5;
        String opw = Integer.toString(sp1);
        System.out.println(opw);


        //stringtokenizer 是字符分割器 可以使用nextoken()逐个获取字符串中的语言符号(单词)
        StringTokenizer tokenizer = new StringTokenizer("hello world , well come to java!",", ");
        //指定, 和 (空格) 为分隔符
        while(tokenizer.hasMoreTokens()){
            System.out.println(tokenizer.nextToken());
        }


        /*public void getChars(int start,int end,char c[],int offset )
        将字符串中从位置start到end-1位置上的字符拷贝的数组c中,并从数组c的offset处开始存放这些字符。
        需要注意的是,必须保证数组c能容纳下要被拷贝的字符。 */

        String kt1 = "hello,world";
        //创建数组的方法
        char[] chararray = new char[kt1.length()];
        kt1.getChars(0,5,chararray,0);
        System.out.println(chararray);
        //打印数组内容  数组的打印方法
        for (char ch:chararray){
            System.out.println(ch);
        }



       //public char[] toCharArray()   字符串对象调用该方法可以初始化一个字符数组,
        //该数组的长度与字符串的长度相等,并将字符串对象的全部字符拷贝到该数组中。
        String lp = "world";
        char[] charsarray = lp.toCharArray();
        for(char ch : charsarray){
            System.out.println(ch);
        }


        /*String(byte[],int offset,int length) 该构造方法使用平台默认的字符编码,
        用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。 */
        String mp1 = "hello,world";
        //先弄个数组
        char[] use = mp1.toCharArray();
        for(char h : use){
            System.out.println(h);
        }//现在是输出每个字符
        //现在组合字符
        String use1 = new String(use,0,5);
        System.out.println(use1);

        /*public byte[]  getBytes() 使用平台默认的字符编码,将当前字符串转化为一个字节数组*/
        //就是把每个字符都转换成ascii值然后存到数组中
        String ap = "hello,world";
        byte[] bit = ap.getBytes();
        for(byte cg : bit){
            System.out.println(cg);
        }





        /*String类创建的字符串对象是不可修改的,也就是说,
        String字符串不能修改、删除或替换字符串中的某个字符,
        即String对象一旦创建,那么实体是不可以再发生变化的*/

        /*StringBuffer类能创建可修改的字符串序列,
        也就是说,该类的对象的实体的内存空间可以自动的改变大小
        ,便于存放一个可变的字符序列。   (相当于字符串变量)*/

        StringBuffer a = new StringBuffer("helloworld");
        System.out.println(a);




        //正则表达式是含有一些具有特殊意义字符的字符串,这些特殊字符称作正则表达式中的元字符。
        //比如,“\\dhello”中的\\d就是有特殊意义的元字符,代表0到9中的任何一个。
        // 字符串“9hello”和“1hello”都是和正则表达式:“\\dhello”匹配的字符串之一。



        /*字符串对象调用
        public boolean matches(String regex)方法可以判断当前字符串对象是否和参数regex指的正则表达式匹配。*/

        String str = "Hello, World!";
        boolean isMatch = str.matches("Hello.*");
        System.out.println(isMatch);
        // 结果为 true,因为字符串以 "Hello" 开头

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值