To intercept a Chinese-English mixed string of specified lengths in C #

Source: Internet
Author: User
Tags mixed
String

We often do one thing, is in the article system, the interception of a certain length of the article title, more than the specified length, add "..."

such as two strings:
String str1 = "Chinese people want AH abc ~";
String str2 = "1 Chinese 23456abc ~";

To intercept, after the output:

str1 = "Chinese people want ...";
str2 = "1 Chinese 2 ...";

That is, the string to be mixed in Chinese and English, after the interception, the length should be consistent, that is, 8 bytes length (excluding three points), and can not appear in the middle of the case of truncated. So I wrote a method:

public static string Getstr (String s,int L)
    {   
    string temp = s;
    if (regex.replace (temp, "[\u4e00-\u9fa5]", "ZZ", Regexoptions.ignorecase). LENGTH<=L)
    {
        return temp;
   }
    for (int i=temp. length;i>=0;i--)
    {
        temp = temp. Substring (0,i);
        if (regex.replace (temp, "[\u4e00-\u9fa5]", "ZZ", Regexoptions.ignorecase). length<=l-3)
        {
             return temp + "";
       }   
   }
    Return "";
   }
Call:
String content = "Chinese AH abc yes";
Content = Getstr (content,13);



Related Article

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.