I mostly write my codes in java and have started using c++ too. I wanted to know how to check if a given string in c++ starts with another specified string. I have posted the equivalent code in java below.
public boolean check(String string) //ENTERED string
{
String another_string="SSS"; //to be checked if the ENTERED string starts with this string
return (string.startsWith(another_string)); //<string>.startsWith(<string>) returns a boolean value
}
http://ideone.com/w1ifiJ
#include <iostream>
using namespace std;
int main() {
string str ("abcdefghijklmnoabcde");
string str2 ("abcde");
size_t found = str.find(str2);
if(found == 0)
{
cout << "found";
}
return 0;
}
more info : http://www.cplusplus.com/reference/string/string/find/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With