Formatting of strings

U

Ulf

I'm looking for the best way to combine two strings so that the result
ends with the 2:nd string in position 30-32. The 1:st string is 1 to
30 chars long.

Currently I use the following (untested) code.

char cc[] = new char[33];
for(int i = 0; i < 33; i++) { cc = ' '; } //Make spaces
cc[30] = '/'; // This is the 3 chars for string2
cc[31] = '0';
cc[32] = '0';
string1.getChars(0,string1.length()- 1, cc, 0);
resultString = new String(cc);

I would appreciate suggestions both on the solution (perhaps this is
the best solution) and proper Java coding.

This operation will be used just a few times a day, so the solution
really doesn't matter for the project, but it matters for me trying to
learn Java.

/Ulf
 
M

Mark Space

Ulf said:
I'm looking for the best way to combine two strings so that the result
ends with the 2:nd string in position 30-32. The 1:st string is 1 to
30 chars long.

Currently I use the following (untested) code.

char cc[] = new char[33];
for(int i = 0; i < 33; i++) { cc = ' '; } //Make spaces
cc[30] = '/'; // This is the 3 chars for string2
cc[31] = '0';
cc[32] = '0';
string1.getChars(0,string1.length()- 1, cc, 0);
resultString = new String(cc);

I would appreciate suggestions both on the solution (perhaps this is
the best solution) and proper Java coding.

This operation will be used just a few times a day, so the solution
really doesn't matter for the project, but it matters for me trying to
learn Java.



This looks ok (I didn't try to compile it however). You should give it
a go. It might even be the most efficient way of doing it.

However, from a learning Java standpoint, you should look at
StringBuffer and StringBuilder classes. Those are the more normal ways
of making strings by building up parts at a time.
 
S

Stefan Ram

Ulf said:
ends with the 2:nd string in position 30-32. The 1:st string is 1 to

»Formatting« sounds as if an operation named »format« might help:

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( java.lang.String.format( "%-30s%3s", "alpha", "/00" )); }}

alpha /00
 
G

GArlington

I'm looking for the best way to combine two strings so that the result
ends with the 2:nd string in position 30-32. The 1:st string is 1 to
30 chars long.

Currently I use the following (untested) code.

char cc[] = new char[33];
for(int i = 0; i < 33; i++) { cc = ' '; } //Make spaces
cc[30] = '/'; // This is the 3 chars for string2
cc[31] = '0';
cc[32] = '0';
string1.getChars(0,string1.length()- 1, cc, 0);
resultString = new String(cc);

I would appreciate suggestions both on the solution (perhaps this is
the best solution) and proper Java coding.

This operation will be used just a few times a day, so the solution
really doesn't matter for the project, but it matters for me trying to
learn Java.

/Ulf


How about a simple:
String temp = " "; // Set it to 30 spaces...
String resultString = (yourStr1 + temp).substring(0, 30) + yourStr2;
 
R

Roedy Green

I'm looking for the best way to combine two strings so that the result
ends with the 2:nd string in position 30-32. The 1:st string is 1 to
30 chars long.

The standard way of composing strings from pieces is to use a
StringBuilder. The nice thing about the technique it is does not get
any more complicated if the pieces are variable length.

see http://mindprod.com/jgloss/stringbuilder.html
 
L

Lew

Roedy said:
The standard way of composing strings from pieces is to use a
StringBuilder. The nice thing about the technique it is does not get
any more complicated if the pieces are variable length.

see http://mindprod.com/jgloss/stringbuilder.html

char filler = new char [lenNeeded];
String filled = new StringBuilder( filler.length )
.append( filler ).append( arg ).toString();
 
R

rmoldskr+usenet

Ulf said:
I'm looking for the best way to combine two strings so that the result
ends with the 2:nd string in position 30-32. The 1:st string is 1 to
30 chars long.

Here's another way to do it. I don't know about "best", but I believe it is
more readable than anything that might be quicker, and quicker than anything
that might be more readable.


public class StringMaker {

// 30 space characters followed by "/00"
private static final char[] TEMPLATE = " /00".toCharArray( );

public static String makeString( String baseString ) {
char[] combined = TEMPLATE.clone( );
baseString.getChars( 0, baseString.length( ), combined, 0);
return new String( combined );
} // end makeString( )

} // end class StringMaker
 

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
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top