Using System. Text. RegularExpressions;
/// <Summary>
/// Calculate the number of times a substring appears.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "substring"> substring </param>
/// <Returns> Number of occurrences </returns>
Public static int SubstringCount (string str, string substring)
{
If (str. Contains (substring ))
{
String strReplaced = str. Replace (substring ,"");
Return (str. Length-strReplaced. Length)/substring. Length;
}
Return 0;
}
// Call example. Here is only a reference.
Private void FindPosition ()
{
STemp = "aabbccdd \ r \ naa3323 \ r \ naa325sdf \ r \ n233asdf554 \ r \ n"; // here is the original string
Int iCount = SubstringCount (sTemp, "\ r \ n"); // here \ r \ n is the delimiter
If (iCount> 3)
{
Regex reg = new Regex ("\ r \ n ");
Match mat = reg. Match (sTemp );
Int iCountTemp = 0;
While (mat. Success)
{
ICountTemp ++;
// MessageBox. Show (mat. Index. ToString (); // location
If (iCountTemp> 20)
{
STemp = sTemp. Substring (0, mat. Index );
Break;
}
Mat = reg. Match (sTemp, mat. Index + mat. Length );
}
}
}
From the column keenweiwei