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

From CodeCodex

(Java)
(Ruby)
Line 8: Line 8:
 
         s = ">" + fillString(' ', 25) + "<";
 
         s = ">" + fillString(' ', 25) + "<";
 
         System.out.println(s);
 
         System.out.println(s);
         /*  
+
         /* output :   
          output :  >XXXXXXXXXXXXXXXXXXXXXXXXX<
+
        >XXXXXXXXXXXXXXXXXXXXXXXXX<
                    >                        <
+
        >                        <
 
         */             
 
         */             
             
 
 
     }
 
     }
  
Line 23: Line 22:
 
}
 
}
 
</highlightsyntax>
 
</highlightsyntax>
 +
 +
===Ruby===
 +
<pre>
 +
size = 25
 +
puts '>' + 'X' * size + '<'
 +
puts '>' + ' ' * size + '<'
 +
</pre>
 +
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Ruby]]
 +
[[Category:String]]

Revision as of 11:22, 31 July 2010

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>

Ruby

size = 25
puts '>' + 'X' * size + '<'
puts '>' + ' ' * size + '<'