Learn how to a build a cloud-first strategyRegister Now

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

how to split string text in asp.net

i have a string something like this:

"dp@Duplicate Passport;sp@Stolen Passport;"

how do i split to like this:

dp = Duplciate Passport
sp = Stoled Passport

i'm using asp.net 2
c#

thanks.
0
service2001
Asked:
service2001
4 Solutions
 
Raju SrivatsavayeSoftware EngineerCommented:
declare an arraylist and then assign it to mystring.spit(";")

and then repeat it with mystring.split("@")
0
 
service2001Author Commented:
can you post the sample and i know the concept but i need a working sample

thanks.
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
string strToSplit = "dp@Duplicate Passport;sp@Stolen Passport;";

string[] items = strToSplit.Split(";");
foreach(string item in items)
{
    string[] ask = item.Split("@");
    // ask[0] will = "dp"
    // ask[1] will = "Duplicate Passport"
   // and it repeats.  What you do with it, would be up to you or how you want to store it.
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Elvio Lujan.Net Senior DeveloperCommented:
Dim s as string() = ("1,2,3").ToString.Split(",")
0
 
strickddCommented:
string[] someString = "dp@Duplicate Passport;sp@Stolen Passport;".Remove(";").Split("@".ToCharArray());

string dp = someString[1];
string sp = someString[3];

You can't dynamically name a variable so you have to know the variable names first. You can also insert a loop and an if statement if the string may have the variables in a different order.
0
 
service2001Author Commented:
guys sorry for late response
but here is what im doing and it throwing me error
 
Cannot convert type 'string' to 'string[]'

stringbuilder sb = new stringbuilder();
 foreach (string key in Request.QueryString.Keys) //get the url string from querystring
        {
            //items data looks like this: "SP;ADP;FDD;DFD";
            string items = Request[key];

            string[] itemsarray = (string[])items;
            foreach (string item in items)
            {
                string[] x = item.Split(';');
                if (string[0] == "SP")
                  {
                     sb.append("sp = stolen passport");
                  }
               if (string [1] == "ADP")
                  {
                    sb.append("adp = active directory project");
                  }
            }
           
        }
any help ?

thanks
0
 
service2001Author Commented:
also i dont like the idea of hardcoding the string[0] or string[1] rather i would like to put in the loop

 
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
You should know the name of the key coming in, or you could be looking for trouble with all those hackers putting things in the query strings and hitting websites.

on the URL you would see somekey=SP;ADP;FDD;DFD so you would use Request.QueryString["somekey"]
You cannot take a string and convert it to an array without Split or another method that splits them into items.  Look at the code below and see if that is what you are looking to do.

stringbuilder sb = new stringbuilder();
string key = "somekey";

//items data looks like this: "SP;ADP;FDD;DFD";
string items = Request.QueryString[key];

string[] itemsarray = items.Split(';');

foreach (string item in items)
{
      switch(item)
      {
           case "SP":
                sb.Append("sp=stolen passport");
                break;
           case "ADP":
                sb.Append("adp = active directory project");
                break;
      }
}
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
I returned and gave further answers.  Recommend I at least get some points.

Ben.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now