String truncation method with fixed length (C #)
There is nothing special about this function, that is, it can intercept strings of a certain length. It may be a small feature that Len is a byte, which solves the problem that different Chinese characters and English bytes lead to different lengths directly intercepted.
1 string truncation function # region string truncation Function
2 public static string cutstring (string inputstring, int Len)
3 {
4
5
6 asciiencoding ASCII = new asciiencoding ();
7int templen = 0;
8 string tempstring = "";
9 byte [] S = ASCII. getbytes (inputstring );
10for (INT I = 0; I <S. length; I ++)
11 {
12If (INT) s [I] = 63)
13 {
14 templen + = 2;
15}
16 else
17 {
18 templen + = 1;
19}
20
21try
22 {
23 tempstring + = inputstring. substring (I, 1 );
24}
25 catch
26 {
27 break;
28}
29
30if (templen> Len)
31 break;
32}
33 // If yes, add half ellipsis
34 byte [] mybyte = system. Text. encoding. Default. getbytes (inputstring );
35if (mybyte. length> Len)
36 tempstring + = "... ";
37
38
39 return tempstring;
40}
41 # endregion
42