The difference between substr() in C++ and substring() in java

Note: There is only one substring operation function in C++: s.substr(); there is no s.substring() function, s.substring() is in java.
There are operations on substrings in both Java and C++, substr() in C++ and substring() in Java. The usage of the two is slightly different.

1. First of all, for the case of only one parameter:

s.substr(start) 和 s.substring(start) 

Both represent the substring from the start position to the end.

2. For the case of two parameters:
there is a difference between the two:
C++: substr(start, len),The starting position of the first parameter, the second parameter is the length of the substring

Java: substring(start, end),The first parameter is the starting position, the second parameter is the ending position

3. You can refer to the following example code:
C++:

string s = "HelloWorld";
cout << s.substr(4, 4) << endl; // oWor
cout << s.substr(4) << endl; // oWorld

Java:

String s = "HelloWorld";
System.out.println(s.substring(4, 8)); // oWor
System.out.println(s.substring(4)); // oWorld

Reprinted from: The difference between C++ substr() and Java substring()

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/106649623