string between two delimiters....

  • Thread starter Thread starter kw_uh97
  • Start date Start date
K

kw_uh97

I would like to extract a string that has a single qoutes at the beginning
and end like the 'mystring' how would I go about getting the mystring to
appear?

Thanks in Advance for any help
 
I would like to extract a string that has a single qoutes at the beginning
and end like the 'mystring' how would I go about getting the mystring to
appear?

This is a really simple thing to do, apparently you are a real newbie
to programming.

If all you need to do is strip off the leading and trailing single
quote characters, and assuming there will not be any embedded single
quote characters anywhere inside of the leading and trailing single
quotes that would need to remain, all you would do is:

string myString = "'mystring'";

myString = myString.Replace("'", "");
 
kw_uh97 said:
I would like to extract a string that has a single qoutes at the beginning
and end like the 'mystring' how would I go about getting the mystring to
appear?

Thanks in Advance for any help

To extract mystring from the 'mystring' in the lines above you typically use
RegEx expressions.

using System.Text.RegularExpression;
....
Regex expression = new Regex("'[^']*'");
Match match = expression.Match("some string with 'apples' and 'bananas'");
string result = match.Value.Trim(new char[] { '\'' });

The above code match will contain 'apples', which you can strip the ' from
in numerous ways. Calling match.NextMatch() will give you 'orange' etc.

You can also do it without RegEx

string s = "some string with 'apples' and 'bananas'";
int start = s.IndexOf('\'') + 1;
int end = s.IndexOf('\'', start);
string result = s.Substring(start, end - start);

But this method may cause exceptions if you are missing a '
 
Sorry about that. I am indeed a newbie to programming and I started the
preface my question with an apology letting everyone know that my brain is
not functioning properly due to that I am under the weather. The effects of
the benadryle and cough medicine have me drowsy and sluggish. Sorry for my
laziness. :>)
 
This was exactly what I was looking for because my string was containing text
before my delimiters and all I needed was what was between my delimiters.

Many Thanks.

Morten Wennevik said:
kw_uh97 said:
I would like to extract a string that has a single qoutes at the beginning
and end like the 'mystring' how would I go about getting the mystring to
appear?

Thanks in Advance for any help

To extract mystring from the 'mystring' in the lines above you typically use
RegEx expressions.

using System.Text.RegularExpression;
...
Regex expression = new Regex("'[^']*'");
Match match = expression.Match("some string with 'apples' and 'bananas'");
string result = match.Value.Trim(new char[] { '\'' });

The above code match will contain 'apples', which you can strip the ' from
in numerous ways. Calling match.NextMatch() will give you 'orange' etc.

You can also do it without RegEx

string s = "some string with 'apples' and 'bananas'";
int start = s.IndexOf('\'') + 1;
int end = s.IndexOf('\'', start);
string result = s.Substring(start, end - start);

But this method may cause exceptions if you are missing a '
 
Back
Top