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
  1. Home
  2. General Programming
  3. C#
  4. Parse out all the "{" + string + "}" occurences?

Parse out all the "{" + string + "}" occurences?

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • _ Offline
    _ Offline
    _iobuf
    wrote on last edited by
    #1

    Is there a way to iterate through a string of text and find each occurence of a string that contains a substring of "{" + (variable string) + "}" and replace it with a corresponding string defined in a dictionary (of which the key value is the "{" + (variable string) + "}" ??

    L M G 3 Replies Last reply
    0
    • _ _iobuf

      Is there a way to iterate through a string of text and find each occurence of a string that contains a substring of "{" + (variable string) + "}" and replace it with a corresponding string defined in a dictionary (of which the key value is the "{" + (variable string) + "}" ??

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      string OldStr = "Application";
      string NewStr = "Program";

              string str = @"Run the Application , Close the Application and  Delete the Application";
              str = str.Replace(OldStr, NewStr);
      

      I know nothing , I know nothing ...

      _ 1 Reply Last reply
      0
      • _ _iobuf

        Is there a way to iterate through a string of text and find each occurence of a string that contains a substring of "{" + (variable string) + "}" and replace it with a corresponding string defined in a dictionary (of which the key value is the "{" + (variable string) + "}" ??

        M Offline
        M Offline
        MidwestLimey
        wrote on last edited by
        #3

        Yes. I suppose you'll want a hint as to how! :) If the string is formatted such that { and } only occur as a wrapper to a variable name then the easiest way I can think of is to run a regex on it, match all "\{[^\}]\}" and iterate through them. If not you'll probably have to write a lexer of some kind.

        10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

        _ 1 Reply Last reply
        0
        • L Lost User

          string OldStr = "Application";
          string NewStr = "Program";

                  string str = @"Run the Application , Close the Application and  Delete the Application";
                  str = str.Replace(OldStr, NewStr);
          

          I know nothing , I know nothing ...

          _ Offline
          _ Offline
          _iobuf
          wrote on last edited by
          #4

          Thanks.. I used that in a loop and put all the key values as the string to be replaced

          1 Reply Last reply
          0
          • M MidwestLimey

            Yes. I suppose you'll want a hint as to how! :) If the string is formatted such that { and } only occur as a wrapper to a variable name then the easiest way I can think of is to run a regex on it, match all "\{[^\}]\}" and iterate through them. If not you'll probably have to write a lexer of some kind.

            10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

            _ Offline
            _ Offline
            _iobuf
            wrote on last edited by
            #5

            This is a good solution too, thx

            1 Reply Last reply
            0
            • _ _iobuf

              Is there a way to iterate through a string of text and find each occurence of a string that contains a substring of "{" + (variable string) + "}" and replace it with a corresponding string defined in a dictionary (of which the key value is the "{" + (variable string) + "}" ??

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Given that you have the text in the text variable and the replacements in the replacements dictionary:

              text = RegEx.Replace(text, @"\{(.+?)\}", m => replacements[m.Value]);

              Despite everything, the person most likely to be fooling you next is yourself.

              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