Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to split string text in asp.net

Posted on 2006-11-02
12
Medium Priority
?
441 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 128 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 25

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
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.

 
LVL 18

Assisted Solution

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

Assisted Solution

by:strickdd
strickdd earned 124 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 25

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 124 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 25

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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