1> String()
2> String(char[] chars)
String(char[] chars,int startIndex,int numChars)
3> String(String strObj)
4> String(byte asciiChars[])
String(byte asciiChars[],int startIndex,int numChars)
2.整型、字符串相互轉換
1> String -> int
(1)int i=Integer.parseInt(String s)
(2)int i=Integer.valueOf(str).intValue()
2>int -> String
(1)String s=String.valueOf(i)
(2)String s=Integer.toString(i)
(3)String s=””+i;
代碼
//String --> int
String m_str="123";
int i=Integer.parseInt(m_str);
System.out.println("number is :"+(i+1));
i=Integer.valueOf(m_str);
System.out.println("number is :"+(i+2));
//String --> double
m_str="123.4";
Double d=Double.parseDouble(m_str);
System.out.println("number is :"+(d+3));
//int --> String
int j=321;
String s=String.valueOf(j);
System.out.println("string is :"+s+1);
s=Integer.toString(j);
System.out.println("string is :"+s+2);
s=""+j;
System.out.println("string is :"+s+3);
截圖
3.字符串處理函數
1>提取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
截圖
2>字符串連接
String concat(String str)
代碼
String str="live";
String ss;
ss=str.concat(" nihao");
System.out.println(ss);
System.out.println(str);
結果
live nihao
live
截圖
3>字符串長度
int length()
4>求字符串中某一位置的字符
char charAt(int index)
代碼
String str="powerful";
char c=str.charAt(3);
System.out.println(c);
System.out.println(str);
結果
e
powerful
5>字符串的比較 (StringBuffer未重寫equals方法)
(1) int compareTo(String anotherString)
代碼
String str="powerful";
String cstr="powerful";
String costr="poaer";
String comstr="pozes";
if(str.compareTo(cstr)==0)
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
if(str.compareTo(costr)>0)
{
System.out.println("str>costr");
}
else if(str.compareTo(costr)<0)
{
System.out.println("str<costr");
}
else
{
System.out.println("str==costr");
}
if(str.compareTo(comstr)>0)
{
System.out.println("str>comstr");
}
else if(str.compareTo(comstr)<0)
{
System.out.println("str<comstr");
}
else
{
System.out.println("str==comstr");
}
結果
str==cstr
str>costr
str<comstr
截圖
(2) boolean equals(Object anObject) //大小寫不相等
代碼
String str="powerful";
String cstr="powerful";
String costr="poaer";
if(str.equals(cstr))
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
if(str.equals(costr))
{
System.out.println("str==costr");
}
else
{
System.out.println("str!=costr");
}
結果
str==cstr
str!=costr
截圖
(3) boolean equalsIgnoreCase(String anotherString)
代碼
String str="powerful";
String cstr="poWErFul";
if(str.equalsIgnoreCase(cstr))
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
截圖
(4) regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
regionMatches(boolean ignoreCase ,int startIndex,String str2,int str2StartIndex,int numChars)
代碼
String str="Appl pineApple ppEN ";
String s="Apple";
if(str.regionMatches(9,s,0,s.length()))
{
System.out.println("str中子串與s相等");
}
else
{
System.out.println("str中子串與s不等");
}
if(str.regionMatches(5,s,0,s.length()))
{
System.out.println("str中子串與s相等");
}
else
{
System.out.println("str中子串與s不等");
}
結果
str中子串與s相等
str中子串與s不等
截圖
代碼
String str="Appl pineApple ppEN ";
String s="apple";
if(str.regionMatches(true,9,s,0,s.length()))
{
System.out.println("str中子串與s相等");
}
else
{
System.out.println("str中子串與s不等");
}
if(str.regionMatches(9,s,0,s.length()))
{
System.out.println("str中子串與s相等");
}
else
{
System.out.println("str中子串與s不等");
}
截圖
6>判斷字符串的前綴和後綴
(1)boolean startsWith(String prefix) 區分大小寫
代碼
String string = "powerful";
String preString="power";
if(string.startsWith(preString))
{
System.out.println("preString是string的前綴");
}
else
{
System.out.println("string的前綴不是preString");
}
截圖
(2) boolean startsWith(String prefix, int toffset)
代碼
String string = "powerful";
String preString = "wer";
if (string.startsWith(preString, 2)) {
System.out.println("preString是string的前綴");
} else {
System.out.println("string的前綴不是preString");
}
截圖
(3) boolean endsWith(String suffix)
代碼
String string = "powerful";
String afterString = "ful";
if (string.endsWith(afterString)) {
System.out.println("afterString是string的後綴");
} else {
System.out.println("string的前綴不是afterString");
}
截圖
7>字符串單個字符的查找
(1) int indexOf(char ch)
代碼
String str="powerful";
int c=str.indexOf(4);
System.out.println(c);
c=str.indexOf('e');
System.out.println(c);
截圖
(2) int indexOf(char ch, int fromIndex)
代碼
String str="pineapple len";
int c=10;
c=str.indexOf('p');
System.out.println(c);
c=str.indexOf('p',2);
System.out.println(c);
c=str.indexOf('p',4);
System.out.println(c);
c=str.indexOf('p',6);
System.out.println(c);
c=str.indexOf('p',7);
System.out.println(c);
截圖
(3) int lastIndexOf(char ch)
int lastIndexOf(char ch, int fromIndex)
代碼
String str="an pineapple pen";
int c=10;
c=str.lastIndexOf('p');
System.out.println(c);
c=str.lastIndexOf('p',2);
System.out.println(c);
c=str.lastIndexOf('p',4);
System.out.println(c);
c=str.lastIndexOf('p',8);
System.out.println(c);
c=str.lastIndexOf('p',11);
System.out.println(c);
截圖
8>字符串子串的查找
(1) int indexOf(String str)
int indexOf(String str, int fromIndex)
代碼
String str="apple pineapple pen";
int c=10;
c=str.indexOf("le");
System.out.println(c);
c=str.indexOf("le",5);
System.out.println(c);
c=str.indexOf("le",14);
System.out.println(c);
c=str.indexOf("el");
System.out.println(c);
截圖
(2) int lastIndexOf (String str)
int lastIndexOf (String str, int fromIndex)
代碼
String str="apple pineapple pen";
int c=10;
c=str.lastIndexOf ("le");
System.out.println(c);
c=str.lastIndexOf ("le",8);
System.out.println(c);
c=str.lastIndexOf ("le",14);
System.out.println(c);
c=str.lastIndexOf ("le",2);
System.out.println(c);
c=str.lastIndexOf ("el");
System.out.println(c);
截圖
9>字符串中字符大小寫的轉換
(1) String toLowerCase() 轉換成小寫
(2) String toUpperCase() 轉換成大寫
代碼
String str="Apple pineApple PEN";
String upperStr=str.toUpperCase();//大寫
String lowerStr=str.toLowerCase();//小寫
System.out.println("原始:"+str);
System.out.println("大寫:"+upperStr);
System.out.println("小寫:"+lowerStr);
截圖
10>字符串中多餘空格的去除
String trim() //僅是去除開頭和結尾的空格,字符串裏的空格保留
代碼
String str=" Apple pineApple PEN ";
String trimStr=str.trim();
System.out.println("an"+str+"u like");
System.out.println("an"+trimStr+"u like");
截圖
11>字符串中字符的替換
(1) String replace(char oldChar,char newChar)
代碼
String str=" Apple pineApple PEN ";
String replaceStr=str.replace('p','o');
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
截圖
(2) String replaceFirst(String regex, String replacement)
代碼
String str=" Apple pineApple PEN ";
String replaceStr=str.replaceFirst("pp","live");
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
結果
an Apple pineApple PEN u like
an Alivele pineApple PEN u like
截圖
(3) String replaceAll(String regex, String replacement)
代碼
String str=" Apple pineApple ppEN ";
String replaceStr=str.replaceAll("pp","live");
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
結果
an Apple pineApple ppEN u like
an Alivele pineAlivele liveEN u like
截圖
12>字符串轉換成字符數組
toCharArray ()
代碼
String str="Appl pineApple 蘋果";
char[] ch=str.toCharArray();
System.out.println("str:"+str);
System.out.println("ch:"+ch);
System.out.println("ch:");
for(int i=0;i<str.length();i++)
{
System.out.print(ch[i]);
}
System.out.println();
System.out.println("ch[7]:"+ch[7]);
System.out.println("ch[16]:"+ch[16]);
結果及注意
str:Appl pineApple 蘋果
ch:[C@15db9742
ch:
Appl pineApple
ch[7]:n
此結果得出的非常不穩定,不知道是不是sublime text軟件的問題,還是程序的問題?(歡迎每個人去測試,然後評論,大家一起學習進步)
截圖
string trim()方法:刪除字符串起始和末尾字符串
String s=" ni hao ";
String s2=s.trim();
則s2="ni hao";主要在用戶登錄頁面的時候使用。
轉載自:http://www.cnblogs.com/cainiaomahua/p/6040644.html