Corrupt data from StringBuilder () in Android Java application

I have some code in an android app that formats data like this. "[x, y, z]"

...

We mainly use the Java StringBuilder () class, add it like this:

StringBuilder sb = new StringBuilder();
sb.append("[x, y, z");
sb.append("]");

      

but in some cases we are doing basic string concatenation using the plus operator:

String str = "[x, y, z";
str = str + "]";

      

We recently ran into a situation where I see that the "]" character translates to this one "ิ‚3๏–›าก๒ฌ ƒๅถดu)"

, but only in places where the stringbuilder class is used. It started working correctly, but after he started transforming this character, he did this transform every time the character was seen and in several places in the code. This resulted in the output looking like this:"[ x, y, zิ‚3๏–›าก๒ฌ ƒๅถดu)";

This is a very rare occurrence as I have only seen this a couple of times, but I hope someone can have an idea of โ€‹โ€‹why this is happening and if there is anything that can be done to prevent it from happening. I would prefer not to remove stringbuilder from our project in order to fix such a rare event.

We noticed this on an Android device and it started very suddenly. (It worked correctly and then all of a sudden all subsequent instances of "]" were converted)

Any ideas or suggestions to prevent this? Unfortunately, I do not have access to the device that reported this data for testing.

Thank!

------------------------ Edit ---------------------- -

I ran into the same again (also with the StringBuilder () class), but this time it translated the same character "]"

into "U"

.

So the result looked like this: "[x, y, zU"

He sews, as always, the same "]" character that StringBuilder () has occasional problems with. (Maybe because it's always the last character?)

Hooray!

------------------------ Edit ---------------------- -

An example using one method that displayed this behavior: (only the last "]" has the problem, the rest of the line looks as expected)

public static String convertArrayToString(double[] array) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int i = 0; i < array.length; i++) {
        sb.append(String.valueOf(array[i]));
        if ((i + 1) != array.length) {
            sb.append(",");
        }
    }
    sb.append("]");
    return sb.toString();
}

      

+3


source to share





All Articles