Tech_Men
asked on
split string variable to 2 variables
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....
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....
Use Regular Expressions:
http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx
http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx
ASKER
hi there ,
thanks for your answer
jon500 after length 34 its need to be split
thanks for your answer
jon500 after length 34 its need to be split
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
/(.*?)(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
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;
}
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;
}
By the way, you need to import the following:
using System.Text.RegularExpress ions;
using System.Text.RegularExpress
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
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
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.
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.
ASKER
i try this :
its not working
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;
}
@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?
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?
ASKER
the best way to do it
ASKER
if i can split the string by name and not by lenth so its a bether wey for me
sorry ...
sorry ...
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
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.
What do you think?
Jon500
ASKER
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 ....
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);
ASKER
i want it by name not Length pichos the string might change from time to time
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
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.
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.
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