Introduction to asp.net string truncation functions

Source: Internet
Author: User

Character truncation function

The code is as follows: Copy code

/// <Summary>
/// Cut the specified string by the specified length,
/// </Summary>
/// <Param name = "oldStr"> string to be truncated </param>
/// <Param name = "maxLength"> maximum length of a string </param>
/// <Param name = "endWith"> suffix that exceeds the length </param>
/// <Returns> If the length is exceeded, a new string with a suffix is returned. Otherwise, the original string is returned. </returns>
Public static string StringTruncat (string oldStr, int maxLength, string endWith)
        {
If (string. IsNullOrEmpty (oldStr ))
// Throw new NullReferenceException ("the original string cannot be blank ");
Return oldStr + endWith;
If (maxLength <1)
Throw new Exception ("The Returned string length must be greater than [0]");
If (oldStr. Length> maxLength)
            {
String strTmp = oldStr. Substring (0, maxLength );
If (string. IsNullOrEmpty (endWith ))
Return strTmp;
Else
Return strTmp + endWith;
            }
Return oldStr;
        }

Call method

The code is as follows: Copy code

<Div> <% # StringTruncat (Eval ("Title"). ToString (), 12, "...") %> </div>

Example 2

The code is as follows: Copy code

Public string GetContent (string str, string start, string last, int n)
    {
If (str. ToLower (). IndexOf (start. ToLower ()> = 0)
        {
If (str. ToLower (). IndexOf (last. ToLower ()> = 0)
            {
Switch (n)
                {
// Intercept both the left and right (both take the front) (including keywords)
Case 1: str = str. substring (str. toLower (). indexOf (start. toLower (), str. length-str. toLower (). indexOf (start. toLower (); str = str. substring (0, str. toLower (). indexOf (last. toLower () + last. length); break;
// Intercept both the left and right (both take the front) (remove keywords)
Case 2: str = str. substring (str. toLower (). indexOf (start. toLower () + start. length, str. length-str. toLower (). indexOf (start. toLower ()-start. length); str = str. substring (0, str. toLower (). indexOf (last. toLower (); break;
// Intercept both the left and right (both get the back) (including keywords)
Case 3: str = str. substring (str. toLower (). lastIndexOf (start. toLower (), str. length-str. toLower (). lastIndexOf (start. toLower (); str = str. substring (0, str. toLower (). lastIndexOf (last. toLower () + last. length); break;
// Intercept both the left and right (both obtain the back) (remove keywords)
Case 4: str = str. substring (str. toLower (). lastIndexOf (start. toLower () + start. length, str. length-str. toLower (). lastIndexOf (start. toLower ()-start. length); str = str. substring (0, str. toLower (). lastIndexOf (last. toLower (); break;
// Intercept both left and right (first and last) (including keywords)
Case 5: str = str. substring (str. toLower (). indexOf (start. toLower (), str. length-str. toLower (). indexOf (start. toLower (); str = str. substring (0, str. toLower (). lastIndexOf (last. toLower () + last. length); break;
// Intercept both left and right (first and last) (remove keywords)
Case 6: str = str. substring (str. toLower (). indexOf (start. toLower () + start. length, str. length-str. toLower (). indexOf (start. toLower ()-start. length); str = str. substring (0, str. toLower (). lastIndexOf (last. toLower (); break;
Default: str = ""; break;
                }
            }
Else
            {
Switch (n)
                {
// Only truncate to the left (take the previous one) (including keywords)
Case 7: str = str. Substring (0, str. ToLower (). IndexOf (start. ToLower () + start. Length); break;
// Only truncate to the left (take the previous one) (remove the keyword)
Case 8: str = str. Substring (0, str. ToLower (). IndexOf (start. ToLower (); break;
// Only truncate to the left (obtain the following) (including keywords)
Case 9: str = str. Substring (0, str. ToLower (). LastIndexOf (start. ToLower () + start. Length); break;
// Only truncate to the left (obtain the following) (remove the keyword)
Case 10: str = str. Substring (0, str. ToLower (). LastIndexOf (start. ToLower (); break;
// Only truncate to the right (take the previous part) (including keywords)
Case 11: str = str. substring (str. toLower (). indexOf (start. toLower (), str. length-str. toLower (). indexOf (start. toLower (); break;
// Only truncate to the right (take the previous one) (remove the keyword)
Case 12: str = str. substring (str. toLower (). indexOf (start. toLower () + start. length, str. length-str. toLower (). indexOf (start. toLower ()-start. length); break;
// Only truncate to the right (obtain the following) (including keywords)
Case 13: str = str. substring (str. toLower (). lastIndexOf (start. toLower (), str. length-str. toLower (). lastIndexOf (start. toLower (); break;
// Only truncate to the right (obtain the back part) (remove the keyword)
Case 14: str = str. substring (str. toLower (). lastIndexOf (start. toLower () + start. length, str. length-str. toLower (). lastIndexOf (start. toLower ()-start. length); break;
Default: str = "none"; break;
                }
            }
        }
Else
        {
Str = "none! ";
        }
Return str

}

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.