Search code examples
c++stringstring-parsing

How do I parse a string based on a certain number of characters in the string in C++?


I know how to parse a string based on a delimiter, such as a comma. Does this require turning the string into an array of char?

I am wanting to compare a string that is 6 numebrs i.e. 111222

with another string of numbers that is 12 characters long but I only want the first six.

Basically way to check that 111222 occurs in the string 111222345678.


Solution

  • ....but I only want the first six

    For first n chars comparison you can use std::strncmp

    char s1[] ="111222345678" ;
    char s2[] ="111222";
    
    std::cout << std::strncmp( s1,s2, 6 ) ; // n =6 chars