?? decomposewords2.java
字號:
/*本程序的主要目的是演示對象方法的使用。
*replace()---替換字符串中的字符
*indexOf()---查找串中字符的位置
*substring()---取串中的字串
*trim()---去掉字符串的前導和拖尾的空格
*程序名:DecomposeWords.java */
import java.util.Arrays; //引入數組類Arrays
public class DecomposeWords2
{
public static void main(String [] args)
{
String str1="The String class represents character strings. All string literals in Java programs, such as \"abc\", are implemented as instances of this class.";
String [] s =new String[50]; //定義數組含50個元素
str1=str1.replace(',',' '); //將字符串中的,號字符替換為空格
str1=str1.replace('.',' '); //將字符串中的.字符替換為空格
int i=0,j;
while((j=str1.indexOf(" "))>0) //查找空格,若找到,則空格前是一單詞
{ s[i++]=str1.substring(0,j); //將單詞取出放入數組元素中
str1=str1.substring(j+1); //在字符串中去掉取出的單詞部分
str1=str1.trim(); //去掉字符串的前導空格
}
s[i]=str1; //將最后一個單詞放入數組單元中
Arrays.sort(s,0,i+1); //在上邊析取了i 個單詞,對它們進行排序
for(j=0; j<=i; j++)
{ System.out.print(s[j]+" "); //輸出各單詞
if((j+1)%5==0) System.out.println();
}
System.out.println();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -