4

我想将一个字符串拆分为 char

那是我的弦

“非常好的网站中的堆栈溢出”

我想转换这个字符串

喜欢

第一个词和第二个拆分成字符

the.. .. .. t.. ..h.. ..e.. .. stack.. .. ..s.. ..t.. ..a.. ..c.. ..k .. .. 溢出.. .. .. ..o.. ..v.. ..e.. ..r.. ..f.. ..l.. ..o.. ..w.. . ..in.. .. ..i.. ..n.. .. 非常.. .. ..v.. ..e.. ..r.. ..y.. .. 好.. .. ..g.. ..o.. ..o.. ..d.. .. 网站.. .. ..w.. ..e.. ..b.. ..s.. ..i .. ..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);
                }
            }
        }
    }
}
4

3 回答 3

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

(顺便说一下它的堆栈溢出)

输出将是:

the...t...h...e...stock...s...t...o...c...k...overflow...o...v。 ..e...r...f...l.. .o...w...in...i...n...非常...v...e... r...y...good...g...o...o...d...website...w. ..e....b....s....i....t....e....

(记得包括using System.Linq;

于 2013-04-24T09:05:37.950 回答
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);

演示

于 2013-04-24T09:09:41.257 回答
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
于 2013-04-24T09:07:10.780 回答