某个字符串后的间距(Unity3D)

问题描述 投票:-1回答:2

我希望在某些字符串之后留出空间。例如:

string str = "DASEULE"

现在,我想在"DAS"之后添加一个空格,它将写入UI Text作为"DAS EULE"。我怎样才能做到这一点?谢谢

编辑:所有字符都是大写的。抱歉

c# string unity3d spacing
2个回答
0
投票

尝试:

string s1 = "DasEule";
s1.Replace("Das", "Das ");

要么

string s2 = "DasEule";
s2.Insert(2, " ");

0
投票

不是世界上最好的代码,检查问题评论中提到的问题,但应该有效。是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;
}
© www.soinside.com 2019 - 2024. All rights reserved.