Как разбить строку на слова с
БлогNot. C++: как разобрать строку на слова средствами string?
C++: как разобрать строку на слова средствами string?
Во встроенном классе string, в отличие от нативных строк char * , нет такой же удобной функции для разбора на слова, как strtok в классическом C. Свойств вроде DelimitedText, как в VCL, тоже не наблюдается. Меж тем, задача остаётся актуальна, и решить её в простейшем случае можно так (консоль Visual Studio 2010):
Здесь бьётся, в общем, не на слова, а на лексемы, поэтому не исключаются знаки препинания, стоящие последними символами строк vecstr[i] , а также слова, не содержащие ни одного алфавитно-цифрового символа и т.п. Но направление, в котором можно смотреть для решения задачи, пример показывает 🙂
Разумеется, ничто не мешает сделать разбор и более традиционными, чем переопределённый оператор, ведь все методы для проверки и извлечения подстрок в классе string имеются. Например, по аналогии с Паскалем, можно так (по-прежнему дано только базовое разбиение на лексемы, но не анализ отдельных строк):
Split a string into words by multiple delimiters [duplicate]
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
I have some text (meaningful text or arithmetical expression) and I want to split it into words.
If I had a single delimiter, I’d use:
How can I break the string into tokens with several delimiters?
7 Answers 7
Assuming one of the delimiters is newline, the following reads the line and further splits it by the delimiters. For this example I’ve chosen the delimiters space, apostrophe, and semi-colon.
If you have boost, you could use:
Using std::regex
A std::regex can do string splitting in a few lines:
I don’t know why nobody pointed out the manual way, but here it is:
and in function:
This way you can do something useful with the delims if you want.
![]()
And here, ages later, a solution using C++20:
![]()
If you interesting in how to do it yourself and not using boost.
Assuming the delimiter string may be very long — let say M, checking for every char in your string if it is a delimiter, would cost O(M) each, so doing so in a loop for all chars in your original string, let say in length N, is O(M*N).
I would use a dictionary (like a map — «delimiter» to «booleans» — but here I would use a simple boolean array that has true in index = ascii value for each delimiter).
Now iterating on the string and check if the char is a delimiter is O(1), which eventually gives us O(N) overall.
Here is my sample code:
Note: tokenizeMyString returns vector by value and create it on the stack first, so we’re using here the power of the compiler >>> RVO — return value optimization 🙂
1.Introduction
Strings are one of the most interesting parts of the software development. We usually get input from a user as a string then process it to obtain the results. Dealing with the strings is a bit tricky so we need to know ways to handle frequent problems. Splitting a string(also known as tokenizing a string) can be perceived as dividing it into parts which we are more interested. Suppose that you need to extract words from a sentence where these words are separated by comma . You need an efficient and simple way to get the part you are interested in — the words — . Same thing applies for other types, you may need to extract numbers, special characters and even whitespaces. In this article, we will inspect the methods we can use when we need to split a string.
2.Logic of Splitting a String
We need to know essentials of this process:
String: The string we need to split.
Delimiter: The character or characters, even maybe another string that be used to divide the string from wherever it is found.
Given a string with different characters below and assume that our delimiter is the comma(‘,’) character :
The splitted string would be like this:
Where strings in each row represents a token. Now, if you have figured it out, we can continue with the implementations.
3.Using C++ Built-in(Native) Functions
C++ has dozens of functions that are dedicated for string operations. Also, to split a string, you can combine these functions.
3.1.Using strtok() function
This function exists in a C library which is named “string.h” or <cstring> in C++. You can also use it in C++, but first you have to convert C++ string to classical C string since it is a C function, then, you can convert it back to C++ string.
The strtok() Prototype and Explanation:
It returns the token, takes string and delimiters as input. For the first call, you need to pass a string for the first argument. But then, you have to pass a null pointer every time to continue splitting.
Algorithm:
Code:
Output:
3.2.Using string::rfind() function with string::substr() function
We can use string::rfind() function with string::substr() function. We use string::rfind() function to find the position of the delimiter(s) which we are interested in, starting from right(or reverse) and string::substr() function to create the substring which is the token itself and erase the found token from string.
Algorithm:
Code:
Output:
3.3.Using string::rfind() function with string::substr() function and string::erase() function
It is similar to 3.2. except we use string::erase() which is an inplace function to erase a part of a string. First argument for erase() function is position and second argument is the length for spanning. In example, if first parameter is 0 and second parameter is 8, it starts from position 0 and erases the 8 characters after this position.
Algorithm:
Code:
Output:
3.4.Using string::find() function with string::substr() function
We can use string::find() function with string::substr() function. We use string::find() function to find the position of the delimiter(s) which we are interested in, starting from left(or beginning) and string::substr() function to create a substring which is the token itself and erase the found token from string.
Algorithm:
Code:
Output:
3.5.Using stringstream with getline() function
We can create a stringstream object with our string and separate it using getline().
Algorithm:
Code:
Output:
4.Storing Tokens
After we have splitted the string, we may need to store them in an array, avector, or a list or any other data structure.
PowerCodX Blog
Здравствуйте уважаемые программисты. Сегодня мы будем обсудить как разделить строку на слова.
Допустим у нас есть строка: «This is a test string» и нам нужно получить слова из этой строки. Посмотрев на нашу строку то у нас возникает идея что надо как-то символ пробела (Space) сделать разделителем и все полученные слова поместить в какой нибудь массив. Для разделения строк на слова нам поможет замечательная функция Split.
String.Split это метод позволяющий разделить строку на подстроки с помощью разделителя. Синтаксис метода таков:
Давайте рассмотрим небольшой пример разделения строк на слова:
Думаю тут все понятно: