A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王德升 中级黑马   /  2012-8-13 15:23  /  1756 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast.day01;

public class MySplitTest {

        /**
         * @param args
         */
        public static void main(String[] args) {

                // TODO Auto-generated method stub

                MySplit_1();

                /*
                thank
                hello
                byebye
                打印结果是这样的,为什么?难道不是存一个打印一个吗?难道是存了一个切割后完整字符串,再一起打印出来?
                */

                MySplit_2();
        }
       
        public static void MySplit_1(){
               
                String s = "thank,hello,byebye";
               
                String[] arr = s.split(",");//请问这里切换之后存进String[]是什么样子的,
               
                for(int x=0 ; x<arr.length; x++){
                       
                        System.out.println(arr[x]);//打印结果是怎么打印的  存一个打一个还是 ?
                }
        }
       
        public static void MySplit_2(){
               
                String s = "thankcc,hellocc,byebyecc";//当出现了重复切割后又是怎么切换的呢?但是API里面跟这个切的结果不一样?why???
               
                String[] arr = s.split("cc");
               
                for(int x=0 ; x<arr.length ; x++){
                       
                        System.out.println(arr[x]);
                }
        }

}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
打印结果很正常啊
   String[] arr = s.split("cc");得到的是["thank“,”hello“,”byebye“]
然后for循环进行遍历数组
挨个打印出来。

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
package cn.itcast.day01;

public class MySplitTest  {

         /**
          * @param args
          */
         public static void main(String[] args) {

                 // TODO Auto-generated method stub

                 MySplit_1();

                 /*
                 thank
                 hello
                 byebye
                 打印结果是这样的,为什么?难道不是存一个打印一个吗?难道是存了一个切割后完整字符串,再一起打印出来?
                 */

                 MySplit_2();
         }
         
         public static void MySplit_1(){
                 
                 String s = "thank,hello,byebye";
                 
                 String[] arr = s.split(",");//请问这里切换之后存进String[]是什么样子的,
                //不是切割完成以后以数组的形式保存在String数组中么???
                 
                 for(int x=0 ; x<arr.length; x++){
                        
                         System.out.println(arr[x]);//打印结果是怎么打印的  存一个打一个还是 ?
                         //都已经保存在数组中了,打印不就是提出来打印就行了么?
                         //你的疑问是什么呢?
                 }
         }
         
         public static void MySplit_2(){
                 
                 String s = "thankcc,hellocc,byebyecc";//当出现了重复切割后又是怎么切换的呢?但是API里面跟这个切的结果不一样?why???
                         //以"cc"切割和以","切割不是一样的么?能问的清楚点么?
                 
                 String[] arr = s.split("cc");
                 
                 for(int x=0 ; x<arr.length ; x++){
                        
                         System.out.println(arr[x]);
                 }
         }

}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 黎健东 于 2012-8-13 16:19 编辑

package com.heima.test;

public class MySplitTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        System.out.println("******1*******");
        MySplit_1();
        System.out.println("******2*******");
        MySplit_2();
        System.out.println("******3*******");
        MySplit_3();
        System.out.println("******4*******");
        MySplit_4();
        System.out.println("******5*******");
        MySplit_5();
    }

    public static void MySplit_1() {

        String s = "thank,hello,byebye";

        String[] arr = s.split(",");// 请问这里切换之后存进String[]是什么样子的,
        /*
         * 进行完上面这一步之后,String [] arr中的情况是这样子的 这个字符串数组arr里边,存进去的是被用","分离后的一个个字符串
         * 第一个被分离的是thank 第二个被分离的是hello 第三个被分离的是byebye 也就是说,相当于这样赋值了:String []
         * arr = {"thank","hello","byebye"};
         */


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

            System.out.println(arr[x]);// 打印结果是怎么打印的 存一个打一个还是 ?
            /*
             * 接下来打印输出字符串数组里边的元素 打印第一个元素:thank 打印第二个元素:hello 打印第三个元素:byebye
             */

        }
    }

    public static void MySplit_2() {

        String s = "thankcc,hellocc,byebyecc";// 当出现了重复切割后又是怎么切换的呢?但是API里面跟这个切的结果不一样?why???

        String[] arr = s.split("cc");
       /*
         * 进行完上面这一步之后,String [] arr中的情况是这样子的 这个字符串数组arr里边,存进去的是被用"cc"分离后的一个个字符串
         * 第一个被分离的是thank 第二个被分离的是,hellocc 第三个被分离的是,byebye 也就是说,相当于这样赋值了:String
         * [] arr = {"thank",",hellocc",",byebye"};
         */


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

            System.out.println(arr[x]);
            /*
             * 接下来打印输出字符串数组里边的元素 打印第一个元素:thank 打印第二个元素:hello 打印第三个元素:byebye
             */

        }
    }

    /*
     * 如果楼主的意思是想切割重复的cc并像第一个输出那样子 就应该在切割里边这样写
     */

    public static void MySplit_3() {

        String s = "thankcc,hellocc,byebyecc";

        String[] arr = s.split("cc,");

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

            System.out.println(arr[x]);
        }

    }

    /*
     * API中是这样子的:
     *
     * 例如,字符串 "boo:and:foo" 使用这些表达式可生成以下结果:
     *
     * Regex          结果
     * :          { "boo", "and", "foo" }
     * o          { "b", "", ":and:f" }
     */

    public static void MySplit_4() {

        String s = "boo:and:foo";

        String[] arr = s.split(":");

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

            System.out.println(arr[x]);
        }

    }
   
    public static void MySplit_5() {

        String s = "boo:and:foo";

        String[] arr = s.split("o");

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

            System.out.println(arr[x]);
        }

    }
   

}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
黎健东 发表于 2012-8-13 16:18
package com.heima.test;

public class MySplitTest {

也就是说 thank就是一个元素  我想成一个字符就是一个元素了 谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马