Java中的基礎----String類的介紹、基本操作
String類的構造方法:
1)String()
2)String(byte[])
3)String(byte[],int,int)
4)String(byte[],int,int,String)
5)String(byte[],String)
6)String(char[])
7)String(char[],int,int)
8)String(String)
9)String(StringBuffer)
10)String(String)
基本操作1:取字串charAt(int index)
package com.study; public class Test { public static void testString(){ <strong>String str=new String("Hello world"); char c=str.charAt(0); //取字串操作</strong> System.out.print(c); } public static void main(String[] args) { Test.testString(); } }
輸出:H
基本操作2:比較字串compareTo(Object o)、compareTo(String str)、CompareToIgnore(String str)
package com.study; public class Test { public static void testString(){ <strong>//compareTo()和CompareToIgnore()返回值為0,則表示相同</strong> String str=new String("Hello world"); String str2 ="Hello world"; <strong>System.out.println("compareTo:"+(str.compareTo(str2)==0)); System.out.println("compareToIgnoreCase:"+(str.compareToIgnoreCase(str2)==0));</strong> } public static void main(String[] args) { Test.testString();<pre name="code" class="java">String s="you are a little boy!"; char[] c=new char[s.length()]; c=s.toCharArray(); System.out.print(c);
}} 輸出結果:
compareTo:truecompareToIgnoreCase:true
基本操作3:連線字串concat(String str)
String str=new String("01234");
String str2 ="5678";
System.out.println(str.concat(str2));
輸出結果:012345678
基本操作4:字元陣列轉為字串 copyValueOf(char[] data) 、 copyValueOf(char[] data,int offset,int count) ,String(char[] data)、String(char[] ,int,int)
char data[]={'a','b','c','d'};
String str=String.copyValueOf(data);
System.out.println(str);
輸出結果:abcd
這樣也行:
char data[]={'a','b','c','d'};
String str=new String(data);
System.out.println(str);
輸出結果:abcd
還有這樣:
char data[]={'a','b','c','.'};
String str=String.copyValueOf(data,1,2);
System.out.println(str);
輸出結果:bc
還有啊:
char data[]={'a','b','c','.'};
String str=new String(data,1,2);
System.out.println(str);
輸出結果:bc
基本操作5:字串轉為字元陣列getChars(int beigin,int end,char[] data,int dstBegin)、toCharArray
String s="you are a little boy!";
char[] c=new char[s.length()];
s.getChars(0, s.length(), c, 0);
System.out.print(c);
輸出結果:you are a little boy!
String s="you are a little boy!";
char[] c=new char[s.length()];
c=s.toCharArray();
System.out.print(c);
輸出結果:you are a little boy!
基本操作6:判斷字串結束內容endsWith(String s)
同理:判斷起始子串startsWith(String s),startsWith(String s,int toffset)
String s="you are a little boy!";
boolean b=s.endsWith("boy!");
System.out.print(b);
輸出結果:true
基本操作7:字串物件是否相等equals(Object o)、equalsIgnoreCase(String s)
String s="you are a little boy!";
boolean b1=s.equals("boy!");
boolean b2=s.equals("you are a little boy!");
boolean b3=s.equalsIgnoreCase("you are a little boy!");
System.out.print(b1+","+b2+","+b3);
輸出結果:false,true,true
基本操作8:字串中的字元的定位indexOf(int ch)、indexOf(int ch,int fromindex)、indexof(String s),indexOf(String s,int fromindex)
String s="you are a little boy!";
int pos =s.indexOf('a');
int pos1=s.indexOf("are");
int pos2=s.indexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);
輸出結果:4和4and19基本操作9:字串中的字元最後的位置lastIndexOf(int ch),lastIndexOf(int ch,int fromindex),lastIndexOf(String s),lastIndexOf(String s,int fromindex)
String s="you are a little boy!";
int pos =s.lastIndexOf('a');
int pos1=s.lastIndexOf("are");
int pos2=s.lastIndexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);
輸出結果:8和4and0基本操作10:判斷長度length()
int Len=(new Stirng("abc")).length();
基本操作11:字串內容的替換replace()
String s="you are a little boy!";
s.replace('b', 'B');
System.out.println(s);
System.out.println(s.replace('b', 'B'));
輸出結果:you are a little boy!
you are a little Boy!
基本操作12:取子字串subString(int begin),subString(int begin,int end)
String s="you are a little boy!";
System.out.println(s.substring(0));
System.out.println(s.substring(0,5));
輸出結果:
you are a little boy!
you a
基本操作13:字串大小寫轉換toUpperCase().toLowerCase()
String s="you are a little boy!";
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
輸出結果:
YOU ARE A LITTLE BOY!
you are a little boy!
基本操作14:將其他型別轉為String:valueOf()
int n=5;
char[] c={'a','b'};
String s1,s2;
s1=String.valueOf(n);
s2=String.valueOf(c);
System.out.print(s1+"和"+s2);
輸出結果:5和ab