Guys, help in C # to make the addition of a character in front of each character. Example: "tour" -We are given such a word, and we have to redo it, as it were. Answer: ".tour" -and that would be a point before and after each character. How to do? C #

    3 answers 3

    Here is a simple example of a console application. The static AddPresymbols method takes two agruments β€” the source word as a string and one character to be added before each symbol of the source word, and returns the result β€” a modified string.

     using System; using System.Linq; using System.Text; namespace ConsoleApplication { class Program { public static String AddPresymbols(String word, Char presymbol) { var result = new StringBuilder(); word.ToList().ForEach(letter => result.Append($"{presymbol}{letter}")); result.Append($"{presymbol}"); return result.ToString(); } static void Main(string[] args) { var word = "tour"; var modifiedWord = AddPresymbols(word, '.'); Console.WriteLine(modifiedWord); } } } 
    • Added an edit so that the desired character is also added to the end of the line. - klutch1991
    • Thank you. I'll see your code in detail right now. - TherionRider
    • one
      And why is the last character a point, and not presymbol? - vp_arth
    • @vp_arth, fixed - klutch1991

    The solution algorithm is simple: create a new line and add to it alternately a separator character or a character from the source line.

    To implement, you can use the fact that the string implements the interface IEnumerable`, which means that you can use the Aggregate function

    This function allows you to go through the collection and accumulate the result of the passage in the battery.

     var result = word.Aggregate(new StringBuilder(word.Length * 2 + 1), // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ пустой StringBuilder (sb, c) => sb.Append($".{c}"), // Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ добавляСм Π² StringBuilder ΠΏΠ°Ρ€Ρƒ символов: Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ символа строки sb => sb.Append(".").ToString()) // ДобавляСм Π·Π°ΠΊΠ»ΡŽΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΡƒΡŽ Ρ‚ΠΎΡ‡ΠΊΡƒ ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰ΡƒΡŽ строку 

    An alternative would be the following:

     var result = word.Aggregate(new StringBuilder(".", word.Length * 2 + 1), // создаСм StringBuilder, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΡƒΠΆΠ΅ Π΅ΡΡ‚ΡŒ символ Ρ‚ΠΎΡ‡ΠΊΠΈ (sb, c) => sb.Append($"{c}."), // Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ добавляСм Π² StringBuilder ΠΏΠ°Ρ€Ρƒ символов: Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ символа строки sb => sb.ToString()); // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰ΡƒΡŽ строку 

    As suggested in the comments: for large lines, instead of interpolated lines, it is better to use consecutive calls to Append, i.e.

     (sb, c) => Append(".").sb.Append(c) 

    or

     (sb, c) => sb.Append(c).Append(".") 

    depending on the previously selected option


    In addition to Aggregate, you can use the Join function , but since this function inserts separators only inside the line, you will need to add points in front and behind, for example:

     var result = $".{string.Join<char>(".", word)}." 

    In this case, you must explicitly indicate that the collection has type char, otherwise an overload will be used for the string, and the dots will not be inserted anywhere.

    Or you can call AsEnumerable

     var result = $".{string.Join(".", word.AsEnumerable())}." 

    In this case, you can omit the char type, since we explicitly pass an IEnumerable<char> and automatic type inference works.

    • one
      There is a suspicion that on Append with interpolation, the memory consumption will double, stupidly at the expense of boxing char (interpolation uses it, checked). You can fix just two consecutive Append in lambda, instead of one, for example, so word.Aggregate(new StringBuilder(word.Length * 2 + 1),(sb, c) => sb.Append(".").Append(c),sb => sb.Append(".").ToString()) - rdorn
    • checked, it is, sequential Append is better - rdorn
     class Program { static void Main(string[] args) { string f = SplitStringByDot(Console.ReadLine()); Console.WriteLine(f); Console.ReadKey(); } public static string SplitStringByDot(string s) { return string.IsNullOrWhiteSpace(s) ? "WRONG!" : string.Format(".{0}.", string.Join(".", s.ToCharArray())); } } 
    • ToCharArray can not be done so as not to create an extra array - Grundy
    • @Grundy thanks, drew attention to your answer - slippyk