Re: how to insert space in a string
- From: Mark Space <markspace@xxxxxxxxxxxxx>
- Date: Fri, 31 Oct 2008 09:24:06 -0700
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...)
.
- Follow-Ups:
- Re: how to insert space in a string
- From: Arne Vajhøj
- Re: how to insert space in a string
- References:
- how to insert space in a string
- From: KC
- how to insert space in a string
- Prev by Date: Re: 7.0 wishlist?
- Next by Date: Re: how to insert space in a string
- Previous by thread: Re: how to insert space in a string
- Next by thread: Re: how to insert space in a string
- Index(es):
Relevant Pages
|