C++C++11C++14C++17Introduction to C++Learn C++

How To Use WString find_first_of() In A Modern C++ App

How To Use WString findfirstof In A Modern C++ App

How can I search only a single character while the search is a wide string in my C++ app? What kind of methods I can use to find a character in a std::wstring? How can I use find_first_of(), find_last_of() methods with wstrings?

Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std::wstring types that are defined in header <string>.

wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other terms, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In modern C++, simply we can define a wide string with L” ” literal as below,

Wide Strings (wstring) are a class contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. Note that, In C++, while wide string contents are defined between L” and with literal L, wide characters are defined between L’ and with literal L

The wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax.We can find characters in a string by using find_first_of() and find_last_of() methods.

What if my strings are not Wide strings in my C++ app?

We discussed the find_first_of and find_last_of methods for std:string in the following article.

How to search a Wide string with the find_first_of() method in a C++ app

The find_first_of() Method is a String Method that finds the first character of a wide string (a character sequence) in another wide string. In this method, it searches only a single character while the search is a string. If the character is not present in a wstring it returns the npos of the wstring (std::string::npos).

Simply we can find a character of a wide string in another wide string by using its find_first_of() method as given syntax.

Syntax:

Here we can find a character of a wide string in another wide string by using its find_first_of() method as given full example below.

Output of this code will be as below,

As you see here, we find the first ‘r’ character in our wide string at the position 3

How to search a Wide String with the find_last_of() method in a C++ app

The find_last_of() Method is a String Method that finds the last character of a wide string (a character sequence) in another wide string. In this method, it searches only a single character while the search is a wide string. If the character is not present in a wide string it returns the npos of the wide string (std::wstring::npos).

Simply we can find a character of a wstring in another wstring by using its find_last_of() method as given syntax.

Syntax:

Here we can find a character of a wstring in another wstring by using its find_last_of() method as given full example below.

Output of this code will be as below,

as you see we found the last ‘r’ character which is in “.org” part at the position 16.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Learn C++

What is A Void Function In The C Programming language?

C++Introduction to C++Learn C++

How To Run A Program Written In The C Language

C++Introduction to C++Learn C++

How To Compile And Run A C Program

C++Introduction to C++Learn C++

How To Exit A C Program In Power Shell