[Original] SubString () function extension

Source: Internet
Author: User

First, let's look at two places that need to use SubString:

1. in C #, we often need to reduce characters and use "... "To replace the reduced part, we usually use (string ). subString (), and then we can reduce it as follows:

Str = (str. Length> 10 )? Str. SubString (0, 10) + "...": str;

The advantage of such writing is that it is easy to think of, easy to understand, and easy to write.
The disadvantage of writing is that the format is relatively rigid, not easy to reuse, and the writing efficiency is low.

2. When I develop some projects, I often have the requirement that a string of N length should take the last few characters or specify the start and end characters in the middle, the rest are used "... "indicates that complicated judgment is often required at this time, and a (string) is used at the end ). subString ().

I will provide a solution that can solve both flexibility and tedious judgment at the same time:

 

1/** // <summary>
2 // The fixed length of the area string, and the rest are omitted
3 ///
4 // rules:
5 // 1. If startIndex is greater than the string length, it is automatically adjusted to the last maxLangth length. If maxLangth length is greater than the string length, startIndex returns to 0.
6 // 2. If the length of maxLangth is greater than the length of the specific string based on startIndex, maxLangth automatically obtains the maximum value, that is, from startIndex to the end of the string.
7 // 3. In the result, strings are replaced by "..." whenever there is a cut.
8 /// </summary>
9 /// <param name = "str"> original string </param>
10 /// <param name = "startIndex"> Start string position </param>
11 /// <param name = "maxLangth"> maximum number of characters </param>
12 /// <returns> </returns>
13 public static string SubString (string str, int startIndex, int maxLangth)
14 {
15 string substring = "";
16
17 // adjust startIndex
18 if (startIndex> str. Length-1) // if startIndex is greater than the string Length
19 {
20 startIndex = (str. Length-maxLangth> 0 )? Str. Length-maxLangth: 0; // The value is automatically adjusted to the last maxLangth Length. If maxLangth length is greater than the string length, startIndex returns to 0.
21}
22
23 // adjust maxLangth
24 if (startIndex + maxLangth> str. Length) // if the Length of maxLangth is greater than the Length of the string based on startIndex
25 {
26 maxLangth = str. Length-startIndex; // then maxLangth automatically obtains the maximum possible value, that is, the value from startIndex until the end of the string.
27}
28 // adjusted
29
30 // scale down symbol
31 substring + = (startIndex> 0 )? "..": ""; // If it is cut at the beginning, it should be replaced "..."
32
33 // obtain a fixed-length string
34 substring + = str. Substring (startIndex, maxLangth );
35
36 // scale-down symbol
37 substring + = (str. Length-startIndex-maxLangth> 0 )? "..": ""; // If the end is cut, replace it "..."
38
39 return substring;
40}
41

You can put this program in a separate class library, such as Common. StringHandle. Because the static method is used, you only need to call it directly as follows:

Str = Common. StringHandle. SubString (str, 7, 8 );

The processing mechanism and parameters are described in detail at the time of writing. Here are some simple examples:

Requirement: In the string "0123456789", take the first five characters, and use "..." for the rest.
Code: Common. StringHandle. SubString ("0123456789)
Output result: 01234 ..

Requirement: In the string "0123456789", take the 3 characters starting with "5" (if less than 3 characters are used, take all). If there are other characters later, use "..." instead.
Code: Common. StringHandle. SubString ("0123456789", 5, 3)
Output result:... 567 ..

Requirement: In the string "0123456789", take the 10 characters starting with "5" (if less than 10 characters are used, take all). If there are other characters, use "..." instead.
Code: Common. StringHandle. SubString ("0123456789)
Output result: .. 56789

I am only providing a way of thinking here. There is still a lot of room for extension in this Code: for example, conventions ".. "form, implementation from the right to cut N characters, or in. in net3.5, the Extension Method is used to complete the tasks. (some of them have been implemented, but some of them may dilute the theme due to the addition. This is omitted, but we can discuss it with exceptions ).

I hope you can discuss it with us!

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.