Solved

Loop thorugh string in .net

Posted on 2012-04-06
5
405 Views
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.
0
Comment
Question by:itinfo7
[X]
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
5 Comments
 
LVL 2

Expert Comment

by:jackDOTisenberg
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.
0
 

Author Comment

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

Author Comment

by:itinfo7
ID: 37817855
Actually string is changeable but it would follow same format I am giving you.
0
 
LVL 2

Expert Comment

by:jackDOTisenberg
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
0
 
LVL 75

Accepted Solution

by:
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, "(?<!%)%[^%]+%"))
            {
                Console.WriteLine(m.Value);
            }

            Console.ReadKey();
        }
    }
}

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.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multi-Column Repeater 3 39
Data Analysis 7 59
Convert a string to date 4 40
Click print button programmatically on print showdialog 1 24
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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