anil
anil

Reputation: 143

split string into char and add "......" after in C#

i want to split a string into char

that is my string

"the stack overflow in very good website"

and i want to convert this string

like

first word and second split into character

the.. .. .. t.. ..h.. ..e.. .. stack.. .. ..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 am using natural Reader software and making a dictation mp3 file with spelling

that 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 stack overflow 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);
                }
            }
        }
    }
}

Upvotes: 4

Views: 1936

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460078

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

Upvotes: 3

Manish Mishra
Manish Mishra

Reputation: 12375

make it simple

    string data = "the stack overflow 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

Upvotes: 2

Habib
Habib

Reputation: 223237

using LINQ you can do:

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 its stack overflow)

Ouput would be:

the...t...h...e...stock...s...t...o...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...

(Remember to include using System.Linq;)

Upvotes: 5

Related Questions