Ask a questionAsk a question
 

Answerc# substring carriage return

  • Wednesday, November 04, 2009 5:09 AMssfftt Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi I have a string variable "MyAddress" which contains 5 lines of text:
    Address1: abc
    Address2: def
    Suburb: ghi
    State: jkl
    Country: mno
    I 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

  • Wednesday, November 04, 2009 12:44 PMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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

  • Wednesday, November 04, 2009 5:13 AMGnanadurai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 5:22 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
  • Wednesday, November 04, 2009 7:13 AMssfftt Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 10:16 AMGnanadurai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
  • Wednesday, November 04, 2009 12:44 PMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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
    •  
  • Wednesday, November 04, 2009 6:14 PMRatZzz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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.

                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;
                    }
    
                    
                }
    
    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

    Thanks 
    Ratz