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
Solved

how to split string text in asp.net

Posted on 2006-11-02
12
433 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 33
ASP.NET MVC identity 6 30
List<PaisEntity> - show some 1 28
Finding the IIS version 5 21
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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