Solved

how to split string text in asp.net

Posted on 2006-11-02
12
437 Views
Last Modified: 2012-06-22
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
Comment
Question by:service2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 9

Accepted Solution

by:
srivatsavaye earned 32 total points
ID: 17860055
declare an arraylist and then assign it to mystring.spit(";")

and then repeat it with mystring.split("@")
0
 

Author Comment

by:service2001
ID: 17860155
can you post the sample and i know the concept but i need a working sample

thanks.
0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 17860168
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Assisted Solution

by:Elvio Lujan
Elvio Lujan earned 31 total points
ID: 17860170
Dim s as string() = ("1,2,3").ToString.Split(",")
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 31 total points
ID: 17868415
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
 

Author Comment

by:service2001
ID: 17874902
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
 

Author Comment

by:service2001
ID: 17878322
also i dont like the idea of hardcoding the string[0] or string[1] rather i would like to put in the loop

 
0
 
LVL 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 31 total points
ID: 17882011
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
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 18377504
I returned and gave further answers.  Recommend I at least get some points.

Ben.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question