等价+ =用于前缀

Jam*_*xon 6 c# string

是否有一些语法糖用于将数据加到字符串的开头,其方式与+ =附加到字符串的方式类似?

Jon*_*eet 20

只需使用:

x = "prefix" + x;
Run Code Online (Sandbox Code Playgroud)

没有复合赋值运算符可以执行此操作.


Jos*_*osh 7

你总是可以写一个扩展方法:

public static class StringExtensions{

    public static string Prefix(this string str, string prefix){
        return prefix + str;
    }

}

var newString = "Bean".Prefix("Mr. ");
Run Code Online (Sandbox Code Playgroud)

它不是语法糖,但很容易.虽然它并不比已经建议的更简单.


Nic*_*GPS 7

在C#中没有= +运算符,但幸运的是OO来到这里救援:

string value = "Jamie";
value = value.Insert(0, "Hi ");
Run Code Online (Sandbox Code Playgroud)

有关string.Insert的更多信息:http: //msdn.microsoft.com/en-us/library/system.string.insert.aspx

我同意a = b + a似乎是最明智的答案.它读取比使用string更好.插入是肯定的.