extract information from a string with a lot of spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rootz88
    New Member
    • Feb 2010
    • 3

    extract information from a string with a lot of spaces

    ok I have this string and other similar( ull find them in the attachment)

    "BRUNEI BANDAR SERI BEGAWAN 5770 343653"

    I need to extract BRUNEI, BANDAR SERI BEGAWAN, 5770 and 343653
    but i cant find a way to do it that work for all my string that i have..
    Can someone help me on that plz..
    thx
    Attached Files
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    This is very obviously a work for hire project that you are asking others to code for you.

    Do you really think it is right? You took on a paying contract to parse through a file of names and addresses - and now you want the volunteers here to tell you how to do it, for free.

    "I can't find a way", implies that you have at least TRIED to find a way. Please tell us what you have tried. Please show us the code you have tried so far and what errors it is giving you.

    Comment

    • Rootz88
      New Member
      • Feb 2010
      • 3

      #3
      Yeah.. sorry, it's the first time that i'm writing on a page like that...
      ok that's what i tried, it worked for quite all of them and i had a problem with only one..
      it's one of my contrustor...
      so while im searching in my string i'm looking first, if it's not a space then i'm putting in the good variable, then after, if the caracter it's a space, i'm gonna look if the caractor next to it (i+1) it,s a space to, then after if the caractor at i+2 is different then a space, so it's a different word... then i'll change variable with indice...
      then if it's a but the space is include in the word like "BUENOS AIRES" ill right it down in my variable... but the problem then, it's for BRUNEI between BANDAR SERI BEGAWAN and 5770 there's only one space so BANDAR SERI BEGAWAN 5770 are only one variable... so there's where i'm stuck... i cant find a way to resolve the problem.

      Code:
      public pays(string extrInfo)
              {
                  int indice = 1;
                  continent = extrInfo[0];
                  for (int i = 1; i < extrInfo.Length; i++)
                  {
                       if (!(extrInfo[i] == ' '))
                      {
                          switch (indice)
                          {
                              case 1:
                                  paysnom = paysnom + extrInfo[i];
                                  break;
                              case 2:
                                  capital = capital + extrInfo[i];
                                  break;
                              case 3:
                                  supercifie = supercifie + extrInfo[i];
                                  break;
                              case 4:
                                  population = population + extrInfo[i];
                                  break;
                          }
                      if (extrInfo[i] == ' ' && (extrInfo[i + 1] == ' ') && !(extrInfo[i + 2] == ' '))
                      {
                          indice++;
                      }
                      else if (extrInfo[i] == ' ' && !(extrInfo[i + 1] == ' ') && !(extrInfo[i - 1] == ' '))
                      {
                          switch (indice)
                          {
                              case 1:
                                  paysnom = paysnom + extrInfo[i];
                                  break;
                              case 2:
                                  capital = capital + extrInfo[i];
                                  break;
                              case 3:
                                  supercifie = supercifie + extrInfo[i];
                                  break;
                              case 4:
                                  population = population + extrInfo[i];
                                  break;
                          }
                      }
      
                      }
                  }
              }
      Last edited by tlhintoq; Feb 22 '10, 04:53 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Read up on MSDN regarding the string.split method.
          You give it a single string, such as the line you are trying to parse, along with the character you want to use for splitting (a space in your example) and it will return a string array of all the little sub strings.

          Code:
          string Source = "BRUNEI BANDAR SERI BEGAWAN 5770 343653";
          string[] SplitUp = string.split(Source, ' ');
          string paysnom = SplitUp[0];
          string capital = SplitUp[1]
          // and so on

          Comment

          • Rootz88
            New Member
            • Feb 2010
            • 3

            #6
            The answer

            Thx!
            I found an other way easier than everything that i saw
            each variable have a limited number of space to use, it cant pass over it
            so with the length of my string i did a substring then a trimend! i got all what i needed!

            Comment

            Working...