Skip to content
Related Articles

Related Articles

Program to remove HTML tags from a given String
  • Last Updated : 18 Feb, 2021

Given a string str which contains some HTML tags, the task is to remove all the tags present in the given string str.
Examples: 
 

Input: str = “<div><b>Geeks for Geeks</b></div>” 
Output: Geeks for Geeks
Input: str = “<a href=”https://www.geeksforgeeks.org/”>GFG</a>” 
Output: GFG 
 

 

Approach: The idea is to use the Regular Expression to solve this problem. The following steps can be followed to compute the resultant string: 
 

  1. Get the string.
  2. Since every HTML tags are inclosed in angular brackets(<>). Therefore use replaceAll() function in regex to replace every substring start with “<“ and ends with “>” to empty string.
  3. The function is used as: 
     
String str;
str.replaceAll("\\", "");
  1.  

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
#include <regex>
using namespace std;
 
// Function to remove the HTML tags
// from the given string
void RemoveHTMLTags(string s)
{
  const regex pattern("\\<.*?\\>");
 
  // Use regex_replace function in regex
  // to erase every tags enclosed in <>
  s = regex_replace(s, pattern, "");
 
  // Print string after removing tags
  cout << s;
 
  return ;
}
 
// Driver Code
int main()
{
   
  // Given String
  string str = "<div><b>Geeks for Geeks</b></div>";
 
  // Function call to print the
  // HTML string after removing tags
  RemoveHTMLTags(str) ;
 
  return 0;
}
 
// This code is contributed by yuvraj_chandra

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
 
class GFG {
 
    // Function to remove the HTML tags
    // from the given tags
    static void RemoveHTMLTags(String str)
    {
 
        // Use replaceAll function in regex
        // to erase every tags enclosed in <>
        str = str.replaceAll("\\<.*?\\>", "");
 
        // Print string after removing tags
        System.out.println(str);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str;
 
        // Given String
        str = "<div><b>Geeks for Geeks</b></div>";
 
        // Function call to print the
        // HTML string after removing tags
        RemoveHTMLTags(str);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the
# above approach
import re
 
# Function to remove the HTML tags
# from the given tags
def RemoveHTMLTags(strr):
     
    # Print string after removing tags
    print(re.compile(r'<[^>]+>').sub('', strr))
     
# Driver code
if __name__=='__main__':
     
    # Given String
    strr = "<div><b>Geeks for Geeks</b></div>"
     
    # Function call to print the HTML
    # string after removing tags
    RemoveHTMLTags(strr);
     
# This code is contributed by vikas_g

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
 
// Function to remove the HTML tags
// from the given tags
static void RemoveHTMLTags(String str)
{
     
    // Use replaceAll function in regex
    // to erase every tags enclosed in <>
    // str = Regex.Replace(str, "<.*?>", String.Empty)
    System.Text.RegularExpressions.Regex rx =
    new System.Text.RegularExpressions.Regex("<[^>]*>");
     
    str = rx.Replace(str, "");
 
    // Print string after removing tags
    Console.WriteLine(str);
}
 
// Driver code
public static void Main(String []args)
{
    String str;
 
    // Given String
    str = "<div><b>Geeks for Geeks</b></div>";
 
    // Function call to print the
    // HTML string after removing tags
    RemoveHTMLTags(str);
}
}
 
// This code is contributed by vikas_g

chevron_right


Output: 

Geeks for Geeks

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :