Is there a bit of syntactic sugar for prefixing data to the beginning of a string in a similar way to how += appends to a string?
Just use:
x = "prefix" + x;
There's no compound assignment operator that does this.
These are methods from the FCL that can be used to merge strings, without having to use any concatenation operator. The + and += operators are prone to using a lot of memory when called repeatedly (i.e. a loop) because of the nature of strings and temp strings created. (Edit: As pointed out in comments, String.Format is often not an efficient solution either)
It's more of a syntactic alternative than sugar.
string full = String.Format("{0}{1}{2}", "prefix", "main string", "last string");
^ More info on String.Format at MSDN.
Edit: Just for two strings:
string result = string.Concat("prefix", "last part");
^ More info on String.Concat.
You could always write an extension method:
public static class StringExtensions{
public static string Prefix(this string str, string prefix){
return prefix + str;
}
}
var newString = "Bean".Prefix("Mr. ");
It's not syntactic sugar, but easy nonetheless. Although it is not really any simpler than what has already been suggested.
There is no =+ operator in C#, but thankfully OO comes to the rescue here:
string value = "Jamie";
value = value.Insert(0, "Hi ");
For more info on string.Insert: http://msdn.microsoft.com/en-us/library/system.string.insert.aspx
I would agree that a = b + a seems the most sensible answer here. It reads much better than using string.Insert that's for sure.