1

我试图在字符串的最后一个单词之前插入单词“and”。到目前为止,这是我的代码:

    string skillset = "";
    foreach (ListItem item in SkillSet.Items)
    {
        if (item.Selected)
        {
            skillset += item.Text + ", ";
        } 
    }
    skillset = skillset.Substring(0, skillset.Length - 2);

任何帮助是极大的赞赏。谢谢托马斯

4

2 回答 2

4

如果您只想将“and”放在最后一个单词的前面,您可以使用将字符串拆分为字符串数组,更改最后一个单词并将字符串重新连接在一起。它看起来像这样

string[] skills = skillset.Split(new char[] { ',' });
skills[skills.Length-1] = "and " + skills[skills.Length-1];
skillset = string.Join(",", skills);
于 2012-10-20T21:50:33.413 回答
0
int myStringLength  = myString.length;

string myString = inputString.Substring(0, myStringLength);

int index = myString.LastIndexOf(' ');


string outputString = myString.Insert(index , " and ");

示例: http: //www.dotnetperls.com/insert

于 2012-10-21T12:56:21.707 回答