java string类及常用方法一

string  s2=""; 空串是字符串常量,同时是一个string类的对象 ,就可以调用string类中的方法

null是空常量,不能调用任何方法,否则出现空指针异常;可以赋值给任意的引用数据类型

string s3=null;由于string类会调用tostring方法就会报告异常错误

 

 

boolean equals(object obj);比较字符串的内容是否相同,区分大小写;

==: 基本类型比较内容,引用类型比较的是地址值;

 

1.

String  s1="abc";

String  s2="abc";

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

 

2.string s1= new string("abc");   这里创建了两个对象 :  一个在常量区  ,一个在堆中

3.string  s1="a"+"b"+"c";   常量优化机制   取值于常量区

  string  s2="abc";

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

 

4.

 

5.string 常见方法:int length():获取字符串的长度

                       char   charAt(int index):获取指定索引位置的字符      

                         int   indexof(int ch):返回指定字符在此字符串中第一次出现处的索引    

                          int   indexof(string  str):返回指定字符串在此字符串中第一次出现处的索引

                          int   indexof(int ch,int fromindex):返回指定字符在此字符串中从指定位置后第一次出现处的索引

                            int   indexof(string str,int fromindex):返回指定字符串在此字符串中从指定位置后第一次出现的索引

                             lastindexof                         

                            string  substring(int start):从指定位置开始截取字符串,默认到末尾

                            string  substring(int start,int end):从指定位置开始到指定位置结束截取字符串

 

                           byte[]  getbytes();把字符串转换为字节数组;

                            char[]  tocharArray():把字符串转换为字符数组;

                            static string valueof(char[] chs):把字符数组转成字符串;

                            static string valueof(int i):把int 类型的数据转成字符串。注意:string类的valueof方法可以把任意数据转成字符串

                            

                              string tolowercase():把字符串转成小写

                             string  touppercase():把字符串转成大写

                             string  concat(string str):把字符串拼接

 

 6.需求:把一个字符串的首字母转成大写,其余小写   (只用string方法实现 不考虑正则)

 

   

 

posted @ 2017-02-27 17:34  yimian  阅读(130)  评论(0编辑  收藏  举报
访问人数:AmazingCounters.com