String. substring Method
Description
String. substring (int32) retrieves a substring from this instance. The substring starts from the specified character position.
String. substring (int32, int32) retrieves a substring from this instance. The substring starts from the specified character position and has the specified length.
Example:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
String S = "Hello C # World! ";
// S1 is a substring after 3 characters are intercepted from S, and 3 represents the starting character position of the substring.
String S1 = S. substring (3 );
Console. writeline (S1 );
// S2 is a string of 2 characters starting from 6 Characters intercepted in S, 6 indicates the starting character position of sub-characters, and 2 indicates the length of sub-characters.
String S2 = S. substring (6, 2 );
Console. writeline (S2 );
}
}
}
The result is as follows:
Lo C # World!
C #
Attach a C # string truncation function.
Public String getstring (string rawstring, int32 length)
{
If (rawstring. Length <= length)
{
Return rawstring;
}
Else
{
For (int32 I = rawstring. Length-1; I> = 0; I --)
{
If (system. Text. encoding. getencoding ("gb2312"). getbytecount (rawstring. substring (0, I) <length)
{
Return rawstring. substring (0, I) + "...";
}
}
Return "...";
}
}
1/** // <summary>
2 // intercept a string without specifying the length of the string
3 /// </Summary>
4 /// <Param name = "str"> string to be truncated </param>
5 /// <Param name = "len"> length of each line, more than the length of this line automatically wrap </param>
6 /// <returns> </returns>
7 Public String cutstr (string STR, int Len)
8 {string S = "";
9
10 For (INT I = 0; I <Str. length; I ++)
11 {
12 INT r = I % Len;
13 int last = (Str. Length/Len) * Len;
14 if (I! = 0 & I <= last)
15 {
16
17 if (r = 0)
18 {
19 s + = Str. substring (I-Len, Len) + "<br> ";
20}
21
22}
23 else if (I> last)
24 {
25 s + = Str. substring (I-1 );
26 break;
27}
28
29}
30
31 return S;
32
33}
34
35
36/*** // <summary>
37 // capture the string and limit the length of the string, more than the given length +...
38 /// </Summary>
39 // <Param name = "str"> string to be intercepted </param>
40 /// <Param name = "len"> length of each line, more than the length of this line automatically wrap </param>
41 // <Param name = "Max"> Maximum length of the output string </param>
42 // <returns> </returns>
43 Public String cutstr (string STR, int Len, int max)
44 {
45 string S = "";
46 string Sheng = "";
47 If (Str. length> MAX)
48 {
49 STR = Str. substring (0, max );
50 Sheng = "";
51}
52 for (INT I = 0; I <Str. length; I ++)
53 {
54 int r = I % Len;
55 int last = (Str. Length/Len) * Len;
56 if (I! = 0 & I <= last)
57 {
58
59 If (r = 0)
60 {
61 s + = Str. substring (I-Len, Len) + "<br> ";
62}
63
64}
65 else if (I> last)
66 {
67 S + = Str. substring (I-1 );
68 break;
69}
70
71}
72
73 return S + Sheng;
74
75}