Search for Quote inside a string

  • Thread starter Thread starter temp
  • Start date Start date
T

temp

Hi,

completeString = "<p><a href="http://www.google.com"> ..... "

Now I want to extract the link pointed out by href so I try to find the
position of first the href tag and then try to see where the quotes begin.

pos_tag = completeString.IndexOf("href", 0, completeString.Length);

pos_text_tag_start = completeString.IndexOf("\"", pos_tag,
completeString.Length);

but this last line gives me an error - how can I find the position of a
quote - " in a string?

I have also tried :

pos_text_tag_start = completeString.IndexOf(""", pos_tag,
completeString.Length);



Any help is appreciated.
 
I think all of these will work:

"""" (four double quotes)
'"' (single quote, double quote, single quote)
Convert.ToChar(32)
(char)32
 
Rob Windsor said:
I think all of these will work:

"""" (four double quotes)
'"' (single quote, double quote, single quote)

These two won't. You need:
@""""
"\""
or '\"'.
 
Jon Skeet said:
These two won't. You need:
@""""
"\""

Doing "\"" doesnt work - see output from immediate window while debugging:


ompleteString.IndexOf("\"", pos_tag, completeString.Length)

'completeString.IndexOf("\"", pos_tag, completeString.Length)' threw an
exception of type 'System.ArgumentOutOfRangeException'

base {System.ArgumentException}: {"Count must be positive and count must
refer to a location within the string/array/collection.\r\nParameter name:
count"}

ActualValue: null

Message: "Count must be positive and count must refer to a location within
the string/array/collection.\r\nParameter name: count"
 
temp said:
Doing "\"" doesnt work - see output from immediate window while debugging:


ompleteString.IndexOf("\"", pos_tag, completeString.Length)

'completeString.IndexOf("\"", pos_tag, completeString.Length)' threw an
exception of type 'System.ArgumentOutOfRangeException'

base {System.ArgumentException}: {"Count must be positive and count must
refer to a location within the string/array/collection.\r\nParameter name:
count"}

ActualValue: null

Message: "Count must be positive and count must refer to a location within
the string/array/collection.\r\nParameter name: count"

The problem is with the count, not with the character you're looking for.
You're trying to look from pos_tag to pos_tag + Length -1, which is beyond
the end of the string. Since you want to search to the end of the entire,
just use the two-argument form:

:pos_tag = 'completeString.IndexOf("\"", pos_tag);
 
I believe regular expression would make things easier in your case.

Try something like this:
(?<Protocol>http):\/\/(?<Domain>[\w.]+\/?)\S*(?x)

And then you can parse the input string with
System.Text.RegularExpressions.Regex, which will probably save you tens of
lines of code.

A great tools called Expresso from Ultrapico can help you build a regular
expression visually. Worth trying in case you are not familiar with regular
expression.

Good luck!
 
Hi,
ompleteString.IndexOf("\"", pos_tag, completeString.Length)

'completeString.IndexOf("\"", pos_tag, completeString.Length)' threw an
exception of type 'System.ArgumentOutOfRangeException'

base {System.ArgumentException}: {"Count must be positive and count must
refer to a location within the string/array/collection.\r\nParameter name:
count"}

Well, the error is pretty self explanatory, pos_tag is probably > 0
therefore pos_tag + string.Length > string.Length , therefore you get the
error, it's CLEARLY stated in MSDN !

Just do IndexOf( "\"" , pos_tag );

Now , I STRONGLY suggest you to take the advice of Ray and use a regular
expression instead, you can find the url enclosed in " or in ' or even
nothing.
To further complicate the things, you could have things like : <a href="#"
onclick="window.open('url');return false">

Have fun! :)
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,
Hi


Now , I STRONGLY suggest you to take the advice of Ray and use a regular
expression instead, you can find the url enclosed in " or in ' or even
nothing.
To further complicate the things, you could have things like : <a
href="#" onclick="window.open('url');return false">


Did I miss something? I cannot see the post of Ray where he has suggested
regular expressions?
 
Found Ray's suggestion (strange that I still dont see it in my news client,
but I see his post on a different site).
 
Back
Top