c# substring carriage return
- hi I have a string variable "MyAddress" which contains 5 lines of text:Address1: abcAddress2: defSuburb: ghiState: jklCountry: mnoI also have 5 string variables called: strAddress1, strAddress2, strSuburb, strState, and strCountry.Is it possible to use a method such as substring to retrieve the related values from MyAddress and put the 5 string variables? e.g. get "abc" for strAddress1.thanks in advance!
Achievement provides the ultimate pleasure in life
Answers
NameValueCollection lines = new NameValueCollection(); foreach(string line in MyAddress.Split(Environment.NewLine)) { string[] parts = line.Split(':'); lines.Add(parts[0].Trim(), parts[1].Trim()); } strAddress1 = lines.Get("Address1"); strAddress2 = lines.Get("Address2"); strSuburb = lines.Get("Suburb"); strState = lines.Get("State"); strCountry = lines.Get("Country");
- Marked As Answer byssfftt Saturday, November 07, 2009 8:04 AM
- Proposed As Answer byDheeraj_Mallemala Friday, November 06, 2009 12:48 PM
All Replies
- Hi,
instead of substring the string value you can use the namevaluecollection.
have a look at this link.hope it helps.
http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you char[] splitString = { '\n' }; char[] splitPart = { ':' }; string[] parts = s.Split(splitString); for (int i = 0; i < parts.Length; i++) { string[] pieces = parts[i].Split[splitParts]; switch (i) { case 0: strAddress1 = pieces[1].Trim(); break; case 1: strAddress2 = pieces[1].Trim(); break; case 2: strSuburb = pieces[1].Trim(); break; case 3: strState = pieces[1].Trim(); break; case 4: strCountry = pieces[1].Trim(); break; } }
There is no error checking in this code, it assumes there are 5 parts to the original string and that each substring has 2 parts seperated by a :
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit- thanks Ron for your quick reply. I want to specifically look for the strings like "Address2" and "State:", is it possible to achieve?
thanks again!
Achievement provides the ultimate pleasure in life - Hi,
using substring we can get but its not a correct way of doing this.if you want you can try out my suggestion.
private void Form1_Load(object sender, EventArgs e) { NameValueCollection addColl = new NameValueCollection(); addColl=CreateAddress(); string country = addColl["Country"].ToString(); MessageBox.Show(country); } private NameValueCollection CreateAddress() { NameValueCollection address = new NameValueCollection(); address.Add("Addr1", "Abc"); address.Add("Addr1", "South"); address.Add("Addr1", "Texas"); address.Add("State", "chennai"); address.Add("Country", "India"); return address; }
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you NameValueCollection lines = new NameValueCollection(); foreach(string line in MyAddress.Split(Environment.NewLine)) { string[] parts = line.Split(':'); lines.Add(parts[0].Trim(), parts[1].Trim()); } strAddress1 = lines.Get("Address1"); strAddress2 = lines.Get("Address2"); strSuburb = lines.Get("Suburb"); strState = lines.Get("State"); strCountry = lines.Get("Country");
- Marked As Answer byssfftt Saturday, November 07, 2009 8:04 AM
- Proposed As Answer byDheeraj_Mallemala Friday, November 06, 2009 12:48 PM
- If I understand it better then you mean to say that you have a 5 lines of Address i.e. a '\r\n' character in between.See if this code works for you.This is just a round about work, Moreover this is not the right approach. Try to implement Dictionary and store values in Key Value Pair
string str = @"Address1: abc Address2: def Suburb: ghi State: jkl Country: mno"; string[] arr = str.Split('\n'); for (int i = 0; i < arr.Length ; i++) { switch (i) { case 0: strAddress1 = arr[i].Replace("Address1: ", ""); break; case 1: strAddress2 = arr[i].Replace("Address2: ", ""); break; case 2: strSuburb = arr[i].Replace("Suburb: ", ""); break; case 3: strState = arr[i].Replace("State: ", ""); break; case 4: strCountry = arr[i].Replace("Country: ", ""); break; default: break; } }ThanksRatz


