length() | 取得字串的字元長度 |
equals() | 判斷原字串中的字元是否相等於指定字串中的字元 |
toLowerCase() | 轉換字串中的英文字元為小寫 |
toUpperCase() | 轉換字串中的英文字元為大寫 |
public class UseString {
public static void main(String[] args) {
String text = "hello";
System.out.println("字串內容: " + text);
System.out.println("字串長度: " + text.length());
System.out.println("等於hello? " +
text.equals("hello"));
System.out.println("轉為大寫: " +
text.toUpperCase());
System.out.println("轉為小寫: " +
text.toLowerCase());
}
}
字串內容: hello 字串長度: 5 等於hello? true 轉為大寫: HELLO 轉為小寫: hello |
Byte.parseByte(字串) | 將字串剖析為位元 |
Short.parseShort(字串) | 將字串剖析為short整數 |
Integer.parseInt(字串) | 將字串剖析為integer整數 |
Long.parseLong(字串) | 將字串剖析為long整數 |
Float.parseFloat(字串) | 將字串剖析為float浮點數 |
Double.parseDouble(字串) | 將字串剖析為double浮點數 |
char charAt(int index) | 傳回指定索引處的字元 |
int indexOf(int ch) | 傳回指定字元第一個找到的索引位置 |
int indexOf(String str) | 傳回指定字串第一個找到的索引位置 |
int lastIndexOf(int ch) | 傳回指定字元最後一個找到的索引位置 |
String substring(int beginIndex) | 取出指定索引處至字串尾端的子字串 |
String substring(int beginIndex, int endIndex) | 取出指定索引範圍子字串 |
char[] toCharArray() | 將字串轉換為字元Array |
public class UseString {
public static void main(String[] args) {
String text = "Your left brain has nothing right.\n"
+ "Your right brain has nothing left.\n";
System.out.println("字串內容: ");
for(int i = 0; i < text.length(); i++)
System.out.print(text.charAt(i));
System.out.println("\n第一個left: " +
text.indexOf("left"));
System.out.println("最後一個left: " +
text.lastIndexOf("left"));
char[] charArr = text.toCharArray();
System.out.println("\n字元Array內容: ");
for(int i = 0; i < charArr.length; i++)
System.out.print(charArr[i]);
}
}
字串內容: Your left brain has nothing right. Your right brain has nothing left. 第一個left: 5 最後一個left: 64 字元Array內容: Your left brain has nothing right. Your right brain has nothing left. |
public class UseString {
public static void main(String[] args) {
String[] filenames = {"caterpillar.jpg", "cater.gif",
"bush.jpg", "wuwu.jpg", "clockman.gif"};
System.out.print("過濾出jpg檔案: ");
for(int i = 0; i < filenames.length; i++)
if(filenames[i].endsWith("jpg"))
System.out.print(filenames[i] + " ");
System.out.println("");
}
}
過濾出jpg檔案: caterpillar.jpg bush.jpg wuwu.jpg |