There is a method that inserts spaces before capital letters. There is an error: in 1 method call, the space is set only 1 time, even if there are a lot of capital letters.

BreakCamelCase("вывыВвывывы"); BreakCamelCase("вывыВвывDывы"); 

Expected to receive in response:

outflows
wyv vvyv dyvy

I receive:
outflows
VyVyvvyv Dyvy

Code:

 public static string BreakCamelCase(string str) { string n = null; for(int i = 0; i < str.Length;i++) { if (Char.IsUpper(str[i])) { n = str.Insert(i, ' '.ToString()); } } return n; } 

    2 answers 2

    This happens because at each iteration, if the condition if (Char.IsUpper(str[i])) triggered, you assign the initial value of the variable str to the variable n inserting a space at the desired position. Thus, you simply overwrite the previous changes in the variable n . To solve a problem, you need to write a string character by character, and not rewrite it again each time the condition is triggered (the answer below indicates the method, I will not duplicate the code). And yes, when character-wisely working with strings in a loop, it is highly desirable to use StringBuilder instead of string — because of the immutability of the strings, at each iteration of your loop a new one will be allocated and, moreover, more memory will be allocated to the resulting string. StringBuilder , in turn, allows you to append data without StringBuilder string

      You forget that strings are immutable (immutable).

      I would rewrite your code like this:

       public static string BreakCamelCase(string str) { string n = ""; for (int i = 0; i < str.Length; i++) { if (Char.IsUpper(str[i])) n += ' '; n += str[i]; } return n; } 

      Well, in the future, the transition to StringBuilder

      • Thanks for the answer, I understood the mistake, I will correct it somehow in my own way - Divannaya Analitik
      • @Andrew, the point here is still not immobility - the author simply overwrites the entire line entirely. If instead of string some other type, including mutable, a code of the form a = b would lead to similar consequences - DreamChild
      • @DreamChild, well, how to say, if the lines were mutable, they would have kept the spaces inserted earlier. Another question is that in the absence of large letters in a string, the function generally returns null, well, you need to take into account that when inserting, the length of the string changes - Andrey NOP
      • @Andrey, regardless of mutability or its absence in the above code, the variable is overwritten entirely. At each subsequent iteration, the line is assigned a new value, and the old one is erased, and not modified. For example, StringBuilder is mutable, but if in the code given by the author to replace the type of the variable n from string to StringBuilder , the problem will remain because at each iteration the variable n will be assigned a new value, while you need to modify the old one so that the changes made on previous iterations are saved - DreamChild
      • @DreamChild, imagine for a second that string mutable, then when you insert a space into it, it will remain in it and "move" to n, the next time we will add another space to the line and it will also remain in it and also to n. So I consider the remark about mutability appropriate, and further discussion of what is not - no) With StringBuilder, you can check for yourself and make sure of your mistake. ) - Andrey NOP