Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to create the StringBuilder in C#
Next article icon

How to remove all characters from StringBuilder in C#

Last Updated : 28 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

StringBuilder.Clear Method is used to remove all the characters from the current StringBuilder instance.

Syntax:

public System.Text.StringBuilder Clear ();

It returns an StringBuilder object whose length will be zero.

Note:

  • Clear is a convenience method that is equivalent to setting the Length property of the current instance to 0 (zero).
  • Calling the Clear method does not modify the current instance’s Capacity or MaxCapacity property.

Example:




// C# program to demonstrate
// the Clear() Method
using System;
using System.Text;
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
        // Creating a StringBuilder object
        StringBuilder sb1 = new StringBuilder("GeeksforGeeks");
  
        // printing the length of "GeeksforGeeks"
        Console.WriteLine("The String is: {0} \nTotal characters -- {1}",
                                             sb1.ToString(), sb1.Length);
  
        // using the method
        sb1.Clear();
  
        // printing the length of "GeeksforGeeks"
        Console.WriteLine("The String is: {0} \nTotal characters -- {1}",
                                             sb1.ToString(), sb1.Length);
  
        sb1.Append("This  is C# Tutorials.");
  
        // printing the length of "GeeksforGeeks"
        Console.WriteLine("The String is: {0} \nTotal characters -- {1}",
                                             sb1.ToString(), sb1.Length);
    }
}
 
 
Output:
  The String is: GeeksforGeeks   Total characters -- 13  The String is:    Total characters -- 0  The String is: This  is C# Tutorials.   Total characters -- 22  

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.clear?view=netframework-4.7.2


Next Article
How to create the StringBuilder in C#

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-method
  • CSharp-StringBuilder-Class

Similar Reads

  • How to find the Capacity of a StringBuilder in C#
    StringBuilder.Capacity Property is used to get or set the maximum number of characters that can be contained in the memory allocated by the current instance. Syntax: public int Capacity { get; set; }Return Value: This property will return the maximum number of characters that can be contained in the
    2 min read
  • How to create the StringBuilder in C#
    StringBuilder() constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the default initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutabl
    2 min read
  • StringBuilder.Chars[] Property in C#
    StringBuilder.Chars[Int32] Property is used to get or set the character at the specified character position in this instance. Syntax: public char this[int index] { get; set; } Here, the index is the position of the character. Property Value: This property returns the Unicode character at position in
    2 min read
  • Convert a Character to the String in C#
    Given a character, the task is to character to the string in C#. Examples: Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A" Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string. /
    1 min read
  • How to create a StringCollection in C#
    StringCollection() constructor is used to initializing a new instance of the StringCollection class. StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace
    2 min read
  • C# | Removing all entries from the StringDictionary
    StringDictionary.Clear method is used to remove all the entries from the StringDictionary. Syntax: public virtual void Clear (); Exception: This method will give the NotSupportedException if the StringDictionary is read-only. Example: // C# code to remove all entries // from the StringDictionary usi
    2 min read
  • How to find the MaxCapacity of a StringBuilder in C#
    StringBuilder.MaxCapacity Property is used to get the maximum capacity of this instance. Syntax: public int MaxCapacity { get; } Property Value: It returns the maximum number of characters of type System.Int32 this instance can hold. Note: The maximum capacity for this implementation is Int32.MaxVal
    1 min read
  • StringBuilder.CopyTo Method in C#
    This method is used to copy the characters from a specified segment of this instance to a specified segment of a destination Char array. Syntax: public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count); Parameters: sourceIndex: It is the starting position in this ins
    2 min read
  • How to find the length of the StringBuilder in C#
    StringBuilder.Length Property is used to get or set the length of the current StringBuilder object. Syntax: public int Length { get; set; } It returns the length of the current instance. Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less
    2 min read
  • Different Methods to Read a Character in C#
    In C#, we know that Console.Read() method is used to read a single character from the standard output device. And also there are different methods available to read the single character. Following methods can be used for this purpose: Console.ReadLine()[0] MethodConsole.ReadKey().KeyChar MethodChar.
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences