Jonas Stawski

Everything .NET and More

Using Brackets with StringBuilder AppendFormat

I was in need of using some brackets with the AppendFormat of the StringBuilder class, but the brackets are a special character for the AppendFormat. The function uses the brackets to indicate variable placement. So in the code below the {0} will be replaced with the value of the variable name.

StringBuilder sb = new StringBuilder();
sb.AppendFormat("Hello, my name is {0}", name);
Dim SB As New StringBuilder() 
SB.AppendFormat("Hello, my name is {0}", Name) 

If you want to add a bracket to the StringBuilder and you also want to use the AppendFormat then you need to escape it by doing double brackets {{ or }}.

sb.AppendFormat("Hello, my name is {0} and these are brackets: {{ }}", name);
SB.AppendFormat("Hello, my name is {0} and these are brackets: {{ }}", Name)


Happy Programming!

Comments (1) -



Add comment