String Fundamentals

来源:互联网 发布:怎么开通淘宝直播间 编辑:程序博客网 时间:2024/06/06 14:28

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html

String s = “Hi Jim”;
String[] strs = s.split(” “);

1.Print

System.out.println(s);

2.Length

s.length() = 6;
strs.length = 2;
str[1].length() = 3;

3.Split string into string array, char array

String array

String[] strs = s.split(” “);

String[] strs = s.split(" ");    for (String x : strs)         System.out.println(x);    System.out.println(strs.length);

The result is

HiJim2
Char Array

char[] c = s.toCharArray();
include blanks

4.The Nth char

S.charAt(3) = J

注意’ ’ 表示char 但 ” “表示String

5. Insert letters

  1. You can use StringBuffer > http://www.coderanch.com/t/390095/java/java/Insert-String

6. Replace letters

  • Turn the String into a char[], replace the letter by index, then convert the array back into a String.
String myName = "domanokz";char[] myNameChars = myName.toCharArray();myNameChars[4] = 'x';myName = String.valueOf(myNameChars);
  • You can overwrite a string, as follows:
String myName = "halftime";myName = myName.substring(0,4)+'x'+myName.substring(5); The result is : halfxime

7. String Concatenation

  • Just use ‘+’, but time consuming > http://alvinalexander.com/blog/post/java/combine-two-strings
  • Use StringBuilder and append

8. Character Manipulation

  • Characeter.isLetter(char x) Character.isDigit(char x)
  • Charaster.toLowerCase() Character.toUpperCase()
  • isAlphanumeric(char x)
0 0
原创粉丝点击