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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dönerbude
    New Member
    • Jun 2022
    • 1

    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 "
  • cactusdata
    Recognized Expert New Member
    • Aug 2007
    • 223

    #2
    Code:
    InputText = "Hello  "
    CleanedText = RTrim(InputText)
    
    ' CleanedTect -> "Hello"

    Comment

    • sakshijn
      New Member
      • Jun 2022
      • 2

      #3
      Remove Last Characters Of String using pop_back();

      Code:
      #include <iostream>
      using namespace std;
      int main()
      {
         string str;
         cin >> str;
         cout<<"Original String: "<<str<<endl;
         str.pop_back();
         cout<<"Required String: "<<str<<endl;
         return 0;
      }

      Remove Last Characters Of String using substr();

      Code:
      #include <iostream>
      using namespace std;
      int main() {
         string str;
         cin >> str;
         cout<<"Original String: "<<str<<endl;
         cout<<"Required String: "<<str.substr(0, str.size() - 1)<<endl;
         return 0;
      }

      Comment

      • Anushka00
        New Member
        • Jul 2022
        • 1

        #4
        In python you can use the strip() method. For example :
        Code:
        name = " hello "
        print(name.strip())
        The output of the code will be: hello

        Comment

        Working...