Divide the string by char and add "......" after in C #

I want to split a string into char

this is my line

"stack overflow on a very good website"

and I want to convert this line

like

first word and second character division

...... t .... h .... e .... set ...... s .... t .... a .... c .... k. ... overflow ...... o .... v .... e .... r .... f .... l .... o .... w .... in ...... i .... n .... very ...... v .... e .... r .... y .... good ..... ..g .... o .... o .... d .... website ...... w .... e .... b .... s .... i .... t .... e ....

I use Natural Reader software and create spelling dictation mp3 file

this is my program

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace file { class Program { public static string fileLoc = @"C:\Users\Administrator\Desktop\sample1.txt"; public static string s; public static string data = "the Qaru in very good website"; private static void Main(string[] args) { Create_File(); Wrint_in_File(); Read_file(); add_comma(); s = Console.ReadLine(); } public static void Wrint_in_File() { if (File.Exists(fileLoc)) { using (StreamWriter sw = new StreamWriter(fileLoc)) { sw.WriteLine(DateTime.Now); sw.WriteLine(data); Console.WriteLine("Data is successfully save in File"); } } } public static void Create_File() { FileStream fs = null; if (!File.Exists(fileLoc)) { using (fs = File.Create(fileLoc)) { Console.WriteLine(@"File is Successfully Created at C:\Users\Administrator\Desktop\sample1.txt"); Console.ReadLine(); } } } public static void Read_file() { if (File.Exists(fileLoc)) { using (TextReader tr = new StreamReader(fileLoc)) { string s= tr.ReadToEnd(); Console.WriteLine(s); Console.ReadLine(); } } } public static void add_comma() { if (File.Exists(fileLoc)) { using (StreamWriter sw = new StreamWriter(fileLoc)) { sw.WriteLine(DateTime.Now); string txt =data.Replace(" ", ".. .. .. .. .. .. .. .."); sw.WriteLine(txt); Console.WriteLine(txt); } } } } } 
+4
source share
3 answers

using LINQ, you can:

 string str = "the stock overflow in very good website"; string separator = "..."; string joinedString = string.Join("", (str.Split() .Select(r=> r + separator + (string.Join(separator, r.ToCharArray())) +separator))); Console.WriteLine(joinedString); 

(By the way, stack overflow)

The conclusion will be:

... t ... h ... uh ... availability ... s ...... to ... s ... to ... Overflow ... o ... v ... e ... t ... e ... l ... o ... w ... c ... I ... p ... very ... d ... e ... g .. y ... well ... g ... o ... o ... d ... site ... w ... e ... b ... s ... i ... t ... e ...

(Remember to enable using System.Linq; )

+5
source

You can use Linq:

 string data = "the stock overflow in very good website"; IEnumerable<string> tokens = data.Split() .Select(w => string.Format("{0}...{1}", w , string.Join("...", w.Select(c => string.Format("{0}...", c))))); string result = string.Join(" ", tokens); 

Demo

+3
source

make it simple

  string data = "the Qaru is a very good website"; string []words = data.Split(' '); string finalString = string.Empty; string separator ="..."; foreach (string word in words) { finalString += word + separator; string temp = string.Empty; foreach (char c in word) { temp += c + separator; } finalString += temp + separator; temp = string.Empty; } //do whatever you want to do with finalString 
+2
source

All Articles