Counting the number of characters inside double quotation marks

前端 未结 3 2048
失恋的感觉
失恋的感觉 2021-01-28 01:23

I want to find the number of characters inside a double quotation mark.

For example :

Case 1

\"Hello World\" , \"Some

output :

3条回答
  •  野性不改
    2021-01-28 01:25

    You could use something simple like finding the positions of the double quotes and subtracting them.

    static const std::string example = "The \"comment\" is quoted.";
    const std::string::size_type first_position = example.find('"');
    const std::string::size_type second_position = example.find('"', second_position + 1);
    const unsigned int character_quantity = second_position - first_position;
    

    There are issues with the above code, such as checking if the double quotes exist, that the OP should implement.

    This is one of many possible implementations.

提交回复
热议问题