Solved

how to split string text in asp.net

Posted on 2006-11-02
12
429 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
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
 
LVL 17

Assisted Solution

by:lem2802
lem2802 earned 31 total points
ID: 17860170
Dim s as string() = ("1,2,3").ToString.Split(",")
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now