tags:

views:

170

answers:

4

Is there a way to get the string between.. lets say quote " The problem with using Indexof and substring is that it gets the first " and last " but not the pair. Like

"Hello" "WHY ARE" "WWWWWEEEEEE"

It will get

Hello" "WHY ARE" "WWWWWEEEEEE

I want it to get to array > Hello, WHY ARE, WWWWEEEEEE

Is there any way to do this?

+2  A: 
string s = '"Hello" "WHY ARE" "WWWWWEEEEEE"'
string[] words = s.Split('"');
// words is now ["Hello", " ", "WHY ARE", " ", "WWWWWEEEEEE"]

If you don't want the empty strings, you can split by '" "', in which case you will get ['"Hello', "WHY ARE", 'WWWWWEEEEEE"'].

On the other hand, using regular expressions could be the best solution for what you want. I'm not a C# expert, so I can't give the code from the top of my head, but this is the regex you'll want to use: "(.*?)"

Can Berk Güder
It won't work. It will have all things between the o" and "W also. I only want the HELLO, etc.
Jonathan Shepherd
you should use `([^"]+)` instead of `(.*?)` because it is more efficient, and will skip the empty ones, like `""`
Svish
([^"]+) is more efficient, that's true. But the OP didn't say he doesn't want to catch empty ones.
Can Berk Güder
+7  A: 

Something like this?

StringCollection resultList = new StringCollection();
try 
{
    Regex regexObj = new Regex("\"([^\"]+)\"");
    Match matchResult = regexObj.Match(subjectString);

    while (matchResult.Success) 
    {
        resultList.Add(matchResult.Groups[1].Value);
        matchResult = matchResult.NextMatch();
    } 
}
catch (ArgumentException ex) 
{
    // Syntax error in the regular expression
}

If subjectString was "Hello" "WHY ARE" "WWWWWEEEEEE", that should give you a list containing:

  • Hello
  • WHY ARE
  • WWWWWEEEEEE


A more compact example which uses the static Regex class instead, and just writes the matches to console instead of adding to a collection:

var subject = "\"Hello\" \"WHY ARE\" \"WWWWWEEEEEE\"";
var match = Regex.Match(subject, "\"([^\"]+)\"");

while (match.Success)
{
    Console.WriteLine(match.Groups[1].Value);
    match = match.NextMatch();
}
Svish
As far as I know, you can simplify it by removing the try-catch though, since when run once you already know that the regular expression don't contain any syntax errors.
Svish
If you want the spaces inbetween as well you can just remove the quotes and the group and take the whole match. Replace `"([^"]+)"` with `[^"]+`, and `match.Groups[1].Value` with `match.Value`.
Svish
A: 
string s = '"Hello" "WHY ARE" "WWWWWEEEEEE"
s.replace("\" \"", "!*!"); // custom seperator
s.Replace('"', string.empty);
string[] words = s.Split('!*!');

Should do the trick,

Kindness,

Dan

Daniel Elliott
this would give [Hello, WHY, ARE, WWWWWEEEEEE]
Can Berk Güder
good point, editting now :)
Daniel Elliott
A: 

You can also use the String.IndexOf(char value, int startIndex) method which has, as its parameter says, a start index from which the scan is started.

int start = 0;
do
{
  int i1 = s.IndexOf('=', start);
  if (i1 < 0) break;

  int i2 = s.IndexOf('=', i1 + 1);
  if (i2 < 0) break;

  yield return s.Substring(i1, i2 - i1);
  start = i2 + 1;
}
while (start < s.Length);
Thomas Danecker