?? decomposewords2.java
字號(hào):
/*本程序的主要目的是演示對(duì)象方法的使用。
*replace()---替換字符串中的字符
*indexOf()---查找串中字符的位置
*substring()---取串中的字串
*trim()---去掉字符串的前導(dǎo)和拖尾的空格
*程序名:DecomposeWords.java */
import java.util.Arrays; //引入數(shù)組類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]; //定義數(shù)組含50個(gè)元素
str1=str1.replace(',',' '); //將字符串中的,號(hào)字符替換為空格
str1=str1.replace('.',' '); //將字符串中的.字符替換為空格
int i=0,j;
while((j=str1.indexOf(" "))>0) //查找空格,若找到,則空格前是一單詞
{ s[i++]=str1.substring(0,j); //將單詞取出放入數(shù)組元素中
str1=str1.substring(j+1); //在字符串中去掉取出的單詞部分
str1=str1.trim(); //去掉字符串的前導(dǎo)空格
}
s[i]=str1; //將最后一個(gè)單詞放入數(shù)組單元中
Arrays.sort(s,0,i+1); //在上邊析取了i 個(gè)單詞,對(duì)它們進(jìn)行排序
for(j=0; j<=i; j++)
{ System.out.print(s[j]+" "); //輸出各單詞
if((j+1)%5==0) System.out.println();
}
System.out.println();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -