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
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
- oneAnd 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.
- oneThere 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())); } } ToCharArraycan not be done so as not to create an extra array - Grundy- @Grundy thanks, drew attention to your answer - slippyk