English 中文(简体)
逼近模式问题——核心 Java(报复)
原标题:String Pattern Question - Core Java (repeatSeparator)
  • 时间:2010-12-20 18:38:40
  •  标签:
  • java

www.un.org/Depts/DGACM/index_spanish.htm 撰写 Java功能,这样,鉴于两条插图、字数和一个分离器,将一大计的计数归结,由分离人阵列。

repeatSeparator("Word", "X", 3) → "WordXWordXWord"        
repeatSeparator("This", "And", 2) → "ThisAndThis"         
repeatSeparator("This", "And", 1) → "This"        

My code is as below but is not working

public String repeatSeparator(String word, String sep, int count) {        
    if(count == 1) {       
        return word;        
    }    
    if(count > 1) {       
        for (int i = 0; i < count-1; i++){        
            word = word + sep + word;           
        }       
    }               
   return word;                         
}

实例

                                   Expected         Run                  Result        
repeatSeparator("Word", "X", 3) → "WordXWordXWord" "WordXWordXWordXWord" X    
最佳回答

以下职能应满足你们的需要:

public String repeatSeparator(String word, String sep, int count) {    
    StringBuffer buffer = new StringBuffer();

    while (count > 0) {
        buffer.append(word);
        count--;
        if (count > 0) {
            buffer.append(sep);
        }
    }

    return buffer.toString();                           
}
问题回答

字 = 字数+

仔细思考一下这第二次如何。 Hint:word自第一次改动以来发生了变化。

解决办法: 采用不同的变量来掌握结果,以便你每次都读到相同的<条码>。 (Free hint: use a StringBuffer or StringBuilder.)

public string doStuff(
    final String word,
    final String seperator,
    final int count)
{
    StringBuffer buffer = new StringBuffer();
    for (int index = 0; index < count; ++index)
    {
        if (buffer.length() > 0)
        {
            buffer.append(seperator);
        }

        buffer.append(word);
    }

    return buffer.toString();
}
public String repeatSeparator(String word, String sep, int count) {        
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < count; i++) {
    sb.append(word);
    if (i < count - 1)
        sb.append(sep);
    }
    return sb.toString();
}

在实际生活中,将使用。ache.org/lang/api-release/index.html StringUtils.repeat

还必须考虑边际案件。 如果计数为零,则该方案应退回空洞的 Str。

public String repeatSeparator(String word, String sep, int count) {
  String ans ="";
  for(int i=0;i<count;i++)
  {
    if(i<count-1)
    {
      ans+=word + sep;
    } else {
      ans+=word;
    }
  }
  return ans;
}
public String repeatSeparator(String word, String sep, int count) {
    String s = "";

   if (count > 1)
   {
       while (count > 0)
       {
           if (count > 1)
           {
               s = s + word + sep;
               count--;
           }


           if (count == 1)
           {
               s = s + word;
               count--;
           }
        }
   }

   if (count == 1) s = word;

   return s;

}
public static String repeatSeparator1(String word,String sep,int count){
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<count;i++){
        sb.append(word+sep);
    }
    return sb.substring(0, sb.lastIndexOf(sep));
}

用于:

public String repeatSeparator(String word, String sep, int count) {

  String result = "";

  if(count == 0)
  return "";


  for(int i = 0 ; i < count-1 ; i++){
    result = result +(word+sep);
  }

  return result+word;
}
public String createSeparatorString(String word, String separator, int count) {    
    StringBuffer stringBuffer = new StringBuffer();

    while (count > 0) {
        stringBuffer.append(word);
        count--;
        if (count > 0) {
            stringBuffer.append(separator);
        }
    }

    return stringBuffer.toString();                           
}

这里还有另一种类似的做法(如果计算值大于0,则在每一指示结尾处加上一字):

public String repeatSeparator(String word, String sep, int count) {
  String out="";
  if(count>0){
    for(int i=1; i<=count-1; i++){
      out += word+sep;
    }
    return out+word;
  }
  else return out;
}
public String repeatSeparator(String word, String sep, int count) {
   String g="";
   for(int i=0;i<count;i++){
     g=g+word;
     if(i<count-1){
       g=g+sep;
     }
  }
   return g;
}
public static void main(String[] args) {

        String str1="Word";
        String str2="X";
        int x=2;

        System.out.println(repeatString(str1, str2, x));

    }

    public static String repeatString(String s1, String s2, int y) {
        String dummy="";
        for(int i=1; i<=y; i++) {
            dummy+=s1;
                if(i!=y) {
                    dummy+=s2;  
                }

        }

        return dummy;

    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签