[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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
0
Stanton_Roux
Asked:
Stanton_Roux
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Praveen VenuCommented:
Dim strArr(2) as String
strArr=strMessage.Split(",")
var1=strArr(0)
var2=strArr(1)


This seems to be the simple answer
 

0
 
Praveen VenuCommented:
here is the c# code

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

0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rgn2121Commented:
okay so...much easier than what I posted...   :)
0
 
Praveen VenuCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now