11 Replies - 1127 Views - Last Post: 29 October 2013 - 07:02 AM Rate Topic: ***** 1 Votes

#1 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 314
  • Joined: 22-October 13

String Splitting Question

Posted 27 October 2013 - 02:45 PM

I have this string:
string myString = "This is a string.  \"This is what I want\", and this is filler"


I want to split the string into a new variable, myNewString, by grabbing everything in between This is a string. \" and ,and this is filler.

The output should be:

This is what I want.

No quotes should be in the output.

Is there an easy way to define the starting phrase and ending phrase and output the middle phrase to the new variable?

Is This A Good Question/Topic? 0
  • +

Replies To: String Splitting Question

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,247
  • Joined: 12-December 12

Re: String Splitting Question

Posted 27 October 2013 - 02:51 PM

    string myNewString = myString.Split('\"')[1];
    MessageBox.Show(myNewString);


Split Example :dotnetperls
Was This Post Helpful? 1
  • +
  • -

#3 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 314
  • Joined: 22-October 13

Re: String Splitting Question

Posted 27 October 2013 - 03:13 PM

View Postandrewsw, on 27 October 2013 - 02:51 PM, said:

    string myNewString = myString.Split('\"')[1];
    MessageBox.Show(myNewString);


Split Example :dotnetperls


I think I need to provide a bit more info on the string. The string is not static, so I have to use a phrase to define the start and end points. For example, the first part of the string can change, but certain phrases remain the same. Thats why I want to use phrases instead of a basic split method.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,247
  • Joined: 12-December 12

Re: String Splitting Question

Posted 27 October 2013 - 03:26 PM

You can use String.IndexOf to find where one string begins in another, then String.Substring to return part of the string.
Was This Post Helpful? 2
  • +
  • -

#5 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 314
  • Joined: 22-October 13

Re: String Splitting Question

Posted 27 October 2013 - 03:59 PM

Working code is:

string myString = "This is a string.  \"This is what I want\", and this is filler"

int first = myString.LastIndexOf("This is");
int last = myString.IndexOf("I want");
string newString = myString.SubString(first, last-first);

Was This Post Helpful? 1
  • +
  • -

#6 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: String Splitting Question

Posted 27 October 2013 - 06:13 PM

for more flexibility, you could do:

string myString = "This is a string.  \"This is what I want\", and this is filler";
int first = myString.IndexOf("\"");
int last = myString.LastIndexOf("\"");
string newString = myString.Substring(first + 1, (last - first) - 1);



this returns the correct string without the " " marks around it. This way you can pull out any substring surrounded by "", without your code being specific for a single string instance. for example "Blah blah \"I want this\" Filler 2" would return 'I want this', while also working with the 1st string.

If you want the substring to contain the " " around it, just take out the +1, and replace the -1 with +1 in the .Substring call.

If you have multiple ' " " ' blocks within a single string, that could produce problems with the current method, and would require some re-working.

This post has been edited by Rhino1111: 27 October 2013 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,247
  • Joined: 12-December 12

Re: String Splitting Question

Posted 27 October 2013 - 06:19 PM

@Rhino1111 The OP isn't looking for the quote-marks specifically, which is why he/she wasn't able to use the Split() version I posted. But to remove any spaces I would just trim the text after extracting it.

This post has been edited by andrewsw: 27 October 2013 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#8 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: String Splitting Question

Posted 27 October 2013 - 06:21 PM

Ah my bad, this late at night I typically tend to skim over posts and miss things :surrender: .
Was This Post Helpful? 0
  • +
  • -

#9 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: String Splitting Question

Posted 27 October 2013 - 10:29 PM

I have to say it: Regex! Given more information about what parts on constant and what isn't, Regex can solve your problem.
Was This Post Helpful? 1
  • +
  • -

#10 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 314
  • Joined: 22-October 13

Re: String Splitting Question

Posted 27 October 2013 - 11:09 PM

View PostMomerath, on 27 October 2013 - 10:29 PM, said:

I have to say it: Regex! Given more information about what parts on constant and what isn't, Regex can solve your problem.


I'll have to look into regex! Never used it before, so I don't know much about it I'm afraid :P
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7095
  • View blog
  • Posts: 24,112
  • Joined: 05-May 12

Re: String Splitting Question

Posted 28 October 2013 - 05:49 AM

My first instinct was to agree with Momerath about using a regex, but then I just realized that you are creating a bigger problem if your leading and trailing phrases can be anything the user inputs.

Let me explain. Typically, the way you would approach this problem by building a regular expression that will look like the pseudo-regex: ^leadingPhrase(?<target>.*)trailingPhrase$. But what happens when the leading an trailing phrases contain characters that are used for regular expressions? Now you'll have to pre-process the leading and trailing expressions to make sure that all characters are escaped before you build the regular expression to use to try to parse out the target phrase.

Now depending on your usage pattern, you'll have to write better optimizations. If you do a lot of matching, but the leading and trailing phrases stay stable, then you'll need to make the escaping code efficient so that you don't escape characters that you don't need to. If on the other hand, you do a lot of matching, but the phrases change each time, then the escaping code can be naive and escape everything, but you'll have to manage disk space and memory well. What does disk space have to do with this? It's because each new regular expression actually causing the .NET regular expression engine to compile a whole new temporary assembly for that particular regular expression.

In the long run, I think that andrewsw's solution of simply using the index of phrases to determine where the middle phrase is the simplest and incurs the least number of other dependencies.
Was This Post Helpful? 1
  • +
  • -

#12 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 314
  • Joined: 22-October 13

Re: String Splitting Question

Posted 29 October 2013 - 07:02 AM

View PostSkydiver, on 28 October 2013 - 05:49 AM, said:

My first instinct was to agree with Momerath about using a regex, but then I just realized that you are creating a bigger problem if your leading and trailing phrases can be anything the user inputs.

...


Hmmmm while it wouldn't work for this issue, it defiantly would for others without changing strings :)

This post has been edited by andrewsw: 29 October 2013 - 07:05 AM
Reason for edit:: Reduced previous quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1