BRDroid
BRDroid

Reputation: 4378

Using String.format

How to use String Formatter to concatenate strings. what I tried is not working.

  String description = "This is description,";
   String message = "This is message";

   String result = description + " " + message; //works fine.

   //I want to replace it using String.format.
   //I tried the below code and it does not work.

   String.format(description, " ", message);

Expected result is This is description This is message

What is the right way of using String.format.

Thanks R

Upvotes: 1

Views: 80

Answers (3)

Premkumar Manipillai
Premkumar Manipillai

Reputation: 2151

Code :

String description = "This is description %1$s";
String message = "This is message";
String finalText = String.format(description, message);

Output :

This is description This is message

Upvotes: 0

Indent
Indent

Reputation: 4967

Try this :

String.format("%s %s",description, message);

Upvotes: 2

Kevin Robatel
Kevin Robatel

Reputation: 8386

String.format("%s %s", description, message);

The function header:

public static String format(String format, Object... args)

You have to pass the format and the variables to set. Take a lot at the Javadoc.

Upvotes: 1

Related Questions