public class StringTokenizing { public static void main(String args[]){ String text = "To be or not to be, that is the question"; String delimits = "[, .]"; String[] tokens = text.split(delimits, 0); String[] tokens1 = text.split(delimits, 5); System.out.println("Num of tokens: " + tokens.length); for(String token:tokens){ System.out.println(token); } System.out.println("Num of tokens: " + tokens1.length); for(String token:tokens1){ System.out.println(token); } } }
DatoutwoCoding
2013年9月5日 星期四
String - split()
String 類別 (class) 有 split() 方法 (method)。
將一個字串分為數個token。
第一個引數為需split部分,第二個引數為該token的開始值。
String - subString()
String 類別 (class) 有 subString() 方法 (method)。
subString可由一個字串取出一個子字串。
public class Test { public static void main(String[] args){ String text = "To be or not to be, that is the question;"+ " Whether 'tis nobler in the mind to suffer"+ " the slings and arrows of outrageous fortune,"+ " or to take arms against a sea of troubles," + " and by opposing end them?"; String sub1 = text.substring(171); String sub2 = text.substring(20, 41); System.out.println(sub1); System.out.println(sub2); } }
String - indexOf()
String 類別 (class) 有 compareTo() 方法 (method)。
indexOf()可用來搜尋字串,並回傳字串所在的數值。
public class Test { public static void main(String[] args){ String text = "To be or not to be, that is the question;"+ " Whether 'tis nobler in the mind to suffer"+ " the slings and arrows of outrageous fortune,"+ " or to take arms against a sea of troubles," + " and by opposing end them?"; int aIndex = -1; int count = 0; while((aIndex = text.indexOf("the", ++aIndex)) > -1){ ++count; } System.out.println("Num of 'the' is : " + count); } }
String-compareTo()
String 類別 (class) 有 compareTo() 方法 (method)。
若str1 > str2 回傳true
str2 > str1 回傳false
相等回傳0
public class SequenceString { public static void main(String args[]){ String string1 = "A"; String string2 = "To"; String string3 = "Z"; String string1Out = "\"" + string1 + "\""; String string2Out = "\"" + string2 + "\""; String string3Out = "\"" + string3 + "\""; if(string1.compareTo(string3) < 0){ System.out.println(string1Out + " is less than " + string3Out); }else{ if(string1.compareTo(string3) > 0){ System.out.println(string1Out + " is greater than " + string3Out); }else{ System.out.println(string1Out + " is equal to " + string3Out); } } if(string2.compareTo(string1) < 0){ System.out.println(string2Out + " is less than " + string1Out); }else{ if(string2.compareTo(string1) > 0){ System.out.println(string2Out + " is greater than " + string1Out); }else{ System.out.println(string2Out + " is equal to " + string1Out); } } } }
String - charAt()
String 類別 (class) 有 charAt() 方法 (method)。
可取出String 物件裡的某個字元。
public class StringCharacters { public static void main(String args[]){ String text = "To be or not to be, that is the question;"+ " Whether 'tis nobler in the mind to suffer"+ " the slings and arrows of outrageous fortune,"+ " or to take arms against a sea of troubles," + " and by opposing end them?"; int spaces = 0; int vowels = 0; int letters = 0; for(int i=0; i < text.length(); i++){ char ch = Character.toLowerCase(text.charAt(i)); if(ch == 'a' || ch =='e' || ch == 'i' || ch == 'o'|| ch =='u'){ vowels ++; } if(Character.isLetter(ch)){ letters ++; } if(Character.isSpace(ch)){ spaces ++; } } System.out.println("vowels is : " + vowels + "\nconstants is : " + (letters - vowels) + "\nspaces is : " + spaces); } }
String - equals()
String 類別 (class) 有 equals() 方法 (method)。
將 兩個str做比較。
public class MatchStrings { public static void main(String args[]){ String string1 = "Hello World "; String string2 = "java"; String string3 = "Hello World java"; string1 += string2; System.out.println("Try"); if(string1.equals(string3)){ System.out.println("s1 and s3 are equls"); }else{ System.out.println("s1 and s3 are different"); } } }
String - concat()
String 類別 (class) 有 concat() 方法 (method) ,將 str 接到字串的後面。
public class Test { public static void main(String[] args){ String x1 = "xyz"; String x2 = "abc"; System.out.println(x1+x2); x1 = "tuv"; System.out.println(x1); x2 = "def"; System.out.println(x2); x2 = x2.concat("def"); System.out.println(x2); } }
訂閱:
文章 (Atom)