how to insert space in a string

K

KC

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
replace it with specify no. of Space char ( " " ), in order to get the
result string char with 50 * (no.of line) char,

example as blow:
" The group you are posting to is a Usenet group.\r\nMessages posted
to this group\r\nWill make your email address\r\n"

orginal sting change it into

"The group you are posting to is a Usenet group. " +
"Messages posted to this group
" +
"Will make your email
address "

for each line contains exactly 50 char



Thx all for attention,
 
K

KC

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
replace it with specify no. of Space char ( " " ), in order to get the
result string char with 50 * (no.of line) char,

example as blow:
" The group you are posting to is a Usenet group.\r\nMessages posted
to this group\r\nWill make your email address\r\n"

orginal sting change it into

"The group you are posting to is a Usenet group. " +
"Messages posted to this group
" +
"Will make your email
address "

for each line contains exactly 50 char

Thx all for attention,


"The group you are posting to is a Usenet group.\r\nMessages posted to
this group\r\nWill make your email address\r\n"

for 1st line: "The group you are posting to is a Usenet group.\r\n"
contains 46 char, that means insert 4 more space char reform to
exactly 50 chat string
"The group you are posting to is a Usenet group. "

for 2nd line: "Messages posted to this group\r\n"
contains 28 char, that means insert 22 more space char reform to
exactly 50 chat string
"The group you are posting to is a Usenet group.
"
 
G

GArlington

"The group you are posting to is a Usenet group.\r\nMessages posted to
this group\r\nWill make your email address\r\n"

for 1st line: "The group you are posting to is a Usenet group.\r\n"
contains 46 char, that means insert 4 more space char reform to
exactly 50 chat string
"The group you are posting to is a Usenet group. "

for 2nd line: "Messages posted to this group\r\n"
contains 28 char, that means insert 22 more space char reform to
exactly 50 chat string
"The group you are posting to is a Usenet group.
"

Not sure what (and WHY) you are trying to do, but see:
http://www.google.co.uk/search?q=ja...s=org.mozilla:en-US:official&client=firefox-a
 
M

Mark Space

KC said:
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 = lines + pad.substring( Math.min(
lines.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...)
 
A

Arne Vajhøj

Mark said:
KC said:
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" );

I think you mean "\r\n" ...

Arne
 
M

Mark Space

Arne said:
Mark said:
KC said:
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" );

I think you mean "\r\n" ...

Arne

Doh!
 
D

Donkey Hottie

thx for reply, I have try thr string.function(s), like .compare... but
not sure which one is capable for this case, if for vb syntax, I know
how to findout the exact postion of newline char in the string var,
but java, I have no idea

Java is very well documented in the internet.

Just google "java string" and click the javadoc link which will come. That
works for every class.

Java String has indexOf() -method which you can you to get the exact
position of newline char in the string var.

int pos = myString.indexOf("\r\n");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top