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!
From: http://www.cnblogs.com/szw/archive/2007/12/17/1003321.html