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.

Thanks
Stanton
Stanton_RouxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Praveen VenuTechnical Project ManagerCommented:
Dim strArr(2) as String
strArr=strMessage.Split(",")
var1=strArr(0)
var2=strArr(1)


This seems to be the simple answer
 

0
Praveen VenuTechnical Project ManagerCommented:
here is the c# code

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgn2121Commented:
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...
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rgn2121Commented:
okay so...much easier than what I posted...   :)
0
Praveen VenuTechnical Project ManagerCommented:
please mark the answer as solution if it was useful to you..
0
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
 
Debug.WriteLine(var1);
Debug.WriteLine(var2);

Open in new window

0
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];
 
              }
              else
              {
                  EmergencyID = strMessage;
                  Password = "";
              }

Open in new window

0
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(' ');
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.