0

I'm trying to print the substring of a string but the substr() function's behaviour seems odd to me.

In my below program, I'm trying to print the substring of a string with value "Kamal".

Here, substr(0,4) prints "Kama" which makes me assume that 0 is inclusive and 4 is exclusive. But substr(1,4) prints "amal" which violates my above understanding since the character in position 4 is printed while in previous case it's not. Can you explain this strange behaviour.

If this question is really dumb, sry this is the first time I'm using substr(). I've searched the net but can't seem to find any answer useful.

#include<iostream>
#include<string>
using namespace std;

int main() {
    string s1 = "Kamal";

    cout << s1.substr(0, 4) << '\n';
    cout << s1.substr(1, 4) << '\n';

    return 0;
}

Output: enter image description here

TontonVelu
  • 471
  • 2
  • 8
  • 21
ALLAN
  • 71
  • 6
  • 1
    Your first number is the index where substring starts, and second number is the length of the substring – Ranoiaetep Oct 20 '20 at 06:33
  • The 2nd parameter means *count*, i.e. *the length of the substring*. See [`substr`](https://en.cppreference.com/w/cpp/string/basic_string/substr). – songyuanyao Oct 20 '20 at 06:33
  • @songyuanyao The link to Cppreference.com or the Interllisence information on Visual Studio could be quite intimidating for beginners, but once you've tried to read through them and understand them, they would be really easy and helpful! – Ranoiaetep Oct 20 '20 at 06:39
  • Guys Thanks for the answers. I got it. I thought the second value of substr() as ending index instead of the length of the substring which is to be obtained – ALLAN Oct 20 '20 at 06:41
  • 1
    [cplusplus.com](http://www.cplusplus.com/reference/string/string/substr/) – pradeexsu Oct 20 '20 at 06:42
  • when in doubt, read the documentation first – phuclv Oct 20 '20 at 08:13
  • 1
    @sutharp777 don't use that site which contains [lots of wrong or outdated information](https://stackoverflow.com/q/6520052/995714). https://en.cppreference.com/w/cpp is actively maintained by experts and is much more reliable and highly updated – phuclv Oct 20 '20 at 15:52
  • thank you @phuclv, from now I will use cppreference insted of cplusplus – pradeexsu Oct 21 '20 at 01:58

1 Answers1

5
string substr (size_t pos = 0, size_t len = npos) const;

Generate substring Returns a newly constructed string object with its value initialized to a copy of a substring of this object.

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

Parameters:

pos:

Position of the first character to be copied as a substring. If this is equal to the string length, the function returns an empty string. If this is greater than the string length, it throws out_of_range. Note: The first character is denoted by a value of 0 (not 1).

len:

Number of characters to include in the substring (if the string is shorter, as many characters as possible are used). A value of string::npos indicates all characters until the end of the string.

AntonioSk
  • 528
  • 3
  • 20
  • I thought the second value of substr() as end index. Now I get that it denotes the length of the output string (ie) the length of the substring. Thanks got it! – ALLAN Oct 20 '20 at 06:40