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

      1. <small id='GVS9o'></small><noframes id='GVS9o'>

          <bdo id='GVS9o'></bdo><ul id='GVS9o'></ul>

        <tfoot id='GVS9o'></tfoot>
      2. 如何获取字符串的前五个字符

        How to get the first five character of a String(如何获取字符串的前五个字符)

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

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

                  本文介绍了如何获取字符串的前五个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我已阅读此问题以获取字符串的第一个字符.有没有办法从 C# 中的字符串中获取前 n 个字符?

                  I have read this question to get first char of the string. Is there a way to get the first n number of characters from a string in C#?

                  推荐答案

                  你可以使用 Enumerable.Take 喜欢:

                  char[] array = yourStringVariable.Take(5).ToArray();
                  

                  或者您可以使用 String.子字符串.

                  string str = yourStringVariable.Substring(0,5);
                  

                  请记住,如果字符串的长度小于所需的字符,String.Substring 可能会引发异常.

                  Remember that String.Substring could throw an exception in case of string's length less than the characters required.

                  如果您想以字符串形式返回结果,则可以使用:

                  If you want to get the result back in string then you can use:

                  • 使用String Constructor和LINQ的Take

                  string firstFivChar = new string(yourStringVariable.Take(5).ToArray());
                  

                  该方法的优点是不会事先检查长度.

                  The plus with the approach is not checking for length before hand.

                  • 另一种方法是使用 String.Substring 进行错误检查
                  • The other way is to use String.Substring with error checking

                  喜欢:

                  string firstFivCharWithSubString = 
                      !String.IsNullOrWhiteSpace(yourStringVariable) && yourStringVariable.Length >= 5
                      ? yourStringVariable.Substring(0, 5)
                      : yourStringVariable;
                  

                  这篇关于如何获取字符串的前五个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <bdo id='nmizu'></bdo><ul id='nmizu'></ul>

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

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

                    • <legend id='nmizu'><style id='nmizu'><dir id='nmizu'><q id='nmizu'></q></dir></style></legend>