0

I have the following situation and I am not sure if I am handling it in smart way.

So I have to create a textual file that contains some lines.

Each line is a String composed by the concatenation of some other strings.

Some of these string (that I have to concatenate) are taken from a model object (are the values of the fields definied into a model object).

Some other string have fixed value (with fixed lenght).

For example (into the method that create the concatenation) I could have something like this:

// FINAL BUILDED String:
String outputLine;

// FIELDS TO BE CONCATENATED:
String jobId = "        ";
String address = myModelObject.getAddress();
String name = myModelObject.getName();

outputLine = jobId + address + name;

So, as you can see from the previous snippet, the jobId field have to be a fixed String composed by 8 blanks consecutive characters

My doubt is: is it correct or can I crete a fixed lenght String (composed by all blanks characters) in some smarter way? Maybe using an array of Char? But I am not sure that it is a smarter solution

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • possible duplicate of [Create a string with n characters](http://stackoverflow.com/questions/2804827/create-a-string-with-n-characters) – Wintermute Feb 27 '15 at 10:37

3 Answers3

3

Java Strings are always immutable. They cannot be changed. String jobId = " "; is a String Literal and is shared by all classes in the JVM. i.e, anything that comes within double quotes "" goes into the String constants pool and will have only one instance irrespective from where it is being used.

Next, outputLine = jobId + address + name; uses StringBuilder internally. So, there is nothing much that can be done here.

Two things that have to be checked while using Strings :

  1. Never use string.concat() in a loop (unless you want to show someone that it is a bad idea :P).

  2. Never create new String("someStringLiteral") (unless you want to show someone that it is a bad idea as well :P).

PS : If you are certain that some string will be used only few times, you could do new String(charArray[]) and keep it on the heap.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

One common method is to use Arrays.fill.

public static String makeFilledString(int n, char c) {
    char[] ca = new char[n];
    Arrays.fill(ca, c);
    return new String(ca);
}

No idea whether this is more or less efficient than just building one in a loop but at least it allows you to configure your field widths instead of hiding their lengths behind a hard-coded string.

Note that this technique always creates a new String. This may result in more memory being used that you would wish. It can be beneficial to keep track of them in a Map so you don't repeat the creations.

private static Map<Integer, String> spaces = new HashMap<>();

public static String spaces(int n) {
    String s = spaces.get(n);
    if (s == null) {
        spaces.put(n, s = makeFilledString(n, ' '));
    }
    return s;
}

NB: The above is not thread-safe.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Maybe what you need is to format the output string, if this is the case, you can use String.format:

outputLine = String.format("%8s%s%s", s1, s2, s3);

If you need your String to have a fixed length, you can create an utility method:

public static String fillString(String string, char character, int size) {
    StringBuilder sb = new StringBuilder(size);
    sb.append(string);
    while (sb.length() < size) {
        sb.insert(0, character);
    }
    return sb.toString();
}  
David SN
  • 3,389
  • 1
  • 17
  • 21