2013-04-24 90 views
4

我想一個字符串分割成字符分割字符串轉換成char和在C#中添加「......」後

這是我的字符串

‘非常好的網站堆棧溢出’

,我想這個字符串轉換

第一個字,第二分割成字符

...... .. .. .. .. .. .. .. ..堆棧.. .. .. .. .. .. .. .. .. .. .. .. .. .. ..溢出.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..很.. .. .. .. .. .. .. .. .. .. .. .. ..好.. .. .. ..g .. .. .. .. .. .. .. .. .. ..網站.. .. .. .. .. .. .. .. .. .. .. .. .. 。我.. ..t .. ..e ..

我使用自然閱讀器軟件,使聽寫的MP3文件,拼寫

即使用我的程序

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); 
       } 
      } 
     } 
    } 
} 
+1

快速的方法:按空間分割。複製所有單詞。然後獲取所有偶數索引,並插入空格和點。然後加入結果。 – Aphelion 2013-04-24 09:02:40

+0

這看起來與您之前詢問的問題非常相似(http://stackoverflow.com/q/16185189/745969) - 答案中的相同原則適用於此處。 – Tim 2013-04-24 09:04:16

回答

5

LINQ你可以這樣做:

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); 

(順便說一下它的堆棧溢出)

輸出繼電器將是:

的...的...^h ... E ...股票內容S ... t ... o ... c ... k ...溢出... o ... v ... e ... r ... f ... l .. .o。 ..w ... ...在1 ... N ... ...很配...è... R ... Y ... ...好...摹的... Ø... d ...網站... W上。 ..e。B內容S ...我...的... E. ..

(請記住,包括using System.Linq;

2

使簡單

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 
3

您可以使用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