1. mysplit class
/// <Summary>
/// Mysplit class
/// </Summary>
Public Class Mysplit: ienumerable
{
Private String [] Elements;
Public mysplit ( string source, char [] delimiters)
{< br> elements = source. split (delimiters);
}
Ienumerator ienumerable. getenumerator ()
{
Return NewMyenumerator (This);
}
# Region Implement the ienumerator interface in the nested class
/// <Summary>
/// Implement the ienumerator interface in the nested class to facilitate the creation of multiple enumerations in the future
/// </Summary>
Public Class Myenumerator: ienumerator
{
Private Int Position = - 1 ;
Private Mysplit T;
PublicMyenumerator (mysplit T)
{
This. T=T;
}
Public Bool Movenext ()
{
If (Position < T. elements. Length - 1 )
{
Position ++ ;
Return True ;
}
Else
{
Return False ;
}
}
Public VoidReset ()
{
Position= -1;
}
Object Ienumerator. Current
{
Get
{
Try
{
Return T. elements [position];
}
Catch (Indexoutofrangeexception)
{
Throw New Invalidoperationexception ();
}
}
}
}
# Endregion
}
2. Use
Mysplit = New Mysplit ( " Soybean boys: I love you! " , New Char [] { ' ' , ' - ' });
Foreach ( String Item In Mysplit)
{
Console. writeline (item );
}
3. output results
Soybean boys:
I
Love
You!
RelatedArticle: Implement index indicator in C #
Address: http://www.cnblogs.com/anjou/archive/2007/07/05/807357.html