Answered by:
string.format problem

Question
-
I have a string "The image width is {width} and height is {height}"
I am getting this from webservice. I want to use string.format to have a string like
"The image width is 200 and height is 240"
What will be the format of the method.
Thursday, February 12, 2015 8:44 AM
Answers
-
You could use the string.Replace method like this:
string ws = "The image width is {width} and height is {height}"; ws = ws.Replace("{width}", "200").Replace("{height}", "240");
...or like this:
string ws = "The image width is {width} and height is {height}"; ws = ws.Replace("{width}", "{0}").Replace("{height}", "{1}"); ws = string.Format(ws, 200, 240);
But you cannot use string.Format directly without replacing {width} and {height} with {0} and {1} respectively first.
Hope that helps.
Please remember to close your threads by marking helpful posts as answer.
- Proposed as answer by Oliver Ulm Saturday, February 14, 2015 4:59 PM
- Marked as answer by Jamles HezModerator Tuesday, February 24, 2015 6:40 AM
Thursday, February 12, 2015 1:59 PM
All replies
-
i have not VS, so there can be small mistakes:
int width = 200; // get width from your webservices int height = 200; // get your height from your webservice string text = string.Format("The image width is {0} and height is {1}", width, height);
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Thursday, February 12, 2015 9:10 AM -
as webservice is returning the string as "The image width is {width} and height is {height}" I was looking for a way that will not need replacing the {width} by {0}Thursday, February 12, 2015 11:27 AM
-
string format not going to help you. But you can can simple use Replace function?
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Thursday, February 12, 2015 11:49 AM -
You could use the string.Replace method like this:
string ws = "The image width is {width} and height is {height}"; ws = ws.Replace("{width}", "200").Replace("{height}", "240");
...or like this:
string ws = "The image width is {width} and height is {height}"; ws = ws.Replace("{width}", "{0}").Replace("{height}", "{1}"); ws = string.Format(ws, 200, 240);
But you cannot use string.Format directly without replacing {width} and {height} with {0} and {1} respectively first.
Hope that helps.
Please remember to close your threads by marking helpful posts as answer.
- Proposed as answer by Oliver Ulm Saturday, February 14, 2015 4:59 PM
- Marked as answer by Jamles HezModerator Tuesday, February 24, 2015 6:40 AM
Thursday, February 12, 2015 1:59 PM