1. <i id='2uaKf'><tr id='2uaKf'><dt id='2uaKf'><q id='2uaKf'><span id='2uaKf'><b id='2uaKf'><form id='2uaKf'><ins id='2uaKf'></ins><ul id='2uaKf'></ul><sub id='2uaKf'></sub></form><legend id='2uaKf'></legend><bdo id='2uaKf'><pre id='2uaKf'><center id='2uaKf'></center></pre></bdo></b><th id='2uaKf'></th></span></q></dt></tr></i><div id='2uaKf'><tfoot id='2uaKf'></tfoot><dl id='2uaKf'><fieldset id='2uaKf'></fieldset></dl></div>

      <small id='2uaKf'></small><noframes id='2uaKf'>

      <tfoot id='2uaKf'></tfoot>
      • <bdo id='2uaKf'></bdo><ul id='2uaKf'></ul>
    2. <legend id='2uaKf'><style id='2uaKf'><dir id='2uaKf'><q id='2uaKf'></q></dir></style></legend>

      如何拆分保留整个单词的字符串?

      时间:2023-08-27
      • <bdo id='I03zk'></bdo><ul id='I03zk'></ul>

          <tbody id='I03zk'></tbody>

          <small id='I03zk'></small><noframes id='I03zk'>

          <tfoot id='I03zk'></tfoot>
          <i id='I03zk'><tr id='I03zk'><dt id='I03zk'><q id='I03zk'><span id='I03zk'><b id='I03zk'><form id='I03zk'><ins id='I03zk'></ins><ul id='I03zk'></ul><sub id='I03zk'></sub></form><legend id='I03zk'></legend><bdo id='I03zk'><pre id='I03zk'><center id='I03zk'></center></pre></bdo></b><th id='I03zk'></th></span></q></dt></tr></i><div id='I03zk'><tfoot id='I03zk'></tfoot><dl id='I03zk'><fieldset id='I03zk'></fieldset></dl></div>

          <legend id='I03zk'><style id='I03zk'><dir id='I03zk'><q id='I03zk'></q></dir></style></legend>
              1. 本文介绍了如何拆分保留整个单词的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我需要将长句子分成保留整个单词的部分.每个部分都应该给出最大字符数(包括空格、点等).例如:

                I need to split long sentence into parts preserving whole words. Each part should have given maximum number of characters (including space, dots etc.). For example:

                int partLenght = 35;
                string sentence = "Silver badges are awarded for longer term goals. Silver badges are uncommon."
                

                输出:

                1 part: "Silver badges are awarded for"
                2 part: "longer term goals. Silver badges are"
                3 part: "uncommon."
                

                推荐答案

                试试这个:

                    static void Main(string[] args)
                    {
                        int partLength = 35;
                        string sentence = "Silver badges are awarded for longer term goals. Silver badges are uncommon.";
                        string[] words = sentence.Split(' ');
                        var parts = new Dictionary<int, string>();
                        string part = string.Empty;
                        int partCounter = 0;
                        foreach (var word in words)
                        {
                            if (part.Length + word.Length < partLength)
                            {
                                part += string.IsNullOrEmpty(part) ? word : " " + word;
                            }
                            else
                            {
                                parts.Add(partCounter, part);
                                part = word;
                                partCounter++;
                            }
                        }
                        parts.Add(partCounter, part);
                        foreach (var item in parts)
                        {
                            Console.WriteLine("Part {0} (length = {2}): {1}", item.Key, item.Value, item.Value.Length);
                        }
                        Console.ReadLine();
                    }
                

                这篇关于如何拆分保留整个单词的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将 .NET 控制台应用程序转换为 Winforms 或 WP 下一篇:C#:重定向控制台应用程序输出:如何刷新输出?

                相关文章

                最新文章

                <legend id='I6WH4'><style id='I6WH4'><dir id='I6WH4'><q id='I6WH4'></q></dir></style></legend>
              2. <i id='I6WH4'><tr id='I6WH4'><dt id='I6WH4'><q id='I6WH4'><span id='I6WH4'><b id='I6WH4'><form id='I6WH4'><ins id='I6WH4'></ins><ul id='I6WH4'></ul><sub id='I6WH4'></sub></form><legend id='I6WH4'></legend><bdo id='I6WH4'><pre id='I6WH4'><center id='I6WH4'></center></pre></bdo></b><th id='I6WH4'></th></span></q></dt></tr></i><div id='I6WH4'><tfoot id='I6WH4'></tfoot><dl id='I6WH4'><fieldset id='I6WH4'></fieldset></dl></div>

                <small id='I6WH4'></small><noframes id='I6WH4'>

                • <bdo id='I6WH4'></bdo><ul id='I6WH4'></ul>

                    <tfoot id='I6WH4'></tfoot>