Difference between revisions of "Create a String with a fixed length"

From CodeCodex

(Java)
 
(Java)
Line 18: Line 18:
 
         // creates a string of 'x' repeating characters
 
         // creates a string of 'x' repeating characters
 
         char[] chars = new char[count];
 
         char[] chars = new char[count];
         while (count>0) chars[--count] = fillChar;
+
         java.util.Arrays.fill(chars, fillChar);
 
         return new String(chars);
 
         return new String(chars);
 
     }
 
     }

Revision as of 01:07, 18 August 2009

Implementations

Java

<highlightsyntax language="java122">

public class StringFixedAndFilled {
   public static void main(String argv[])     {
       String s = ">" + fillString('X', 25) + "<";
       System.out.println(s);
       s = ">" + fillString(' ', 25) + "<";
       System.out.println(s);
       /* 
         output :  >XXXXXXXXXXXXXXXXXXXXXXXXX<
                   >                         <
       */            
              
   }
   public static String fillString(char fillChar, int count){
       // creates a string of 'x' repeating characters
       char[] chars = new char[count];
       java.util.Arrays.fill(chars, fillChar);
       return new String(chars);
   }

} </highlightsyntax>