Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Problem with String.Split()

Problem with String.Split()

Scheduled Pinned Locked Moved C#
csharpdata-structureshelpquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 2 Offline
    2 Offline
    2TammyB
    wrote on last edited by
    #1

    Hi i am new to OOP and c# , Im building a program that reads settings form a .cfg file . I have managed to import the entrire cfg file into an array using string .split

    onst string fName = @"Settings.cfg";
    string[] lines = File.ReadAllLines(fName);
    List fields = new List();
    foreach (string s in lines)
    {
    if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
    {
    continue;
    }
    fields.AddRange(s.Split(new char[] { '=' }));
    }
    Settings = fields.ToArray();

    However this also adds the setting name to the array which is not needed , is there any way to modify the code so only values to the right of the = get added to the array ? Thanks

    F L H A 4 Replies Last reply
    0
    • 2 2TammyB

      Hi i am new to OOP and c# , Im building a program that reads settings form a .cfg file . I have managed to import the entrire cfg file into an array using string .split

      onst string fName = @"Settings.cfg";
      string[] lines = File.ReadAllLines(fName);
      List fields = new List();
      foreach (string s in lines)
      {
      if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
      {
      continue;
      }
      fields.AddRange(s.Split(new char[] { '=' }));
      }
      Settings = fields.ToArray();

      However this also adds the setting name to the array which is not needed , is there any way to modify the code so only values to the right of the = get added to the array ? Thanks

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      You might want to use Regular Expressions[^]

      1 Reply Last reply
      0
      • 2 2TammyB

        Hi i am new to OOP and c# , Im building a program that reads settings form a .cfg file . I have managed to import the entrire cfg file into an array using string .split

        onst string fName = @"Settings.cfg";
        string[] lines = File.ReadAllLines(fName);
        List fields = new List();
        foreach (string s in lines)
        {
        if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
        {
        continue;
        }
        fields.AddRange(s.Split(new char[] { '=' }));
        }
        Settings = fields.ToArray();

        However this also adds the setting name to the array which is not needed , is there any way to modify the code so only values to the right of the = get added to the array ? Thanks

        F Offline
        F Offline
        Fadi Yoosuf
        wrote on last edited by
        #3

        you can use Substring method instead of Split method. find the index of '=' character using IndexOf method and then pass this index + 1 as the parameter of Substring method.

        1 Reply Last reply
        0
        • 2 2TammyB

          Hi i am new to OOP and c# , Im building a program that reads settings form a .cfg file . I have managed to import the entrire cfg file into an array using string .split

          onst string fName = @"Settings.cfg";
          string[] lines = File.ReadAllLines(fName);
          List fields = new List();
          foreach (string s in lines)
          {
          if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
          {
          continue;
          }
          fields.AddRange(s.Split(new char[] { '=' }));
          }
          Settings = fields.ToArray();

          However this also adds the setting name to the array which is not needed , is there any way to modify the code so only values to the right of the = get added to the array ? Thanks

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Two things occur to me. The first, to allow you to carry on using the same design that you have:

                      if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
                      {
                          continue;
                      }
          
                      fields.Add(s.Split(new char\[\] { '=' }\[1\])); <===== you might have to add a ToString(), or cast to a string
          

          The second would be to use a Dictionary, rather than a List for fields. This would allow you to keep both parts; the setting name as the key and the setting value as the er.... value.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          P 1 Reply Last reply
          0
          • 2 2TammyB

            Hi i am new to OOP and c# , Im building a program that reads settings form a .cfg file . I have managed to import the entrire cfg file into an array using string .split

            onst string fName = @"Settings.cfg";
            string[] lines = File.ReadAllLines(fName);
            List fields = new List();
            foreach (string s in lines)
            {
            if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
            {
            continue;
            }
            fields.AddRange(s.Split(new char[] { '=' }));
            }
            Settings = fields.ToArray();

            However this also adds the setting name to the array which is not needed , is there any way to modify the code so only values to the right of the = get added to the array ? Thanks

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            If your cfg file is set out like:

            Name=SK
            FavWord=Delicious

            So you only have one item per line, you should just be able to add the second element of the array:

            foreach (string s in lines)
            {
            if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
            {
            continue;
            }
            string[] parts = s.Split(new char[] { '=' });
            fields.Add(parts[1]);
            }

            Also, it might be worth your while to check out the Dictionary class, which may work out better for you. With a dictionary you can add an item like this:

            myDict.Add("Name", "SK");
            //And then get the value of the setting 'Name'...
            string m_name = myDict["Name"];

            Handy stuff. EDIT: Wow I need to post quicker. When I first hit 'Reply' this question had no answers!

            My current favourite word is: Delicious!

            -SK Genius

            Game Programming articles start -here[^]-

            H 1 Reply Last reply
            0
            • H Henry Minute

              Two things occur to me. The first, to allow you to carry on using the same design that you have:

                          if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
                          {
                              continue;
                          }
              
                          fields.Add(s.Split(new char\[\] { '=' }\[1\])); <===== you might have to add a ToString(), or cast to a string
              

              The second would be to use a Dictionary, rather than a List for fields. This would allow you to keep both parts; the setting name as the key and the setting value as the er.... value.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              If the value contains an equal sign, then also use the overload of Split that limits the number of strings returned.

              1 Reply Last reply
              0
              • A Anthony Mushrow

                If your cfg file is set out like:

                Name=SK
                FavWord=Delicious

                So you only have one item per line, you should just be able to add the second element of the array:

                foreach (string s in lines)
                {
                if (s == "" || s.StartsWith(" ") || s.StartsWith("/"))
                {
                continue;
                }
                string[] parts = s.Split(new char[] { '=' });
                fields.Add(parts[1]);
                }

                Also, it might be worth your while to check out the Dictionary class, which may work out better for you. With a dictionary you can add an item like this:

                myDict.Add("Name", "SK");
                //And then get the value of the setting 'Name'...
                string m_name = myDict["Name"];

                Handy stuff. EDIT: Wow I need to post quicker. When I first hit 'Reply' this question had no answers!

                My current favourite word is: Delicious!

                -SK Genius

                Game Programming articles start -here[^]-

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                SK Genius wrote:

                EDIT: Wow I need to post quicker. When I first hit 'Reply' this question had no answers!

                Ditto.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups