Loop thorugh string in .net

Posted on 2012-04-06
Last Modified: 2012-04-06
I have
string like this

string mystring = This is the %EventName% and place is %ShelterName%

 I want to seperate the %EventName% ,%shelterName% as seperate string.

I am not sure how I can loop thorugh string and get the position of %.Please Help me out with this.
Question by:itinfo7
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 37817833
Can you please provide a sample of the string that you need to parse? Depending on the structure of the string, you will be able to parse the string and pull out the different variables that you want.

If you post a sample, I may be able to write the parse statement for you.

Author Comment

ID: 37817853
string parsestring  = VCA is the %Animal% Hospital and It's located %texas%

Author Comment

ID: 37817855
Actually string is changeable but it would follow same format I am giving you.

Expert Comment

ID: 37817869
This requires a little more code than I can do over my phone, but the following example should give you everything you need to get the variables you want.

Extract a value between two spaces in a string
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 37818034
You may consider using regular expressions for this. Be careful though:  regex is not a parser, so if you leave out an expected percent symbol, or if you other text actually contains a percent symbol, then you will end up with unexpected results. In that case, I might suggest creating some kind of "escape" character to denote a literal percent symbol vs. a percent that is part of a placeholder. Perhaps by doubling-up the percent (%%).

Here is an example of using regex. I have included checking for the doubled-up percent I mentioned.

using System;
using System.Text.RegularExpressions;

namespace _27665770
    class Program
        static void Main(string[] args)
            string mystring = "This is the %EventName% and place is %ShelterName%";

            foreach (Match m in Regex.Matches(mystring, "(?<!%)%[^%]+%"))


Open in new window

If you don't think you'll encounter any percents which aren't part of a placeholder, then you can remove the "(?<!%)" part, but it shouldn't hurt anything to leave it in.

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question