在7个字符(C#)后插入一个字符

在7个字符(C#)后插入一个字符,c#,C#,所以在我的游戏中,每个玩家都有一个七位数的数字。例如,如果一名玩家的ID号为0002232,我将如何使其在游戏中显示为000-2232。基本上,我想在三个字符后加一个“-” 谢谢!:) 简短回答 你可以用 字符串。插入 public string Insert( int startIndex, string value ) startIndex Type: System.Int32 The zero-based index position of the insertion.

所以在我的游戏中,每个玩家都有一个七位数的数字。例如,如果一名玩家的ID号为0002232,我将如何使其在游戏中显示为000-2232。基本上,我想在三个字符后加一个“-”


谢谢!:)

简短回答

你可以用

字符串。插入

public string Insert(
    int startIndex,
    string value
)

startIndex
Type: System.Int32
The zero-based index position of the insertion.
value
Type: System.String
The string to insert.
解释

此方法在指定的索引位置将一个字符串插入到另一个字符串中。第一个参数,
startIndex
是插入的从零开始的索引位置,您希望将其插入到
3
的索引位置,并且您希望第一个参数是
3
。第二个参数是要插入的字符串,您希望插入
-
,因此将第二个参数设置为
-

代码:

string idnumber = "0002232";
string displayednumber = idnumber.Insert(3, "-");
displayednumber = "000-2232"
结果:

string idnumber = "0002232";
string displayednumber = idnumber.Insert(3, "-");
displayednumber = "000-2232"
在线试用


一点研究或努力会有所帮助。你为什么不试着在string类的文档中四处看看,而不是在这里提问呢?可能的重复我不知道为什么不清楚,但我知道为什么研究很少,这是重复的^I已经对这个答案投了赞成票,但因为我的声望不足15分,所以不会公开展示。我如何接受这个答案?:)@OmarDajani,投票按钮下面有一个复选按钮。单击该复选按钮接受答案,如果有帮助,请接受答案。@NoahCristino没问题:)