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

From CodeCodex

(Java)
(Implementations)
 
(One intermediate revision by one other user not shown)
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 8: Line 8:
 
         s = ">" + fillString(' ', 25) + "<";
 
         s = ">" + fillString(' ', 25) + "<";
 
         System.out.println(s);
 
         System.out.println(s);
         /*  
+
         /* output :   
          output :  >XXXXXXXXXXXXXXXXXXXXXXXXX<
+
        >XXXXXXXXXXXXXXXXXXXXXXXXX<
                    >                        <
+
        >                        <
 
         */             
 
         */             
             
 
 
     }
 
     }
  
Line 22: Line 21:
 
     }
 
     }
 
}
 
}
</highlightsyntax>
+
</pre>
 +
 
 +
===Ruby===
 +
<pre class="ruby">
 +
size = 25
 +
puts '>' + 'X' * size + '<'
 +
puts '>' + ' ' * size + '<'
 +
</pre>
 +
 
 +
=== Tcl ===
 +
<pre class="tcl">
 +
puts ">[string repeat X 25]<"
 +
puts ">[string repeat " " 25]<"
 +
</pre>
 +
 
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Ruby]]
 +
[[Category:Tcl]]
 +
[[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]<"