?
Solved

split string variable to 2 variables

Posted on 2009-12-31
18
Medium Priority
?
248 Views
Last Modified: 2012-05-08
hi there ,
i have a string calld custDet that hold customers detailes like :
my name is simon my address is XXX
i want to that "my name is simon" will be in a new string variable calld  CustName
and the my address is XXX will be in a new string variable Calld CustAddress
how can i split the CustDet variable
thanks....
0
Comment
Question by:Tech_Men
  • 7
  • 6
  • 5
18 Comments
 
LVL 8

Expert Comment

by:Jon500
ID: 26156238
Hello,

You need to know the lengths of the strings involved or you need to have a special character (a delimiter) in the string in order to know how to split it into the CustName and the CustAddress strings.

Do you have the length or do you have delimiter?

Regards,
Jon500
0
 

Author Comment

by:Tech_Men
ID: 26156283
hi there ,
thanks for your answer
jon500 after length 34 its need to be split
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:igni7e
ID: 26156287
The expression will be something like this:
/(.*?)(My Address.*)/

Match m = Regex.Match("My Name is agasdg My Address is asg", "/(.*?)(My Address.*)/", RegexOptions.IgnoreCase);

if (m.Success)
{
   string name = match.Groups[1].Value;
   string addr = match.Groups[2].Value;
}


http://dotnetperls.com/regex-match-use
0
 
LVL 4

Expert Comment

by:igni7e
ID: 26156299
If you use regex, the length of the names and addreses can vary.


string CustDet,
CustName,
CustAddress;

Match m = Regex.Match(CustDet, "/(.*?)(My Address.*)/", RegexOptions.IgnoreCase);

if (m.Success)
{
   CustName = match.Groups[1].Value;
   CustAddress = match.Groups[2].Value;
}
0
 
LVL 4

Expert Comment

by:igni7e
ID: 26156310
By the way, you need to import the following:
using System.Text.RegularExpressions;

0
 
LVL 8

Expert Comment

by:Jon500
ID: 26156323
Hi,

If it is always after 34 then use the code below.

CustName = custDet.Substring(1,34);
CustAddress = custDet.Substring(35);

This is NOT a good application of regular expressions (REGEX). No need.

Regards,
Jon500
0
 
LVL 4

Expert Comment

by:igni7e
ID: 26156334
i have a string calld custDet that hold customers detailes like

The author is saying it holds cumtomerS details, plural.
So I'm assuming you aren't going to only choose customers with names of a certain length, am I correct?

So, RegEx is the best way to go.
Jon500 probably suggested split for a DB field of customers names because it's an easy ugly way to do it something to hardcoded stuff.
0
 

Author Comment

by:Tech_Men
ID: 26156405
i try this :
its not working

    string ParitName = "my name is simon my address is XXX  my phone is YYY";
            Match m = Regex.Match(ParitName, "/(.*?)(simon.*)/", RegexOptions.IgnoreCase);


            if (m.Success)
            {
                string p1 = m.Groups[1].Value;
                string p2 = m.Groups[2].Value;
               
            }

Open in new window

0
 
LVL 8

Expert Comment

by:Jon500
ID: 26156420
@igni7e: The author said to split after length 34. To use Substring, in the case, is not "ugly way". In fact, it is totally unacceptable to apply Regex in this case.

Author: You're giving one requirement to me and following the advice of someone else who is not giving you a solution to breaking a string at a particular character position. What exactly do you want here?
0
 

Author Comment

by:Tech_Men
ID: 26156423
the best way to do it
0
 

Author Comment

by:Tech_Men
ID: 26156424
if i can split the string by name and not by lenth so its a bether wey for me
sorry ...
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26156433
Well, I have an idea that still is FAR BETTER than Regex:

CustName = custDet.Substring(1, "name goes here".Length);
CustAddress = custDet.Substring("name goes here".Length + 1);

If you want a cleaner solution:
string NameVal = "name value";
CustName = custDet.Substring(1, NameVal.Length);
CustAddress = custDet.Substring(NameVal.Length + 1);

What do you think?
Jon500
0
 

Author Comment

by:Tech_Men
ID: 26156450
not working
i try this code :

i need in p1 this string : my name is simon
in p2 : my address is XXX my phone is YYY

thanks ....
  string ParitName = "my name is simon my address is XXX  my phone is YYY";
            string p1 = ParitName.Substring(1,"simon".Length);
            string p2 = ParitName.Substring("simon".Length + 1);

Open in new window

0
 

Author Comment

by:Tech_Men
ID: 26156453
i want it by name not Length  pichos the string might change from time to time
0
 
LVL 8

Accepted Solution

by:
Jon500 earned 2000 total points
ID: 26156463
You did not follow my example.

In Substring(1, "simon".Length) you need to put the full name:
Substring(1, "my name is simon ".Length)

Notice that you need to include the space too.  Substring is going to match the characters you supply.

Try doing my cleaner way and this way you don't have to type the name twice:
string ParitName = "my name is simon my address is XXX  my phone is YYY";
string NameToLookFor = "my name is simon ";
            string p1 = ParitName.Substring(1,NameToLookFor.Length);
            string p2 = ParitName.Substring(NameToLookFor.Length + 1);

Let me know.
0
 

Author Closing Comment

by:Tech_Men
ID: 31671689
thanks
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26156476
Sure. Thank you for the points.

As a general observation, many inexperience programmers do not know when to apply certain tools--or they have missed the basics. I often recommend such books as VB.Net For Dummies (http://www.amazon.com/VisualBasic-NET-Dummies-Computer-Tech/dp/0764508679) to give such programmers a "leg up" on the language.

Substring is a great function for quickly parsing strings. Regex, on the other hand, is a powerful tool designed to match patterns--often very complex ones. Mastering the use of both is a sign of a good programmer.

I do wonder how your Name and Address values got merged somehow into a single string and how you will know the length of the Name string. It seems a bit odd to me but, then again, you gave the requirement as such as that is what I worked with.

Glad to help. Happy New Year, and thanks again for the points.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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