我希望在某些字符串之后留出空间。例如:
string str = "DASEULE"
现在,我想在"DAS"
之后添加一个空格,它将写入UI Text作为"DAS EULE"
。我怎样才能做到这一点?谢谢
编辑:所有字符都是大写的。抱歉
尝试:
string s1 = "DasEule";
s1.Replace("Das", "Das ");
要么
string s2 = "DasEule";
s2.Insert(2, " ");
不是世界上最好的代码,检查问题评论中提到的问题,但应该有效。是Ren的答案的变种。
private string AddSpacesIfNecessary(string word)
{
string[] prefixes = new string[] { "DAS", "DIE", "DER" }; // Add other prefixes
foreach(string prefix in prefixes)
{
if(word.StartsWith(prefix))
{
return word.Insert(prefix.Length - 1, " ");
}
}
// Prefixes are not found, no need to add spaces
return word;
}