在C#中生成固定长度的字符串

时间:2013-06-12 10:41:48

标签: string c#-4.0

如何根据c#中给出的输入生成一个固定长度的字符串。

For example :
string s="1";
string newstring ;
I want the newstring to be "AB_000001";
// Similarly if 
s="xyz";
//I want the newstring as 
newstring = "AB_000xyz";

2 个答案:

答案 0 :(得分:1)

string basestr = "AB_000000";
string inp = "xyz";
string res = basestr.Substring(0, basestr.Length - inp.Length) + inp;

答案 1 :(得分:1)

String s = "AB_000000";
String newString="xyz";
s = s.Remove(s.Length - newString.Length, newString.Length);
s = s + newString;