473,287 Members | 1,674 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,287 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 15238
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.