Append String in android using StringBuilder

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Append String in android using StringBuilder

Postby vivekkalady » Sat Nov 22, 2014 3:05 pm

StringBuilder is a mutable string, unlike regular strings which are immutable.

StringBuilder is especially useful when you need to concatenate many strings

Code: Select all

StringBuilder sb = new StringBuilder();
sb.append("first String ");
sb.append(intValue);
sb.append(" string again");
sb.append(floatValue);
return sb.toString();


below code shows how to combine all the elements of a string array to a string with , separation

Code: Select all

String list[]=new String[]{"A","C","D","F"};

public String CombineString(String arr[]){

    StringBuilder sb = new StringBuilder();
    String prefix = "";
    for (String str : arr)
    {
        sb.append(prefix);
        prefix = ",";
        sb.append(str); 
    }
    return sb;
}

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron