0
string myString = www.stackoverflow.com  
  1. i need to check if myString ends with . and any 3 other characters after it, i would prefer to do it with the EndsWith() method but if that's not possible any other way will work.

  2. How can i check if myString contains a . that has anything but www behind it (and i'd like to know how to check if it has any Y amount of characters before/after it), i tried this

         if (!myString.Contains(".".StartsWith("www")) &&  myString.Contains("."))
         {
             Console.WriteLine("Test");
         }
    

But it gives an error saying it can't convert from bool to string, i also tried creating dynamic dot = "."; and using it in place of "." and even though it compiled it would still crash when i run it.

Octavylon
  • 3
  • 2
  • 1
    `".".StartsWith("www")` returns a boolean value and when you pass it to `myString.Contains(...)` it gives you the error, because `.Contains(...)` expects a string to search for. – dimitar.d Jul 30 '20 at 11:52
  • Does this answer your question? [How to check whether a string is a valid HTTP URL?](https://stackoverflow.com/questions/7578857/how-to-check-whether-a-string-is-a-valid-http-url) – Sinatr Jul 30 '20 at 11:55
  • You are running into [XY problem](https://meta.stackexchange.com/q/66377/299295) and instead of asking how to validate web address asking about your attempted solution with string methods. – Sinatr Jul 30 '20 at 11:57
  • @Sinatr Yes but not fully, i still want to know how to do the things ive listed incase im not checking websites – Octavylon Jul 30 '20 at 12:09

3 Answers3

0
        string myString = "www.stackoverflow.com";

        var splittedString = (myString.Split('.'));

        if (splittedString[0] == "www")
        {
            ///before first occurrence of . has wwww
        }

        if (splittedString[splittedString.Length-1].Length == 3)
        {
            //has 3 characters after last .
        }
apomene
  • 14,282
  • 9
  • 46
  • 72
  • I don't understand the last part. The string is split into 3 halfs, shouldn't you have wrrote `if (splittedString[2].Length == 3)`? And how did you say `splittedString.Length` without specifying which cut ? – Octavylon Jul 30 '20 at 12:20
  • @Octavylon This certain string has two ., thus 3 parts. In general if you dont know how many dots the string has you must take the expression I wrote. – apomene Jul 30 '20 at 12:23
  • But how did u say `[splittedString.Length -1]` without specifying which part of the cut? How will it decide which cut? – Octavylon Jul 30 '20 at 12:36
  • @Octavylon Length-1 will always be the last cut – apomene Jul 30 '20 at 12:37
0

If you want to be able to validate all website urls use System.Uri

string url = "www.helloWorld.com";
if(Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
  {
  //Do Something
}
0

You can use regular expression:

            string input = "www.stackoverflow.com";
            int numOfChars = 13;
            Regex rex = new Regex($"www.[A-Za-z0-9]{{{numOfChars}}}.[A-Za-z]{{3}}");
            var res = rex.IsMatch(input); // res == true
dimitar.d
  • 645
  • 1
  • 6
  • 18