简体   繁体   English

将字符串拆分为char并在C#后添加“...”

[英]split string into char and add “…” after in C#

i want to split a string into char 我想将一个字符串拆分为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 我正在使用自然的Reader软件并使用拼写制作听写mp3文件

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

using LINQ you can do: 使用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);

(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. 所述...吨... H ... E ...库存内容S ...吨... O ...用C ... K ...溢出... O ...诉.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... [R ... Y ... ...好...摹的...的... ... d网站... W上。 ..e...b...s...i...t...e... ..e。B内容S ...我...的... E. ..

(Remember to include using System.Linq; ) (记得包括using System.Linq;

You can use Linq: 你可以使用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 演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM