Re: Formatting of strings



Ulf wrote:
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[i] = ' '; } //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.

.



Relevant Pages

  • Re: how to wait for socket communications
    ... chars or bytes out of it? ... how to communicate the chars to Java. ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to wait for socket communications
    ... Well, in C++ I am converting the unsigned chars to chars, but I don't know ... how to communicate the chars to Java. ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Why String is Immutable?
    ... StringBuffer buffer; ... if StringBuffer had a non-explicit constructor taking a ... For the local object creation that Java does not have. ...
    (comp.lang.java.programmer)
  • Re: how to wait for socket communications
    ... how to communicate the chars to Java. ... because it seems that C++ chars are the same as Java bytes. ... So if I can just make a file out of that, the communication from C++ ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)
  • Re: supporting older JVMs
    ... a 1.4 environment, even if you don't use any Java 5 or 6 code in your program. ... compiler converts these concatenations into StringBuffer operations behind the scenes, ... It's not a bad idea to use a modern compiler, as they should have more bugs ironed out. ...
    (comp.lang.java.help)