user7157477
user7157477

Reputation: 45

Substring a char c++

Untitled1.cpp request for member 'substr' in 'ReceiveBuf', which is of non-class type 'char[1024]'

I want to remove everything after space but I have no idea how to do it with char, I only know how to do it with a string.

Upvotes: 0

Views: 490

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

If the array contains a string or you are sure that there is space character in the array then you can write

if ( char *p = strchr( ReceiveBuf, ' ' ) ) *p = '\0';

or

ReceiveBuf[ strcspn( ReceiveBuf, " \t" ) ] = '\0';

Or you can create a new object of the type std::string the following way

std::string s( ReceiveBuf, strcspn( ReceiveBuf, " \t" ) );

Upvotes: 1

Related Questions