# Formatting Strings

# Format a string resource

You can add wildcards in string resources and populate them at runtime:

  • Edit strings.xml
    <string name="my_string">This is %1$s</string>
    
    
  • Format string as needed
    String fun = "fun";
    context.getString(R.string.my_string, fun);
    
    
  • # Formatting data types to String and vise versa

    Data types to string formatting

    Data types like int, float, double, long, boolean can be formatted to string using String.valueOf().

    String.valueOf(1); //Output -> "1"
    String.valueOf(1.0); //Output -> "1.0"
    String.valueOf(1.2345); //Output -> "1.2345"
    String.valueOf(true); //Output -> "true"
    
    

    Vise versa of this, formatting string to other data type

    Integer.parseInt("1"); //Output -> 1
    Float.parseFloat("1.2"); //Output -> 1.2
    Boolean.parseBoolean("true"); //Output -> true
    
    

    # Format a timestamp to string

    For full description of patterns, see SimpleDateFormat reference (opens new window)

    Date now = new Date();
    long timestamp = now.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    String dateStr = sdf.format(timestamp);