0

I have the following java method, written for an android application.

private String removeWifiFudge(String message, String removedFudge)
{
    int find1 = 255;   //FF
    int find2 = 0;     //00
    int find3 = 204;   //CC
    int find4 = 36;    //24

    char[] charMessage = message.toCharArray();

    boolean find1True = false;

    for (char eachCharacter : charMessage)
    {
        if (find1True)
        {
            if ((int) eachCharacter == find2)
            {
                removedFudge = removedFudge + String.valueOf((char)find1);
            }
            else
            {
                if ((int) eachCharacter == find3)
                {
                    removedFudge = removedFudge + String.valueOf((char)find4);
                }
                else
                {
                    removedFudge = removedFudge + String.valueOf((char)find1);

                    removedFudge = removedFudge + String.valueOf(eachCharacter);
                }
            }

            find1True = false;
        }
        else 
        {
            if ((int) eachCharacter == find1)
            {
                find1True = true;
            }
            else
            {
                removedFudge = removedFudge + String.valueOf(eachCharacter);
            }
        }
    }   

    return removedFudge;
}

In a nutshell, it takes a string, message and searches it character by character for instances of 0xFF00 and 0xFFCC. On finding these instances, it replaces them by 0xFF and 0x24 respectively, putting it in a new string removedFudge.

This method is taking up a massive percentage of the CPU time and whilst using the java profiler embedded in eclipse, DDMS, it informed me 53% of the method time is spent:

java/lang/StringBuilder. (Ljava/lang/String;)V

This seems to be saying it is taking up the time initialising a string, however as I am passing it the already initialised string to put the new message in, I can't see where the initialising String is coming from.

Anyone an expert on DDMS?

Pippa Rose Smith
  • 1,377
  • 5
  • 18
  • 42
  • You should use [String.replaceall](http://developer.android.com/reference/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29) . – nandeesh Aug 17 '12 at 10:27

1 Answers1

4

String concatenation results in using a StringBuilder at runtime and so it's appearing in your profiler.

Vikdor
  • 23,934
  • 10
  • 61
  • 84