Re: how to insert space in a string



KC wrote:
Hi all,

I m new in Java, Im not know well about Java syntax, for now, I need
to reform a string var, find out the newline char ( "\r\n" ) and

This bit here is easy in Java. I think split() is what you want.

String[] lines = in.split( "\n\r" );

This is a string function, please look up the details in the Java docs.

replace it with specify no. of Space char ( " " ), in order to get the
result string char with 50 * (no.of line) char,

This here I'm not sure what you mean to do. I'm guessing that you want to pad out the strings so that each string is 50 characters long. Anyway, here's my guess:

public static void main( String[] args )
{
String pad =
" ";//50 spaces
String in = "1\n\rTest 2\n\r\n\rTest 4testmore\n\r";
String[] lines = in.split( "\n\r" );
System.out.println( "Size: " + lines.length );
for( int i = 0; i < lines.length; i++ ) {
lines[i] = lines[i] + pad.substring( Math.min( lines[i].length(), pad.
length() ) );
}
for( String result : lines ) {
System.out.println( "["+result+"]" );
}
}

There unfortunately is no "make a string X characters long" function in Java, so I had to build my own with "pad" and the substring() function. If this is a problem, it's not hard to make your own such function. See the StringBuilder class.

(Also, in Java, "functions" are called "methods". Just saying...)
.



Relevant Pages

  • Re: socket communication: send & receive doesnt work right
    ... How can I send the string as string in Java and how can I receive ... public void send_doubles(double vals, int len) throws ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Help me!! Why java is so popular
    ... String or StringBuffer? ... keep in mind I knew NOTHING about Java. ... The language accepted it, no warnings, and thus I ... in some native assemblers you can peruse the instruction ...
    (comp.lang.java.programmer)
  • Re: question about assigning null to a reference object
    ... String getAuthor() ... void setAuthor ... b.setTitle("Thinking in Java 4th Edition"); ...
    (comp.lang.java.help)
  • Re: Java questions: Urgent
    ... why I would expect almost any Java programmer to be able to answer ... Which is the main reason I "butted" in a bit.. ... on the characters in the String. ... > therefore naturally cover all the basics, or they have missed out badly ...
    (comp.lang.java.programmer)
  • Re: Using Cobol when writing CGIs for the OSU web server.
    ... string safe as well as make slow conversion (as bugs are found and ... The string type approach. ... Because no one has bothered to add it to the "offending" language. ... Java is certainly no replacement for C. ...
    (comp.os.vms)