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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
        构造函数的方法:
                String(){}无参构造
                String(byte[] bytes)
                String(byte[] bytes,int index,int length)
                String(char[] value)
                String(char[] value,int index,int length)
                String(String str)
        获取String字符串的长度:
                public int length()
        String类中的方法:
                int indexof(int ch):返回指定字符在此字符串中第一次出现的索引。
                int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现的索引。
                char charAt(int index):返回字符串中index位置上的字符,其中index的取值范围是:(0~字符串长度-1)
                boolean endsWith(String suffix):判断此字符串中是否以指定字符串结尾
                boolean equals(Object anObject):将此字符串与指定的字符串相比较。--==比较的是地址,这个方法比较的是内容
                boolean isEmpty():当且仅当字符串长度为0时,返回true。
                boolean startsWith(String prefix):判断此字符串是否以指定的字符串开始。
                boolean contain(charSquence cs):判断此字符串中是否包含指定的字符序列。
                boolean toLowerCae():使用默认的语言环境的规则将String中所有字符都转换成小写
                boolean toUpperCase():使用默认的语言环境的规则将String中的所有字符全部大写
                String valueof(int i):返回int参数的字符串表现形式。
                char[] toCharArray():将此字符串转换为一个字符数组。
                String replace(charSquence oldstr,charSquence newstr):返回一个新的字符串,它是通过用newstr替换此字符
                                                                                                                        中出现的所有oldstr得到的
                String[] split(String regex):根据参数regex将原来的字符串分割为若干个子字符串。
                String substring(int beginIndex):返回一个新字符串,它包含字符串索引beginIndex后的所有字符。
                String substring(int beginIndex,int endIndex):返回一个新字符串,它包含字符串中从索引beginIndex到索引
                                                                                                          endIndex之间的所有字符串。
                CharSequence subSequence(int beginIndex,int endIndex):返回一个新的字符序列,它是此序列的一个子序列。
                String trim():返回一个新字符串,它去除了原字符串首尾的空格。
                int compareTo(String str):按字典顺序进行比较。
                String concat(String str):将指定字符串连接到给定字符串的结尾处

                字符表:由字符和数值组成的表格--
                                                                                'a':97;'b':98;'c':99;.....
                                                                                'A':65;'B':66;'C':67;.....
                                                                                '0':48;'1':49;'2':50;.....                                                                               
*/


class StringTest
{
        public static void main(String[] args)
        {
                System.out.println('a'+1);//98
                System.out.println('A'+1);//66
                System.out.println('0'+1);//49
                System.out.println(".................");

               
                String s="helloWorld1234";
                String s1="12345678";
                String s3="helloWorld1234";
                String s4="";

                //        获取String字符串的长度:
                //public int length()
                System.out.println(s.length());
                System.out.println(".................");

                //返回指定字符在此字符串中第一次出现的索引。
                System.out.println(s.indexOf('W'));
                System.out.println(".................");


                //int indexof(int ch);
                //注int indexof(int ch):这里的参数时int类型是因为int类型的数据也可以参与方法的运算,而char类型的参数则不可以。

                //返回指定字符在此字符串中最后一次出现的索引
                System.out.println(s.lastIndexOf('3'));
                System.out.println(".................");
               
                //返回字符串中index位置上的字符,其中index的取值范围是:(0~字符串长度-1)
                //返回整个字符串组的每个索引上的字符
                for (int i=0;i<s.length() ;i++ )
                {
                        System.out.println(s.charAt(i));
                }
                System.out.println(".................");


                //判断此字符串中是否以指定字符串结尾
                System.out.println(s.endsWith("1234"));
                System.out.println(".................");

                //将此字符串与指定的字符串相比较。
                System.out.println(s.equals(s1));
                System.out.println(s.equals(s3));
                System.out.println(s==s1);
                System.out.println(s==s3);
                System.out.println(".................");


                //当且仅当字符串长度为0时,返回true。
                System.out.println(s.isEmpty());
                System.out.println(s4.isEmpty());
                System.out.println(".................");
               
                //判断此字符串是否以指定的字符串开始。
                System.out.println(s.startsWith("hel"));
                System.out.println(".................");

                //判断此字符串中是否包含指定的字符序列。
                System.out.println(s.contains("d12"));
                System.out.println(".................");

                //使用默认的语言环境的规则将String中所有字符都转换成小写
                //使用默认的语言环境的规则将String中的所有字符全部大写
                System.out.println(s.toLowerCase());
                System.out.println(s.toUpperCase());
                System.out.println(".................");

                //返回int参数的字符串表现形式。
                System.out.println(s1.valueOf(65));
                System.out.println(".................");
       

                //将此字符串转换为一个字符数组。
                //输入字符串中的每个字符
                char[] ch=s.toCharArray();
                for(int i=0;i<s.length();i++)
                        {
                                System.out.print(ch[i]+",");
                        }
                        System.out.println(".................");

                //返回一个新的字符串,它是通过用newstr替换此字符中出现的所有oldstr得到的
                        System.out.println(s.replace("hel","123"));       
                        System.out.println(".................");
                       

                //返回一个新字符串,该字符串是指定索引index处后的所有字符串
                        System.out.println(s.substring(1));
                        System.out.println(".................");

                //返回一个新字符串,它包含字符串中从索引beginIndex到索引 endIndex之间的所有字符串。
                        System.out.println(s.substring(1,6));
                        System.out.println(".................");
                       

                //返回一个新字符串,它去除了原字符串首尾的空格。
                String s5="    aaaa      ";
                System.out.println(s.trim());
                //返回去除字符串中所有空格得到新字符串的方法
                String s6="aaaa bbbb cccc dddd";
                System.out.println(s6.replace(" ",""));
                System.out.println(".................");

                //将字符串数组剪切掉指定字符串分割成几个子字符串
                String s7="aaa3gbbb3gccc";
                String[] str=s7.split("3g");
                System.out.println(str.length);
                System.out.println(".................");

                //按字典顺序进行比较
                String str1="Asdfdg";
                String str2="Asdfdg";
                System.out.println(str1.compareTo(str2));
                System.out.println(".................");
        }
}
在这个小细节里,我学到了split()方法中值得注意的,原来length()和length是不同的,开始为了输出String【】类型的字符串数组,纠结了好一阵字。。

评分

参与人数 2技术分 +1 黑马币 +5 收起 理由
郑飞 + 5
MVP + 1

查看全部评分

1 个回复

正序浏览
总结的很细致,要是加上那几个案例就更好了。:lol
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马