Questions tagged [stdstring]

std::string is the C++ standard library's byte-based "string" type, defined in the header.

1177 questions
996
votes
51 answers

How to trim an std::string?

I'm currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(" \n\r\t")+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with…
Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
770
votes
25 answers

How to concatenate a std::string and an int

I thought this would be really simple, but it's presenting some difficulties. If I have std::string name = "John"; int age = 21; How do I combine them to get a single string "John21"?
Obediah Stane
  • 15,471
  • 14
  • 39
  • 28
618
votes
17 answers

How to replace all occurrences of a character in string?

What is the effective way to replace all occurrences of a character with another character in std::string?
big-z
  • 6,812
  • 5
  • 20
  • 19
610
votes
44 answers

std::string formatting like sprintf

I have to format std::string with sprintf and send it into file stream. How can I do this?
Max Frai
  • 61,946
  • 78
  • 197
  • 306
356
votes
13 answers

convert a char* to std::string

I need to use an std::string to store data retrieved by fgets(). To do this I need to convert the char* return value from fgets() into an std::string to store in an array. How can this be done?
Jonathan Prior
  • 6,114
  • 7
  • 29
  • 26
293
votes
5 answers

Is it possible to use std::string in a constexpr?

Using C++11, Ubuntu 14.04, GCC default toolchain. This code fails: constexpr std::string constString = "constString"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... …
Vector
  • 10,879
  • 12
  • 61
  • 101
214
votes
20 answers

How do you append an int to a string in C++?

int i = 4; string text = "Player "; cout << (text + i); I'd like it to print Player 4. The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
Wawa
157
votes
18 answers

Alternative to itoa() for converting integer to string C++?

I was wondering if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I try to build my program under Linux, I get a compilation error.
Tomek
  • 4,689
  • 15
  • 44
  • 52
131
votes
12 answers

How to get the number of characters in a std::string?

How should I get the number of characters in a string in C++?
Elliot
  • 6,086
  • 11
  • 45
  • 57
129
votes
13 answers

What does string::npos mean in this code?

What does the phrase std::string::npos mean in the following snippet of code? found = str.find(str2); if (found != std::string::npos) std::cout << "first 'needle' found at: " << int(found) << std::endl;
boom
  • 5,856
  • 24
  • 61
  • 96
128
votes
7 answers

Legality of COW std::string implementation in C++11

It had been my understanding that copy-on-write is not a viable way to implement a conforming std::string in C++11, but when it came up in discussion recently I found myself unable to directly support that statement. Am I correct that C++11 does not…
acm
  • 12,183
  • 5
  • 39
  • 68
124
votes
22 answers

How to implode a vector of strings into a string (the elegant way)

I'm looking for the most elegant way to implode a vector of strings into a string. Below is the solution I'm using now: static std::string& implode(const std::vector& elems, char delim, std::string& s) { for…
ezpresso
  • 7,896
  • 13
  • 62
  • 94
111
votes
6 answers

How to convert std::string to NSString?

I am trying to convert a standard std::string into an NSString but I'm not having much luck. I can convert successfully from an NSString to a std::string with the following code NSString *realm = @"Hollywood"; std::string REALM = [realm…
Anthony McCormick
  • 2,724
  • 3
  • 25
  • 27
110
votes
4 answers

How to efficiently get a `string_view` for a substring of `std::string`

Using http://en.cppreference.com/w/cpp/string/basic_string_view as a reference, I see no way to do this more elegantly: std::string s = "hello world!"; std::string_view v = s; v = v.substr(6, 5); // "world" Worse, the naive approach is a pitfall…
sehe
  • 374,641
  • 47
  • 450
  • 633
108
votes
11 answers

How do you construct a std::string with an embedded null?

If I want to construct a std::string with a line like: std::string my_string("a\0b"); Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax?
Bill
  • 1,343
  • 4
  • 13
  • 19
1
2 3
78 79