Learner
Learner

Reputation: 1376

How to split a string at the last '.' value

I have a string like

    string str = "123.1.1.QWE";
    string[] seqNum = textBox1.Text.Split('.');

I want to split the string at the last . value and have to split into two strings only like

seqNum[0]="123.1.1";
seqNum[1]="QWE";

How can I split it into two strings at the last index.

Thanks in advance.

Upvotes: 2

Views: 8739

Answers (2)

EncounterPal
EncounterPal

Reputation: 11

            string str = "123.1.1.QWE";
            string[] seqnum = new string[2];
            foreach (char ch in str)
            {
                if (char.IsNumber(ch) || ch == '.')
                {

                }
                else
                {
                    int indx = str.IndexOf(ch);
                    seqnum[0] =  str.Substring(0, indx).ToString();
                    seqnum[1] =  str.Substring(indx,str.Length-indx).ToString();
                    break;
                }
            }


         // output
        //  seqnum[0]=123.1.1.
        //  seqnum[1]=QWE

Upvotes: 0

viclim
viclim

Reputation: 959

string str = "123.1.1.QWE";    
int index = str.LastIndexOf(".");
string[] seqNum = new string[] {str.Substring(0, index), str.Substring(index + 1)};

Upvotes: 12

Related Questions