You must Sign In to post a response.
  • Category: .NET

    Splitting of string in c# language

    Hello.

    I have requirement to split the string into array. For better understanding, take a look at below string.

    string str1 = "Solve OP1 + OP2 and |*OP1 + OP2*| and find out answer of |*OP1 / OP2*| with correct logic.";

    In output, I expect array with below items.

    Solve OP1 + OP2 and
    |*OP1 + OP2*|
    and find out answer of
    |*OP1 / OP2*|
    with correct logic.


    I want to separate out all values within "|* *|" and create array with text which is out of |* *|.


    Thanks
  • #769567
    Hi,
    Please find code snippet.


    // Method invoke Place
    String strInput= "Solve OP1 + OP2 and |*OP1 + OP2*| and find out answer of |*OP1 / OP2*| with correct logic.";
    StringSplit(strInput);


    private static void StringSplit(string strInput)
    {
    List<string> lstOutput = new List<string>();

    string currentStr = string.Empty;
    currentStr = strInput;
    string splitStart = "|*";
    string splitEnd = "*|";

    while (currentStr != string.Empty)
    {
    if(currentStr.Substring(0,2) != splitStart)
    {
    string strnew ="";
    if(currentStr.IndexOf(splitStart)>0)
    {
    strnew = currentStr.Substring(0, currentStr.IndexOf(splitStart));
    currentStr = currentStr.Substring( currentStr.IndexOf(splitStart), currentStr.Length - currentStr.IndexOf(splitStart));
    }
    else
    {
    strnew = currentStr;
    currentStr ="";
    }
    lstOutput.Add(strnew);
    }
    else if (currentStr.Substring(0,2) == splitStart)
    {
    string strnew= currentStr.Substring(0, currentStr.IndexOf(splitEnd)+2);
    lstOutput.Add(strnew);
    currentStr =currentStr.Substring(currentStr .IndexOf(splitEnd)+2, currentStr.Length - currentStr.IndexOf(splitEnd)-2);
    }
    } //end of while


    //binding Output
    foreach( string s in lstOutput)
    {
    Console.WriteLine(s);
    }

    }

    Regards,Mahe
    Happy Coding

  • #769568
    Hi,

    You can use the following logic if you have given string format fixed.

    public string[] GetArray(string strdata)
    {
    // Solve OP1 +OP2 and
    // | *OP1 + OP2 *|
    // and find out answer of
    // | *OP1 / OP2 *|
    // with correct logic.


    strdata = "Solve OP1 + OP2 and |*OP1 + OP2*| and find out answer of |*OP1 / OP2*| with correct logic.";


    int iCounter = 0;
    int firstIndex = strdata.IndexOf("|*");
    int secondIndex = (strdata.IndexOf("*|") - firstIndex) + 2;


    string part1 = strdata.Substring(0, firstIndex);
    string part2 = strdata.Substring(firstIndex, secondIndex);
    string part3 = strdata.Substring((secondIndex + firstIndex), firstIndex + 3);
    iCounter = part1.Length + part2.Length + part3.Length;
    string part4 = strdata.Substring(iCounter, secondIndex + 1);
    iCounter += part4.Length;
    string part5 = strdata.Substring(iCounter, strdata.Length - iCounter);
    string separator = "::";

    string finalData = string.Format("{0}" + separator + "{1}" + separator + "{2}" + separator + "{3}" + separator + "{4}", part1, part2, part3, part4, part5);

    return finalData.Split(separator);

    }

    let me know if you have any issue.

    - Pavan Pareta


  • Sign In to post your comments