Instance 1
* Stringhelper. split ("1-2-3", "-"); <br>
* Result: {"1", "-", "2", "-", "3"} <br>
* Stringhelper. split ("-1--2-", "-"); <br>
* Result: {"", "-", "1", "-", "", "-", "2", "-", ""} <br>
* Stringhelper. split ("123", ""); <br>
* Result: {"123"} <br>
* Stringhelper. split ("1-2--3---4----5", "--"); <br>
* Result: {"1-2", "--", "3", "--", "-4 ","--","","--", "5"} <br>
Public static string [] split (string s, string delimiter ){
Int delimiterlength;
// The next statement has the side effect of throwing a null pointer
// Exception if s is null.
Int stringlength = s. length ();
If (delimiter = null | (delimiterlength = delimiter. length () = 0 ){
Return new string [] {s };
}
Int count;
Int start;
Int end;
// Scan s and count the tokens.
Count = 0;
Start = 0;
While (end = s. indexof (delimiter, start ))! =-1 ){
Count ++;
Start = end + delimiterlength;
}
Count ++;
// Allocate an array to return the tokens,
// We now know how big it shocould be
String [] result = new string [count];
// Scan s again, but this time pick out the tokens
Count = 0;
Start = 0;
While (end = s. indexof (delimiter, start ))! =-1 ){
Result [count] = (s. substring (start, end ));
Count ++;
Start = end + delimiterlength;
}
End = stringlength;
Result [count] = s. substring (start, end );
Return (result );
}
Instance 2
Public static string [] splitincludedelimiters (string s, string delimiter ){
Int delimiterlength;
Int stringlength = s. length ();
If (delimiter = null | (delimiterlength = delimiter. length () = 0 ){
Return new string [] {s };
}
Int count;
Int start;
Int end;
// Scan s and count the tokens.
Count = 0;
Start = 0;
While (end = s. indexof (delimiter, start ))! =-1 ){
Count + = 2;
Start = end + delimiterlength;
}
Count ++;
// Allocate an array to return the tokens,
// We now know how big it shocould be
String [] result = new string [count];
// Scan s again, but this time pick out the tokens
Count = 0;
Start = 0;
While (end = s. indexof (delimiter, start ))! =-1 ){
Result [count] = (s. substring (start, end ));
Count ++;
Result [count] = delimiter;
Count ++;
Start = end + delimiterlength;
}
End = stringlength;
Result [count] = s. substring (start, end );
Return (result );
}
}