views:

1303

answers:

5

I need to check whether an std:string begins with "xyz". How do I do it without searching through the whole string or creating temporary strings with substr().

Thanks.

+2  A: 

I feel I'm not fully understanding your question. It looks as though it should be trivial:

s[0]=='x' && s[1]=='y' && s[2]=='z'

This only looks at (at most) the first three characters. The generalisation for a string which is unknown at compile time would require you to replace the above with a loop:

// look for t at the start of s
for (int i=0; i<s.length(); i++)
{
  if (s[i]!=t[i])
    return false;
}
1800 INFORMATION
Well, I know how to compare strings in using C functions. My question was about doing it object-oriented way by means of C++ STL.
Jack
There is no C function being used here. And the Standard Library does not preclude you from writing unctions of your own.
anon
+3  A: 

Look to the Boost's String Algo library, that has a number of useful functions, such as starts_with, istart_with (case insensitive), etc. If you want to use only part of boost libraries in your project, then you can use bcp utility to copy only needed files

Alex Ott
A: 

I am really fed up when people demands : " I want to fly up to London but I don't want to use an airplane, neither a rocket and I even don't want to be transformed into a bird."

When I ask them "Any particular reason?", they say: "Airplane will burn a lot of fuel and cause pollution, the rocket is too fast for me and I don't want to loose my human-ship". These are the symptoms of pre-mature optimization and as Donald Knuth beautifully puts up

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

So, in a nutshell, please tell us "What you want to achieve" and not "How you want to achieve".

There is nothing wrong in using substr and string comparisons, so why not use it.

siddhant3s
I don't see the problem. Asking for a reasonably efficient way to test if a string begins with some known substring doesn't seem like premature optimization to me. He may be working on very long strings (where copying or traversing the entire string *would* get prohibitively expensive), or he may have profiled it and found that this is a bottleneck. Why do people feel the need to quote that "premature optimization" line in *every* even vaguely performance-related question. How do you know this isn't one of those 3% cases?
jalf
You also inexplicably left out half the quote. Here's the full version: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%"
jalf
+15  A: 

I would use compare method:

std::string s("xyzblahblah");
std::string t("xyz")

if (s.compare(0, t.length(), t) == 0)
{
// ok
}
Wacek
I'd forgotten about compare - this is the best method, but there is no need to use c_str(0 to get a character string
anon
So simple? Thanks!
Jack
You can do string s("xyz") == "xyz" too. :)
Skurmedel
Neil, you're absolutely right, I edited answer and removed c_str() call
Wacek
Why don't you simply use s.compare(t)?
Franck Mesirard
+1  A: 
if (mystring.find("xyz", 0) == 0)
{
   // you found it
}
else
{
   // you didn't find it, or it doesn't start with xyz
}
Brian Neal