## 用顿号将若干个字符串分隔开

String[] words
= { " a " , " b " , " c " } ;
String word
= "" ;

for ( int  i = 0 ;i < words.length;i ++ )

{
word
+= words[i]＋ " " ;
}

正确做法：

String[] words
= { " a " , " b " , " c " } ;
String word
= "" ;

for ( int  i = 0 ;i < words.length;i ++ )

{

if  ( ! word.equals( "" ))
word
+= " " ;
word
+= words[i];
}

posted on 2007-02-27 08:20 我为J狂 阅读(2709) 评论(15)  编辑  收藏 所属分类: Java算法

### 评论

#### #re: 用顿号将若干个字符串分隔开 2007-02-27 08:33 easyrun

if ( ! word.equals( "" ))

#### #re: 用顿号将若干个字符串分隔开 2007-02-27 09:33 永恒之光

for ( int i = 0 ;i < words.length-1;i ++ )  回复  更多评论

#### #re: 用顿号将若干个字符串分隔开 2007-02-27 10:02 我为J狂

“永恒之光”的方法真妙！以上的程序改为：
String[] words = { " a " , " b " , " c " } ;
String word = "" ;
int i=0;
for ( i = 0 ;i < words.length-1;i ++ )
{
word+=words[i]+" 、 ";
}
word +=words[i];

回复  更多评论

#### #re: 用顿号将若干个字符串分隔开 2007-02-27 11:12 我为J狂

String[] words = { " a " , " b " , " c " } ;
StringBuffer word =new StringBuffer("");
int i=0;
for ( i = 0 ;i < words.length-1;i ++ )
{
word=word.append(new StringBuffer(words[i]).append(" 、 "));
}
word=word.append(words[i]);

#### #re: 用顿号将若干个字符串分隔开 2007-02-27 12:00 hcom

String[] words = { " a ", " b ", " c " };
StringBuffer word = new StringBuffer("");
int i = 0;
for (i = 0; i < words.length - 1; i++) {
word.append(words[i]).append("、");
}
word.append(words[i]);

#### #re: 用顿号将若干个字符串分隔开 2007-03-05 23:09 dreamstone

@我为J狂
Equals的时候尽量用"内容"开头，这样不用判断空

#### #re: 用顿号将若干个字符串分隔开 2007-03-05 23:13 dreamstone

@我为J狂
int i = 0;
for (i = 0; i < words.length - 1; i++) {
word.append(words[i]).append("、");
}

for (int i = 0; i < words.length - 1; i++) {
word.append(words[i]).append("、");
}
回复  更多评论

#### #re: 用顿号将若干个字符串分隔开 2007-03-06 11:23 我为J狂

@dreamstone

for (int i = 0; i < words.length - 1; i++) {
word.append(words[i]).append("、");
}

//代码1
for(Iterator it=list.iterator();it.hasNext();)
{
}
//代码2
Iterator it=list.iterator();
while(it.hasNext())
{
}

#### #re: 用顿号将若干个字符串分隔开 2007-03-09 09:35 slx

for (int i = 0; i < words.length; i++) {
if(i != 0){
world.append("、");
}
word.append(words[i]);
}
回复  更多评论

#### #re: 用顿号将若干个字符串分隔开 2007-03-09 18:01 我为J狂

@slx

 只有注册用户登录后才能发表评论。 网站导航: 相关文章:
 < 2007年2月 >
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

• 随笔 - 47
• 文章 - 30
• 评论 - 327
• 引用 - 0

•

• 积分 - 169296
• 排名 - 343