About string interception

Source: Internet
Author: User

String interception I believe we are not strangers. But in the same string we need to intercept different data, such as when the data in text is converted into a table.

These data may include Chinese and so on.

This time if you use String. Length () You will find that the length of the string will be different as long as the number of Chinese is different.

Here is the code:

        <summary>///intercept string///</summary>//<param name= "strline" > intercepted string &lt         ;/param>//<param name= "dt" > Filled table </param>///<param name= "weight" > Intercept length </param> private void Getdatatodatatable (string strline,datatable dt,int[] weight) {DataRow Dr=dt.            NewRow ();            int sum=0;            int count = 0;            ArrayList num = new ArrayList (); char[] temp = strline.            ToCharArray (); for (int i = 0; i < temp. Length; i++) {if (((int) temp[i]) >255)///greater than 255 are Chinese characters or special characters {num.                ADD (i); }} for (int i = 0; i < dt. Columns.count;          i++) {count = 0; for (int y = 0; y < Num. Count;                    y++) {if (Convert.ToInt32 (Num[y]) >=sum&&convert.toint32 (Num[y]) <=sum+weight[i]) {Count++; }} dr[i]=strline.          Substring (Sum,weight[i]-count);  Sum+=weight[i]-count; }        }

This excludes the issue of the placeholder for Chinese characters.

When you calculate the overall length, the Chinese character is the position of two characters, but when you get the length of the string (string.length), only one character length is counted, which creates a positional bias.

In the process of interception, we first find the Chinese characters contained in the truncated string. This will be known to reduce the length of interception.

About string interception

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.