user23755236
user23755236

Reputation: 11

Why does cycle works backward?

I have strings written in PascalCase. I want to add a space between both words. I done by using method below. First cycle should delete first word in char array and the second cycle - second. But the problem is - cycles are working backwads: a second word is deleted from array a and first word from b. Why is this happening?

Also in commented section for b it gives me out of range error for startindex.

private string skillName(string skillDef)
{
    int index = 0;
    bool firstUp = false;

    foreach (Char c in skillDef)
    {
        if (Char.IsUpper(c) && firstUp) 
            break;
        else if (Char.IsUpper(c) && !firstUp)
        {
            firstUp = true;
            index++;
        }
        index++;
    }

    //char[] a = skillDef.ToCharArray(0, index-1);

    //char[] b = skillDef.ToCharArray(index, skillDef.Length);

    char[] a = skillDef.ToCharArray(0, skillDef.Length);
    char[] b = skillDef.ToCharArray(0, skillDef.Length);

    for (int i = 0; i < index - 1; i++)
    {
        a[i] = ' ';
    }

    for (int i = index-1; i < b.Length; i++)
    {
        b[i] = ' ';
    }

    skillDef = new string(b).Trim();
    string myString = new string(a).Trim();

    skillDef += " " + myString;

    return skillDef;
}

Upvotes: 0

Views: 41

Answers (0)

Related Questions