Advertisement
dimipan80

Advanced Topics 15. Extract URLs from Text

Jul 4th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. // Write a program that extracts and prints all URLs from given text.
  2.  
  3. namespace _15.ExtractURLsFromText
  4. {
  5.     using System;
  6.  
  7.     public class ExtractURLsFromText
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.WriteLine("Enter your Text on single line!");
  14.                 string inputText = Console.ReadLine();
  15.  
  16.                 char[] separators = new char[] { ' ', ',', ';' };
  17.                 string[] text = inputText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  18.                 if (text.Length > 0)
  19.                 {
  20.                     Console.WriteLine("The URLs in Given Text are:");
  21.                     for (int i = 0; i < text.Length; i++)
  22.                     {
  23.                         string word = text[i];
  24.                         bool isWebSite = word.Contains("http://") || word.Contains("www.");
  25.                         if (isWebSite)
  26.                         {
  27.                             Console.WriteLine(word);
  28.                         }
  29.                     }
  30.                 }
  31.                 else
  32.                 {
  33.                     Console.WriteLine("Error! - Invalid Input!!!");
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement