Namespaces
Variants
Views
Actions

Talk:cpp/string/basic string/substr

From cppreference.com

This page contains conflicting information:

Returns a substring [pos, pos+count). If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos, size()).

vs.

Exceptions std::out_of_range if pos > size()


--Per Malmberg (talk) 04:56, 4 April 2017 (PDT)

In the first case, pos is less than size, in the second case, pos is greater than size. Added the throwing case to the example --Cubbi (talk) 05:33, 4 April 2017 (PDT)


[edit] Past the end of string

If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos, size()).

Shouldn't that be [pos, size()-pos] ? As written, this doesn't really explain anything since if pos>0, [pos, size()] will extend past the end of the string, and the explanation becomes circular. --204.138.153.254 12:45, 10 October 2018 (PDT)

The [pos, size()) is using half-open interval notation of the form [begin, end) --Ybab321 (talk) 07:31, 19 October 2018 (PDT)
It's not an interval. It's a position and length. The article is wrong. 90.230.55.237 23:05, 1 June 2020 (PDT)
exactly Rob42 (talk) 07:07, 18 September 2020 (PDT)


[edit] npos

The default value of the second argument is npos. But the page does not specify what it is. 179.154.143.45 06:41, 19 December 2019 (PST)

I've made it a link now, and added it to the "See also" section as well. --Hadriel (talk) 22:49, 19 December 2019 (PST)
I think std::string::npos is not good, because it requires a specied specialization while this function belongs to all specializations of std::basic_string. std::basic_string::npos might be better, but it's not a valid qualified-id. --Fruderica (talk) 22:31, 19 February 2020 (PST)