How to set a string null in C++ - CodeSpeedy (2024)

Hello readers! In this tutorial, we are going to learn how to set a string null in C++. Let’s have a look at how we can do this. We can do this using the std::string::clear function.

void::string::clear

It helps to clear the whole string and sets its value to null (empty string) and the size of the string becomes 0 characters. string::clear does not require any parameters, does not return any error, and returns a null value.

Let’s look at the code as an example

C++: Set a string to null

#include <iostream>using namespace std;void clearstring(string str) //function to clear string{ str.clear(); // sets the string to null cout << "how string looks after : "; cout << str;}// Driver codeint main(){ string str("Codespeedy coding platform"); //initialize the string cout << "how string looks before : "; cout << str << endl; clearstring(str); return 0;}

Output:

how string looks before : Codespeedy coding platformhow string looks after :

Explanation to the code

First, we will call the function clear string which will clear all the content that already exist then we will use str clear which doesn’t contain any parameter and do not return any value this will set the string value to null.

I hope you have found this post useful!

'); } if (paragraph_for_ad.length > 15) { let adparagraph = paragraph_for_ad[10]; adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 30) { let adparagraph = paragraph_for_ad[25]; adparagraph.insertAdjacentHTML('afterEnd', '

'); } } else if(window.innerWidth > 1360) { if (paragraph_for_ad.length > 15) { let adparagraph = paragraph_for_ad[10]; // 71161633/article_incontent_1/article_incontent_1 adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 22) { let adparagraph = paragraph_for_ad[18]; // 71161633/article_incontent_2/article_incontent_2 adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 30) { let adparagraph = paragraph_for_ad[29]; // 71161633/article_incontent_1/article_incontent_1 adparagraph.insertAdjacentHTML('afterEnd', '

How to set a string null in C++ - CodeSpeedy (2024)
Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6516

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.