-1

I want to space after certain string. for example:

string str = "DASEULE"

Now, I want to add a space after "DAS" and it will write to UI Text as "DAS EULE". How can i do this? Thanks

Edit: the all chars are uppercase. Sorry

freedom667
  • 29
  • 3
  • 9
  • I have a feeling I read your question wrong after leaving an answer. You want to add space if the there is capital letter in the string? – Programmer Mar 05 '18 at 08:14
  • Actually the all chars are uppercase. there are artikels. "Das","Die","Der" etc. I solved myself but I think it is not stable. I'll try another solving, maybe the following answers – freedom667 Mar 05 '18 at 08:18
  • *"Actually. The all chars are uppercase. it has "DASEULE" instead of "DasEule""* Please fix your question. The example you used is a bit confusing. Check ren's answer. That might be what you are looking for. – Programmer Mar 05 '18 at 08:23
  • fixed. Sorry again, it was my fault – freedom667 Mar 05 '18 at 08:27
  • No problem. The big question is how do we know when to add space since there other different keywords like DIE and DER...... Do you have list of all these words that requires space after them? – Programmer Mar 05 '18 at 08:31
  • I wrote this code: switch (wordPanel.GetChild(0).GetComponentInChildren().text) { case "DAS": text.text = spacing.Replace("DAS", "DAS "); break; default: text.text = spacing; break; } How is this? according to you, can be? – freedom667 Mar 05 '18 at 08:34
  • 1
    As long as all input is all uppercase there is no way to suggest a generic approach, you couldn't differenciate between two separate words `THECAT` should be `THE CAT` and correct one words like `THERMAL` or `THEORY`. – Cleptus Mar 05 '18 at 08:46
  • you're right. but I have no other choice. anyway, the artikels won't be in game. neverthless. thanks. – freedom667 Mar 05 '18 at 10:18

2 Answers2

0

Try:

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

Or

string s2 = "DasEule";
s2.Insert(2, " ");
Ren
  • 79
  • 7
  • your code can be. it works but I don't know is this stable. because there are more artikels, not only "DAS" – freedom667 Mar 05 '18 at 08:24
0

Not the best code in the world, check the problem mentioned in the question comments, but should work. Is a variant of Ren's answer.

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;
}
Cleptus
  • 3,446
  • 4
  • 28
  • 34