String

String

1.String s="abc"=>"abc"是一个String对象,创建之后将不能修改

2.各种方法:

  获取

1 * 1.获取长度:public int length();
2 * 2.获取某位置字符:public char charAt(int index);
3 * 3.根据字符ascii获取位置:public int indexOf(int ch);public int indexOf(int ch,int fromIndex);
4 * 4.获取第一次出现str的位置:public int indexOf(String str);public int indexOf(String str,int fromIndex);
5 
6 * 5.获取子串:public String subString(int begin);public String subString(int begin,int end);
7 * 6.反向获取,lastIndexOf(..);一个道理
View Code

  判断

* 1.判断字符串时候包含某一个子串:public boolean contains(CharSequence s);
* 2.字符串时候为空:public boolean isEmpty();
* 3.是否以指定内容开头:public boolean startsWith(String prefix);
* 4.是否以指定内容结尾:public boolean endsWith(String suffix);
* 5.判断字符串时候相同:复写了Object的equals()方法public boolean equals(Object anObject);
* 6.判断字符串时候相同,忽略大小写:public boolean equalsIgnoreCase(String anotherString);
View Code

  转换

* 1.转换字符数组成字符串:构造public String(char[] value,..);
* public static String copyValueOf(char[] data,int offset,int count)
*     public static String valueOf(char[] data)
* 2.字符串转换成字符数组:public char[] toCharArray()
* 3.字节数组转换成字符串:同1,参数改为byte[]
* 4.字符串转换成字节数组:
* 5.大小写转换:public String toUpperCase(..);public String toLowerCase(..);
View Code

  替换

*1.替换字符public String replace(char oldchar,char newchar)
* 替换一堆public String replace(CharSequence target,CharSequence replacement)
View Code

  比较

*1.比较字符串大小(按ascii):public int compareTo(String another);相同为0
*2.或略大小写比较:public int compareToIgnoreCase(String another);
*3.比较两字符串是否相同:public boolean equals(Object anObject);
*4.忽略大小写比:public boolean equalsIgnoreCase(String another);
View Code

  切割字符串: public String[] split(String regex)

  去除首尾空格:public String trim() 

 示例:能快速参考手册即可

  1 package String;
  2 
  3 /**
  4  * 
  5  *@author john
  6  */
  7 public class StringTest {
  8     public static void main(String[] args) {
  9 //        StringTest.method_get();
 10 //        StringTest.method_is();
 11 //        StringTest.method_trans();
 12 //        StringTest.method_replace();
 13 //        StringTest.method_split();
 14 //        StringTest.method_sub();
 15 //        StringTest.method_deBlank();
 16 //        StringTest.method_equals();
 17     }
 18 
 19     /**
 20      * 1.获取长度:public int length();</br>
 21      * 2.获取某位置字符:public char charAt(int index);</br>
 22      * 3.根据字符ascii获取位置:public int indexOf(int ch);public int indexOf(int ch,int fromIndex);</br>
 23      * 4.获取第一次出现str的位置:public int indexOf(String str);public int indexOf(String str,int fromIndex);</br>
 24      * 5.反向获取,lastIndexOf(..);一个道理
 25      */
 26     public static void method_get(){
 27         String s="abcdaabb";
 28         sop(s+":"+"长度:"+s.length());
 29         sop(s+":"+"charAt(1):"+s.charAt(1));
 30         sop(s+":"+"indexOf(97):"+s.indexOf(97));
 31         sop(s+":"+"indexOf(97,1):"+s.indexOf(97,1));
 32         sop(s+":"+"indexOf(\"ab\"):"+s.indexOf("ab"));
 33         sop(s+":"+"indexOf(\"ab\",1):"+s.indexOf("ab",1));
 34     }
 35     /**
 36      * 1.判断字符串时候包含某一个子串:public boolean contains(CharSequence s);
 37      * 2.字符串时候为空:public boolean isEmpty();
 38      * 3.是否以指定内容开头:public boolean startsWith(String prefix);
 39      * 4.是否以指定内容结尾:public boolean endsWith(String suffix);
 40      * 5.判断字符串时候相同:复写了Object的equals()方法public boolean equals(Object anObject);
 41      * 6.判断字符串时候相同,忽略大小写:public boolean equalsIgnoreCase(String anotherString);
 42      */
 43     public static void method_is(){
 44         String s="Hello World";
 45         sop(s+": "+"contains(\"hello\")"+" ? "+s.contains("hello"));
 46         sop(s+": "+"isEmpty()"+" ? "+s.isEmpty());
 47         sop(s+": "+"startsWith(\"hello\")"+" ? "+s.startsWith("hello"));
 48         sop(s+": "+"endsWith(\"World\")"+" ? "+s.endsWith("World"));
 49         sop(s+": "+"equals(\"hello world\")"+" ? "+s.equals("hello world"));
 50         sop(s+": "+"equalsIgnoreCase(\"hello world\")"+" ? "+s.equalsIgnoreCase("hello world"));
 51     }
 52     /**
 53      * 1.转换字符数组成字符串:构造public String(char[] value,..);
 54      *         public static String copyValueOf(char[] data,int offset,int count)
 55      *        public static String valueOf(char[] data)
 56      * 2.字符串转换成字符数组:public char[] toCharArray()
 57      * 3.字节数组转换成字符串:同1,参数改为byte[]
 58      * 4.字符串转换成字节数组:
 59      * 5.大小写转换:public String toUpperCase(..);public String toLowerCase(..);
 60      */
 61     public static void method_trans(){
 62         //构造
 63         char[] ch={'H','e','l','l','o'};
 64         String s=new String(ch);
 65         String s2=new String(ch,2,3);
 66         sop(s+": "+"String(ch)"+" ? "+s);
 67         sop(s2+": "+"String(ch,2,3)"+" ? "+s2);
 68         //valueOf
 69         sop("hello"+": "+"String.valueOf(ch)"+" ? "+String.valueOf(ch));
 70         //字符串转字符数组
 71         char[] chs=s.toCharArray();
 72         for(int x=0;x<chs.length;x++)
 73             sop(chs[x]);
 74         //转换大小写
 75         sop(s+": "+"s.toUpperCase()"+"? " +s.toUpperCase());
 76         sop(s+": "+"s.toLowerCase()"+"? " +s.toLowerCase());
 77         
 78     }
 79     /**
 80     *1.替换字符public String replace(char oldchar,char newchar)
 81     *  替换一堆public String replace(CharSequence target,CharSequence replacement)
 82     */
 83     public static void method_replace(){
 84         String s="Hello World";
 85         sop(s+": "+"s.replace('o','*')"+" ? "+s.replace('o','*'));
 86         sop(s+": "+"s.replace(\"World\",\"*\")"+" ? "+s.replace("World","*"));
 87     }
 88     /**
 89     *1.切割字符串public String[] split(String regex)
 90     */
 91     public static void method_split(){
 92         String s="hello,I'm,your,respect,erge";
 93         String[] ss=s.split(",");
 94         for(int i=0;i<ss.length;i++)
 95             sop(ss[i]);
 96     }
 97     /**
 98     *1.获取子串:public String subString(int begin);public String subString(int begin,int end);
 99     */
100     public static void method_sub(){
101         String s="12345678";
102         sop(s+": "+"s.substring(4)"+" ? "+s.substring(4));
103         sop(s+": "+"s.substring(1,6)"+" ? "+s.substring(1,6));
104     }
105     /**
106     *1.去除首尾空格public String trim()
107     */
108     public static void method_deBlank(){
109         String s=" hello world ";
110         String s2=" ";
111         sop(s+": "+"s.trim()"+" ? "+s.trim());
112         sop(s2.trim()+"123");
113     }
114     /**
115     *1.比较字符串大小(按ascii):public int compareTo(String another);相同为0
116     *2.或略大小写比较:public int compareToIgnoreCase(String another);
117     *3.比较两字符串是否相同:public boolean equals(Object anObject);
118     *4.忽略大小写比:public boolean equalsIgnoreCase(String another);
119     */
120     public static void method_equals(){
121         String s1="abcdEf";
122         String s2="abcdef";
123         sop("s1="+s1+"  s2="+s2+"\n"+"s1.compareTo(s2)"+" ? "+s1.compareTo(s2));
124         sop("s1.compareToIgnoreCase(s2)"+" ? "+s1.compareToIgnoreCase(s2));
125     }
126     public static void sop(Object obj){
127         System.out.println(obj);
128     }
129 
130 }
View Code

 StringBuffer

 

StringBuffer可看做是一个字符缓冲区,可以接收各种类型的数据,可以对该缓冲区进行增删改查!

增:append,insert

  public StringBuffer append(boolean b):参数可以各种重载:字符串、StringBuffer、CharSequence、基本数据类型(byte和short被提升为int)

  且返回的是StringBuffer对象,所以可以一直调用s.append(...).append(...)...

1 StringBuffer s=new String();
2 s.append(true).append('a').append("123").append(123.1f);

  public StringBuffer insert(int srcPosition, String str):也是各种重载

  public StringBuffer insert(int srcPosition, char[] str, int chPosition, int len);将字符数组str从chPosition开始到后面的共len个字符插入到Buffer的srcPosition后面

删:delete

  删除start到end-1部分:public StringBuffer delete(int start , int end);

  删除一个字符:public StringBuffer deleteCharAt(int index);

改:replace,setCharAt

  public StringBuffer replace(int start , int end ,String str);将start到end-1的部分替换成字符str

  public void setCharAt(int index , char ch)

查:indexOf,charAt 

  public int indexOf(String str)第一次出现str字符的下标

  public int indexOf(String str , int fromIndex)从fromIndex开始第一次出现字符的下标

 另:public String toString();public String substring(int index);public String substring(int start , int end);public int length();

 

StringBuilder

与StringBuffer方法一致

不同:StringBuffer线程安全,保证同步;而StringBuffer不保证线程安全。

所以:使用StringBuilder会更快,但可能面临线程安全问题,但在单线程中完全不必担心!

posted @ 2015-08-11 10:00  洱海  阅读(196)  评论(0编辑  收藏  举报
.First { margin: 10px 0; font-family: 'Microsoft Yahei'; text-align: left; padding: 6px 20px; color: #fff; background: #55895B; font-size: 20px; border-radius: 4px; clear: both; } .Second { margin: 10px 0; font-family: 'Microsoft Yahei'; padding: 6px 20px; background: #93C8A2; color: white; font-size: 18px; border-radius: 4px; clear: both; } .Third { margin: 10px 0; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0; font-size: 16px; color: black; background: #C6EFD2; border-radius: 4px; clear: both; } .note { margin: 10px 0; padding: 15px 20px 15px 60px; background: #FCFAA9 url('http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png') no-repeat 20px 0; font-size: 15px; font-family: 'Microsoft Yahei'; box-shadow: 0 0 8px #aaa; clear: both; } .demo { text-align: left; padding: 6px 20px; overflow: auto; border-radius: 4px; background: orange; color: #fff; font-size: 16px; clear: both; } .cnblogs_Highlighter { border: solid 1px #ccc; clear: both; } .cnblogs_code { background: #EFFFF4; border: solid 0px #939393; font-size: 14px; clear: both; padding: 10px 20px; } .cnblogs_code pre { font-size: 14px; } .cnblogs_code span { font-family: Courier New; font-size: 14px; }