Question

When retrieving a a list of labels from gmail, they come back as a single string as such:

\Inbox \Sent Important "Muy Importante" foo

I was wondering if there was an inbuilt .NET function to split this into its 'true' format - i.e.

["\Inbox", "\Sent Important", "Muy Importante", "foo" ]

Or is this one for regex?

Was it helpful?

Solution

If the pattern is regular then I would simply iterate through chars until expected delimiters are found.

1. Loop through chars until second "\" character.

At this point you know what your first word is made up. Store it in a variable.

2. From previous position, loop again until you meet a space character.

You now know what the second segment is. Store in a variable.

3. From previous position, loop until you meet a quotation mark.

Now you know what the third segment is. Again, store in a variable.

4. From previous position, loop until you meet a second quotation mark.

You now have the fourth segment. And the fifth is whatever remains. Store both in variables.

Of course at each step you'll want to do some trimming but this gives you the main idea. You'll be able to work with your segment variables to do whatever you want from there on. It will be much more efficient that regular expressions.

Do not forget: this is if the pattern is regular.

OTHER TIPS

Personally, I would use Regex...

C# version

System.Text.RegularExpressions.Regex.Split("\\Inbox \\Sent Important \"Muy Importante\" foo", " \"*")

VB.Net version

System.Text.RegularExpressions.Regex.Split("\Inbox \Sent Important ""Muy Importante"" foo", " ""*")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top