I basically want to check if part of a string begins with a certain sequence - in this case ftp://, or http://. How would I do this?
Thanks
I basically want to check if part of a string begins with a certain sequence - in this case ftp://, or http://. How would I do this?
Thanks
Use String.StartsWith
. With just two possible prefixes you can write it as follows:
if (s.StartsWith("http://") || s.StartsWith("ftp://")) { ... }
If you have a lot of different possible prefixes it might be better to use a loop or a LINQ expression instead. For example:
string[] prefixes = { "http://", "ftp://", /* etc... */ };
if (prefixes.Any(prefix => s.StartsWith(prefix)))
{
// ...
}
if(myString.StartsWith("ftp://") || myString.StartsWith("http://")) { }
if you wish it to ignore case then use StringComparison.OrdinalIgnoreCase.
if(myString.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) || myString.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { }
Personally I'd suggest use Regex, but the most basic form is
string myText = @"http://blabla.com";
if (myText.IndexOf("http://") == 0 || myText.IndexOf("ftp://") == 0))
{
//dosome
}
if( myString.StartsWith("ftp://")){
...
}
Similar if you want to check for http://, but change the parameter to StartsWith.