Deepesh Nayak
Deepesh Nayak

Reputation: 23

How to format a string through String.Format

Numeric

  string a = String.Format("{0:#/#}",12)

output:1/2

But How to Convert it for string Suppose

  String b=String.Format("{0:###-#}","test")

Output : test

Expected Output: tes-t

Upvotes: 1

Views: 149

Answers (2)

almi_n
almi_n

Reputation: 51

I think you can split the string to its characters and then use format

string testString = "test";
string formattedString = string.Format("{0}{1}{2}-{3}", testString.Select(c => c.ToString()).ToArray());

Upvotes: 1

Oded
Oded

Reputation: 498904

You can't format a string through string.Format for the reason that it is already a string.

If you want to manipulate the string, use the methods defined on the string class.

Upvotes: 6

Related Questions