Gets a string of the specified length starting at the specified position in the string, supporting Chinese characters in English with a 2-byte count

Source: Internet
Author: User

#region function: getsubstring () Action: Gets a string of the specified length at the beginning of the specified position in the string, supporting Chinese characters in English mixed Chinese characters to 2 byte count//<summary>///Gets the specified position in the string starting at the specified        The length of the string, supporting Chinese characters in English mixed Chinese characters is 2 byte count///</summary>//<param name= "strsub" > Input mixed Strings in English </param>        <param name= "Start" > Start intercept </param>//<param name= "Length" > Length of string to intercept </param>        <returns></returns> public static string getsubstring (string strsub, int start, int length)            {String temp = strsub;            Int j = 0, k = 0, p = 0; Charenumerator CE = temp.            GetEnumerator (); while (CE. MoveNext ()) {j + = (CE. Current > 0 && CE. Current < 255)?                1:2;                if (J <= start) {p++;                        } else {if (j = = GetLength (temp)) { temp = temp.           Substring (p, k + 1);             Break                    } if (j <= length + start) {k++; } else {temp = temp.                        Substring (P, k);                    Break        }}} return temp; }///<summary>///To get the specified string length, Chinese characters are calculated in 2 bytes//</summary>//<param name= "AOr Gstr "> String to be Counted </param>//<returns></returns> private static int GetLength (String Aorgs            TR) {int intlen = aorgstr.length;            int i;            char[] chars = Aorgstr.tochararray (); for (i = 0; i < chars. Length; i++) {if (System.Convert.ToInt32 (chars[i]) > 255) {Intlen                ++;        }} return Intlen; } #endregion

Gets a string of the specified length starting at the specified position in the string, supporting Chinese characters in English with a 2-byte count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.