Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Can someone help me in creating a method that will remove any given character from a String?

user-image
Question ajoutée par Zain Khater , Project Supervisor – StartUp Project , Injaz
Date de publication: 2016/12/29
Ayman Bushnaq
par Ayman Bushnaq , Social Media Executive , IEEE

I can't tell if you want to delete a single occurrence of the char or ALL the occurrences, but I'll assume you mean All unless you specify otherwise.

#include <iostream>#include<algorithm>using namespace std;int main() {        string str;        getline(cin, str); //get string input from direct input.char eraser='\\\\0'; //initialize char to NULL.cin>>eraser; //get the charachter you wanna erase from direct input.str.erase(std::remove(str.begin(), str.end(), eraser), str.end()); //erases ALL OCCURENCES of the chosen charcout<<str; //show result to check.return0;}I hope this helps. of course this isn't a method but you can implement the solution to a method easily.All the Best,Ayman

Shomaf Saheb
par Shomaf Saheb , Senior software developer , Körber supply chain automation GmbH

in c++11 zou can do it this way:std::string replace(std::string _your_string_here_,std::string _given_character_here_)

{

std::string string(_your_string_here_);

return std::regex_replace(string, _given_character_here_), "");

}

Utilisateur supprimé
par Utilisateur supprimé

This site has the answer in java https://www.roseindia.net/java/beginners/remove-character-from-string.shtml

Shafi Ahmad
par Shafi Ahmad , Principal Software Engineer , Microsoft India Pvt Ltd

C:

#include <stdio.h>

 

void removeAllChar(char* str, char c) {

    char *ptemp = str, *pnext = str;

    while (*ptemp) {

        *pnext = *ptemp++;

        pnext += (*pnext != c);

    }

    *pnext = '\\0';

}

 

int main() {

    char str[] = "Can someone help me in creating a method that will remove any given character from a String?";

    char c = 'o';

    removeAllChar(str, c);

    printf("String after removing the character '%c' : '%s'\\n", c, str);

    return 0;

}

 

C++:

#include <iostream>

#include <string>

#include <algorithm>

 

int main()

{

    std::string str = "Can someone help me in creating a method that will remove any given character from a String?";

    char c = 'e';

 

    /* Removing character c from s */

    str.erase(std::remove(str.begin(), str.end(), c), str.end());

    std::cout << "String after removing the character " << c << " : " << str << "\\n";

}

 

Vishnu Viswagokul Kaithavalappil
par Vishnu Viswagokul Kaithavalappil , Junior Software Engineer , Device Driven

Giving you two solution to the question you have given, First one is the most easiest one but it has got a fault as if the character is different case as that of the character in the string then they wont be removed, a quick fix to that is to convert the given string and the word into the same case and then removing the character but this way while returning the original string will get effected as well.

 

And the second solution looks like a perfect fix, but its kinda long run, so what you does there is you compare each character in the given string converting both the character from the string and the given character to lower case and then building the string and returns the list.

The code can be further reduced with the help of streams, but after all, you only needs a method therefore not looking much into reducing the number of statements in the code and confusing you. :)

 

And yeah made them static because too lazy to create an object in main and call them :P

 

private static String removeThisFromStringS(char word, String string) {

  return string.replace(word,'\\0');

 

private static String removeThisFromStringE(char word, String string) {

  word = Character.toLowerCase(word);

  String result = "";

  for(char c : string.toCharArray()) {

  if(Character.toLowerCase(c) != word) {

    result+=c;

  }

 }

 return result;

}

More Questions Like This