Hi nauna,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string data = "AdminReferal >> disone >> distwo >> disfive >> usertwo";
Response.Write(SplitGetLastTwo(data, ">>"));
Response.Write("<br/>");
data = "AdminReferal >> disone";
Response.Write(SplitGetLastTwo(data, ">>"));
Response.Write("<br/>");
data = "AdminReferal >> disone >> distwo";
Response.Write(SplitGetLastTwo(data, ">>"));
}
private static string SplitGetLastTwo(string data, string separator)
{
string[] options = new string[] { separator };
string[] splitedData = data.Split(options, StringSplitOptions.None);
string value = "";
if (splitedData.Length >= 2)
{
value = splitedData[splitedData.Length - 2] + separator + splitedData[splitedData.Length - 1];
}
else
{
value = string.Join(separator, splitedData);
}
return value;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim data As String = "AdminReferal >> disone >> distwo >> disfive >> usertwo"
Response.Write(SplitGetLastTwo(data, ">>"))
Response.Write("<br/>")
data = "AdminReferal >> disone"
Response.Write(SplitGetLastTwo(data, ">>"))
Response.Write("<br/>")
data = "AdminReferal >> disone >> distwo"
Response.Write(SplitGetLastTwo(data, ">>"))
End Sub
Private Shared Function SplitGetLastTwo(ByVal data As String, ByVal separator As String) As String
Dim options As String() = New String() {separator}
Dim splitedData As String() = data.Split(options, StringSplitOptions.None)
Dim value As String = ""
If splitedData.Length >= 2 Then
value = splitedData(splitedData.Length - 2) & separator & splitedData(splitedData.Length - 1)
Else
value = String.Join(separator, splitedData)
End If
Return value
End Function
Output
disfive >> usertwo
AdminReferal >> disone
disone >> distwo