Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format() doesn't work, but string.Format() does

I would not believe this if I wasn't seeing it with my own eyes.

string test = String.Format( "{0} test {1}", "Mark", 13 );

Results in a value of "{0} test {1}" for variable test

string test = string.Format( "{0} test {1}", "Mark", 13 );

Results in a value of "Mark test 13" for variable test

Whhhhahaaaaattt? This is Xamarin by the way. I am very baffled here. Visual Studio 8.0.4. I've assigned the value of test to a UI element, logged it to LogCat, and viewed it with the debugger. They all agree on the odd value.

like image 580
Mark Herscher Avatar asked Apr 29 '19 19:04

Mark Herscher


People also ask

What is string format () used for?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

What is toString () and string format ()?

What is toString()? A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.

What is %s in string format?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following Python code illustrates the way of performing string formatting.


1 Answers

If you are targetting Android, your String class refers to java.lang.String (not System.String, which is aliased as string in .NET)

It has Format method, but different placeholders https://developer.xamarin.com/api/member/Java.Lang.String.Format/p/System.String/Java.Lang.Object%5B%5D/

For expected placeholders check for example: https://dzone.com/articles/java-string-format-examples

like image 172
Lesiak Avatar answered Sep 27 '22 19:09

Lesiak