Finding

Thread: Finding \0 in a string

in a string

This is a discussion on Finding

Thread: Finding \0 in a string

in a string
within the C++ Programming forums, part of the General Programming Boards category; Like the title states, would anyone know how to find (and remove) a

Thread: Finding \0 in a string

from a std::string? You're probably wondering ...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Finding \0 in a string

    Like the title states, would anyone know how to find (and remove) a \0 from a std::string? You're probably wondering why that would be in there in the first place, but it doesn't really matter. Let's say we had a string like "Hello \0 world", and I wanted to get rid of the \0 so I could see the whole string. Does anyone have any ideas? I've tried this but it doesn't work:
    Code:
    string str = "Hello,\0 world!";
    str.erase(str.length() + 1, 1);
    Can anyone help? Thanks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by mikeman118 View Post
    Like the title states, would anyone know how to find (and remove) a \0 from a std::string? You're probably wondering why that would be in there in the first place, but it doesn't really matter. Let's say we had a string like "Hello \0 world", and I wanted to get rid of the \0 so I could see the whole string. Does anyone have any ideas? I've tried this but it doesn't work:
    Code:
    string str = "Hello,\0 world!";
    str.erase(str.length() + 1, 1);
    Can anyone help? Thanks.
    That initializer won't work, because it is invoked std::string's constructor which takes a C-string. It will stop when it hits the null byte.

    As for how to locate a null byte in a string, just use find(). Then use the resulting index to erase() it.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    This might work:
    Code:
    std::string str("Hello\0 World",12);
    
    std::string::size_type pos = str.find('\0');
    
    if( pos != std::string::npos )
    {
        str.erase(pos,1);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    I'm not sure offhand if
    Code:
    std::string str("Hello\0 World", 12);
    works for initialising str or not with that embedded '\0'; there's the incidental interaction with the convention that a C-style string stops at the first '\0' character.

    One way to force the issue would be to manipulate the string as a container;
    Code:
    std::string str("Hello");
    str.append('\0');
    str.append(" World");
    To find the '\0' character, find() can be used.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I tried it in Visual Studio 2005 and that method of initialization did... something. I was able to find and delete the '\0'. I also printed the string before the deletion and "Hello World" (two spaces) was printed... seems that the '\0' gets converted to a space when output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM