in a string
This is a discussion on Finding 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
from a std::string? You're probably wondering ...
-
Registered User
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.
-
Captain Crash

Originally Posted by
mikeman118
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.
-
Registered User
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);
}

I used to be an adventurer like you... then I took an arrow to the knee.
-
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.
-
Registered User
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.

I used to be an adventurer like you... then I took an arrow to the knee.
Popular pages Recent additions
Similar Threads
-
By John_ in forum C++ Programming
Replies: 8
Last Post: 06-13-2006, 06:52 PM
-
By Brain Cell in forum C++ Programming
Replies: 9
Last Post: 04-14-2005, 05:13 PM
-
By CJ7Mudrover in forum C Programming
Replies: 9
Last Post: 03-10-2004, 09:33 PM
-
By JCK in forum C++ Programming
Replies: 12
Last Post: 12-08-2002, 01:45 PM
-
By spentdome in forum C Programming
Replies: 25
Last Post: 05-27-2002, 06:49 PM