Finding a character in a string left and right of that character

Hi There

I have a string field that is comming through to my application
 string strMessage = context.Request.QueryString["text"];
the string will look like this "34254532 2433"
How do I create 2 variable from that string.
var 1 = everything to the left of the space
var 2=everything to the right of the space

The numbers will not be fixed length but it will always be seperated by a space.

Who is Participating?
Praveen VenuConnect With a Mentor Technical Project ManagerCommented:
here is the c# code

    string[] strArr = new string[2];
    strArr = strMessage.Split(",");
    var1 = strArr(0);
    var2 = strArr(1);

Praveen VenuTechnical Project ManagerCommented:
Dim strArr(2) as String

This seems to be the simple answer

I usually code in VB so I am not exactly sure of syntax, but fundamentally you would want to first check that the space exists, unless it will always exist.  strmessage.Contains(" ")

then you have to find the index...  strmessage.IndexOf(" ")

then get your strings from that using the index...strmessage.Substring(0,Index) or strmessage.Substring(index)

I think all that should work fine...might have to play with it a little.  That is the best way I know off the top of my head.  Good Luck...
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

okay so...much easier than what I posted...   :)
Praveen VenuTechnical Project ManagerCommented:
please mark the answer as solution if it was useful to you..
Tony McCreathTechnical SEO ConsultantCommented:
The accepted solution in C# does not compile! And if it did it would not do the right thing! Try this
string strMessage = "34254532 2433";
string[] strArr = null; // removed new array as this will only get discarded
strArr = strMessage.Split(' '); // changed to char and spilt on a space
string var1 = strArr[0]; // changed to array reference instead of methind call and added type decleration
string var2 = strArr.Length > 1 ? strArr[1] : String.Empty; // added validation test

Open in new window

Stanton_RouxAuthor Commented:
I changed it abit .Just needed to get the IDea
this is what i used
if (strMessage.Contains(" "))
                  string[] strArr = new string[2];
                  strArr = strMessage.Split(' ');
                  EmergencyID = strArr[0];
                  Password = strArr[1];
                  EmergencyID = strMessage;
                  Password = "";

Open in new window

Tony McCreathTechnical SEO ConsultantCommented:
Just one slight improvement. You don't need the "new string[2]". The split creates a brand new array for you. So just do this:

string[] strArr = strMessage.Split(' ');
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.