How to detect and remove invisible or non‑printing characters that break a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
 
// Detect & Remove ASCII Control Characters
std::string removeNonPrintable(const std::string& input) {
    std::string output;
    for (unsigned char ch : input) {
        if (std::isprint(ch)) {
            output += ch;
        }
    }
     
    return output;
}
// Remove control characters but Keep spaces, tabs, and newlines
std::string removeControlChars(const std::string& input) {
    std::string output;
    for (unsigned char ch : input) {
        if (!std::iscntrl(ch)) {
            output += ch;
        }
    }
     
    return output;
}
 
// If you want to see what’s hiding
void debugPrint(const std::string& s) {
    for (unsigned char ch : s) {
        if (std::isprint(ch))
            std::cout << ch;
        else
            std::cout << "\\x" << std::hex << std::setw(2)
                      << std::setfill('0') << (int)ch;
    }
    std::cout << "\n";
}
 
int main() {
    // Contains ASCII control chars + Unicode zero-width space
    std::string raw = "Hello\x01\x02 Milky Way Galaxy\u200B!";
 
    std::cout << "Original (debug): ";
    debugPrint(raw);
 
    std::string asciiClean = removeNonPrintable(raw);
    std::cout << "After ASCII clean: ";
    debugPrint(asciiClean);
    
    std::cout << "After removeControlChars(): ";
    std::cout << removeControlChars("Hello\x01\x02 Milky Way Galaxy\u200B!");
}
 
 
   
/*
run:
   
Original (debug): Hello\x01\x02 Milky Way Galaxy\xe2\x80\x8b!
After ASCII clean: Hello Milky Way Galaxy!
After removeControlChars(): Hello Milky Way Galaxy​!

*/

 



answered Dec 23, 2025 by avibootz
edited Dec 23, 2025 by avibootz
...