Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove url from a given string in C#

Tags:

c#

I tried doing this:

using System;
using System.Collections.Generic;
using System.Text;

namespace UrlsDetector
{
    class UrlDetector
    {
        public static string RemoveUrl(string input)
        {
            var words = input;
            while(words.Contains("https://"))
            {
                string urlToRemove = words.Substring("https://", @" ");
                words = words.Replace("https://" + urlToRemove , @"");
            }
        }
        
    }

    class Program
    {
        static void Main()
        {
            Console.WriteLine(UrlDetector.RemoveUrl(
                "I saw a cat and a horse on https://www.youtube.com/"));

        }
    }
}

but it doesn't work.

What I want to achieve is remove the entire "https://www.youtube.com/" and display "I saw a cat and a horse on".

I also want to display a message like "the sentence you input doesn't have url" if the sentence doesn't have any url.

like image 358
James Exe Avatar asked Aug 07 '26 06:08

James Exe


1 Answers

If you are looking for a non RegEx way to do this, here you go. But the method I encoded below assumes that a URL begins with "http://" or "https://", which means it will not work with URL's that begin with something like ftp:// or file://, although the code below can be easily modified to support that. Also, it assumes the URL path continues until it reaches either the end of the string or a white space character (like a space or a tab or a new line). Again, this can easily be modified if your requirements are different.

Also, if the string contains no URL, currently it just returns a blank string. You can modify this easily too!

using System;

public class Program
{
    public static void Main()
    {
        string str = "I saw a cat and a horse on https://www.youtube.com/";

        UrlExtraction extraction = RemoveUrl(str);
        Console.WriteLine("Original Text: " + extraction.OriginalText);
        Console.WriteLine();
        Console.WriteLine("Url: " + extraction.ExtractedUrl);
        Console.WriteLine("Text: " + extraction.TextWithoutUrl);
    }

    private static UrlExtraction RemoveUrl(string str)
    {       
        if (String.IsNullOrWhiteSpace(str))
        {
            return new UrlExtraction("", "", "");
        }

        int startIndex = str.IndexOf("https://", 
                StringComparison.InvariantCultureIgnoreCase);

        if (startIndex == -1)
        {
            startIndex = str.IndexOf("http://", 
                StringComparison.InvariantCultureIgnoreCase);
        }

        if (startIndex == -1)
        {
            return new UrlExtraction(str, "", "");
        }

        int endIndex = startIndex;
        while (endIndex < str.Length && !IsWhiteSpace(str[endIndex])) 
        {           
            endIndex++;
        }

        return new UrlExtraction(str, str.Substring(startIndex, endIndex - startIndex), 
            str.Remove(startIndex, endIndex - startIndex));
    }

    private static bool IsWhiteSpace(char c)
    {
        return 
            c == '\n' || 
            c == '\r' || 
            c == ' ' || 
            c == '\t';
    }

    private class UrlExtraction
    {
        public string ExtractedUrl {get; set;}
        public string TextWithoutUrl {get; set;}
        public string OriginalText {get; set;}

        public UrlExtraction(string originalText, string extractedUrl, 
            string textWithoutUrl)
        {
            OriginalText = originalText;
            ExtractedUrl = extractedUrl;
            TextWithoutUrl = textWithoutUrl;
        }
    }
}
like image 79
Icemanind Avatar answered Aug 08 '26 20:08

Icemanind