Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string starts with another known string? [duplicate]

Tags:

c++

string

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

}
like image 981
Piyush Ranjan Avatar asked Oct 20 '25 09:10

Piyush Ranjan


1 Answers

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/

like image 72
utarid Avatar answered Oct 21 '25 23:10

utarid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!