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

From CodeCodex

(Ruby)
(Implementations)
 
Line 1: Line 1:
 
==Implementations==  
 
==Implementations==  
 
===Java===
 
===Java===
<highlightsyntax language="java122">
+
<pre class="java">
 
  public class StringFixedAndFilled {
 
  public class StringFixedAndFilled {
 
     public static void main(String argv[])    {
 
     public static void main(String argv[])    {
Line 21: Line 21:
 
     }
 
     }
 
}
 
}
</highlightsyntax>
+
</pre>
  
 
===Ruby===
 
===Ruby===
<pre>
+
<pre class="ruby">
 
size = 25
 
size = 25
 
puts '>' + 'X' * size + '<'
 
puts '>' + 'X' * size + '<'
 
puts '>' + ' ' * size + '<'
 
puts '>' + ' ' * size + '<'
 +
</pre>
 +
 +
=== Tcl ===
 +
<pre class="tcl">
 +
puts ">[string repeat X 25]<"
 +
puts ">[string repeat " " 25]<"
 
</pre>
 
</pre>
  
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 +
[[Category:Tcl]]
 
[[Category:String]]
 
[[Category:String]]

Latest revision as of 18:36, 18 February 2011

Implementations[edit]

Java[edit]

 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);
    }
}

Ruby[edit]

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

Tcl[edit]

puts ">[string repeat X 25]<"
puts ">[string repeat " " 25]<"