473,416 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to remove last characters of String if they contain " "

I want to remove the last characters which are " " but the number of empty characters in the end varies.

sometimes it is:

"hello "

sometimes:
"hello "

sometimes:
"hello "
Jun 23 '22 #1
3 15329
cactusdata
214 Expert 128KB
Expand|Select|Wrap|Line Numbers
  1. InputText = "Hello  "
  2. CleanedText = RTrim(InputText)
  3.  
  4. ' CleanedTect -> "Hello"
Jun 24 '22 #2
sakshijn
2 2Bits
Remove Last Characters Of String using pop_back();

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.    string str;
  6.    cin >> str;
  7.    cout<<"Original String: "<<str<<endl;
  8.    str.pop_back();
  9.    cout<<"Required String: "<<str<<endl;
  10.    return 0;
  11. }

Remove Last Characters Of String using substr();

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.    string str;
  5.    cin >> str;
  6.    cout<<"Original String: "<<str<<endl;
  7.    cout<<"Required String: "<<str.substr(0, str.size() - 1)<<endl;
  8.    return 0;
  9. }
Jun 27 '22 #3
Anushka00
1 Bit
In python you can use the strip() method. For example :
Expand|Select|Wrap|Line Numbers
  1. name = " hello "
  2. print(name.strip())
  3.  
The output of the code will be: hello
Jul 12 '22 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
5
by: brckcc | last post by:
Sometimes when I view my string variables in the watch window, it shows an @ symbol preceding the value. Other times, I don't see the @ symbol. What does this mean?
6
by: Ole Hanson | last post by:
Hi I am wondering if there is any programmatic difference between the String.Empty and the "" for declaring an empty string? Furthermore, is there any performance penalty by doing the one over...
6
by: G. | last post by:
This is an obvious bug in the String.Replace function: //load a XML string into a document XmlDocument doc = new XmlDocument(); doc.LoadXml("<test id='' />"); //Obtain the string...
4
by: michi | last post by:
Hello How can I convert a string to double with a decimal point as seperator? ***CODE**** strNumber ="12.00" dblDouble = Cdbl(strNumber) ****
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
9
by: CeyloR | last post by:
Can anyone tell me what the "c" means in a statement like this: strTest = New String(" "c,10) I know the 10 in above example stands for the maxlength of characters in the string. Is it posible...
6
by: Charlie | last post by:
Hi: Is there any difference between string.Empty and String.Empty? And what is the benefit of using it over "". Thanks, Charlie
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
1
by: Lawrence Lichte | last post by:
Whay can't I end a string assignment with the '\'.... fylpath = "c:\scripts\python\prod_select\" the last '\' isn't included in the string when I print the string. I was using the string to define...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.