Java字符串操作集合

jopen 10年前發布 | 35K 次閱讀 Java Java開發

 String str1 = "abcd";    String str2 = "abcdabcd";

//length():求字符串的長度
int len = str1.length();
System.out.println("字符串的長度:" +len);

//2.charAt(int index)取字符串中指定下標的一個元素
for (int i=0;i<len;i++) {
char c = str1.charAt(i);
System.out.println("下標" + i +"的值:" +c);
}

//3.codePointAt(int index):index 處字符的代碼點值
for (int i=0;i<len;i++) {
int j = str1.codePointAt(i);
System.out.println("下標"+i+"的值:" +j);
}

//4.concat(String str) 將指定字符串連接到此字符串的結尾
String s1 = str1.concat("ABCD");
System.out.println(s1);

String s2 = str1.concat("");
System.out.println(s2);

//5.copyValueOf(char[] data):返回指定數組中表示該字符序列的 String
char[] data = {'a','b','c','d','e','f'};
String str = String.copyValueOf(data);
System.out.println(str);

//6.copyValueOf(char[] data, int offset, int count):返回指定數組中表示該字符序列的 String
char[] data = {'a','b','c','d','e','f'};
String s1 = String.copyValueOf(data, 2, 3);
System.out.println(s1);

//7.endsWith(String suffix):測試此字符串是否以指定的后綴結束
        //startsWith(String prefix)
boolean isd = str1.endsWith("d");
System.out.println("此字符串是否以d結尾:"+isd);

//8.equals(Object anObject):將此字符串與指定的對象比較
boolean iseql = str1.equals("abcd");
System.out.println(iseql);
boolean iseq2 = str1.equals("abc");
System.out.println(iseq2);

//9.equalsIgnoreCase(String anotherString):將此 String 與另一個 String 比較,不考慮大小寫
boolean iseql = str1.equalsIgnoreCase("ABCD");
System.out.println(iseql);

//10.getByte():將字符串轉化為字節數組
byte[] data = str1.getBytes();
for (int i=0;i<data.length;i++) {
System.out.println(data[i]+"\t");
}

//indexOf(int ch, int fromIndex):返回在此字符串中第一次出現指定字符處的索引,從指定的索引開始搜索
int i = str2.indexOf(97, 3);
System.out.println(i);
int t = str2.indexOf(99, 1);
System.out.println(t);
int k = str2.indexOf(97,5);
System.out.println(k);

//isEmpty():當且僅當 length() 為 0 時返回 true
boolean isempty1 = str1.isEmpty();
System.out.println(isempty1);
boolean isempty2 = "".isEmpty();
System.out.println(isempty2);

//substring(int beginIndex, int endIndex):返回一個新字符串,它是此字符串的一個子字符串。該子字符串從指定的 beginIndex 處開始,直到索引 endIndex - 1 處的字符
String s1 = str2.substring(4,7);
System.out.println(s1);
String s2 = str2.substring(2,7);
System.out.println(s2);

//toCharArray():將此字符串轉換為一個新的字符數組
char[] c = str1.toCharArray();
for (int r=0;r<c.length;r++){
  System.out.print(c[r]+"\t");
}

//valueOf
1. 由 基本數據型態轉換成 String 
String 類別中已經提供了將基本數據型態轉換成 String 的 static 方法 
也就是 String.valueOf() 這個參數多載的方法 
有下列幾種 
String.valueOf(boolean b) : 將 boolean 變量 b 轉換成字符串 
String.valueOf(char c) : 將 char 變量 c 轉換成字符串 
String.valueOf(char[] data) : 將 char 數組 data 轉換成字符串 
String.valueOf(char[] data, int offset, int count) : 

將 char 數組 data 中 由 data[offset] 開始取 count 個元素 轉換成字符串 
String.valueOf(double d) : 將 double 變量 d 轉換成字符串 
String.valueOf(float f) : 將 float 變量 f 轉換成字符串 
String.valueOf(int i) : 將 int 變量 i 轉換成字符串 
String.valueOf(long l) : 將 long 變量 l 轉換成字符串 
String.valueOf(Object obj) : 將 obj 對象轉換成 字符串, 等于 obj.toString() 
用法如: 
int i = 10; 
String str = String.valueOf(i); 
這時候 str 就會是 "10" 

2. 由 String 轉換成 數字的基本數據型態 
要將 String 轉換成基本數據型態轉 
大多需要使用基本數據型態的包裝類別 
比如說 String 轉換成 byte 
可以使用 Byte.parseByte(String s) 
這一類的方法如果無法將 s 分析 則會丟出 NumberFormatException 
byte : 
Byte.parseByte(String s) : 將 s 轉換成 byte 
Byte.parseByte(String s, int radix) : 以 radix 為基底 將 s 轉換為 byte 
比如說 Byte.parseByte("11", 16) 會得到 17 
double : 
Double.parseDouble(String s) : 將 s 轉換成 double 
float : 
Double.parseFloat(String s) : 將 s 轉換成 float 
int : 
Integer.parseInt(String s) : 將 s 轉換成 int 
long : 

Long.parseLong(String s)

3. final String url = "http://www.263.net/images/kk/sim_1.jpg";

String parts[] = url.split("/", 5);// ==》images/sim_1.jpg    截取字符串,指明大小
int location = (int) (Math.random()*ips.length);
location = 0;
String ip = ips[location];
//別越界了
String filePath = "http://"+ip+"/"+parts[4];
filePath = filePath.replace(".jpg", ".mp4"); //替換
System.out.println(filePath); //==>http://123.129.249.204/kk/sim_1.mp4

4. String url = http://v.cctv.com/flash/mp4video4/qgds/2010/08/07/qgds_h264418000nero_aac32_20100807_1281191449601-1.mp4&2&3&4&5&6&7&8&9

截取mp4以前的  url = url.substring(0,  mUrl.lastIndexOf(".mp4") + 4);

打出的log:url= http://v.cctv.com/flash/mp4video4/qgds/2010/08/07/qgds_h264418000nero_aac32_20100807_1281191449601-1.mp4

5. 截取 String mUrl = "http://www.263.net/images/sim_1.jpg";

mUrl = mUrl.substring(mUrl.lastIndexOf("/") + 1, mUrl.length());

打出log:sim_1.jpg

6.String轉換為int型
String str;
int i = Integer.parseInt(str);

int型轉換為String
int i;
String str = i.toString();

//convert i(int) to j(Integer)
int i;
Integer j = Integer.valueOf(i);
//convert t(Integer) to n (int)
Integer t;
int n = t.intValue();

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!