How to Check If a String Starts With a Specified String

How to check if a string starts with a specified string?


PHP 8 or newer:

Use the str_starts_with function:

str_starts_with('http://www.google.com', 'http')

PHP 7 or older:

Use the substr function to return a part of a string.

substr( $string_n, 0, 4 ) === "http"

If you're trying to make sure it's not another protocol. I'd use http:// instead, since https would also match, and other things such as http-protocol.com.

substr( $string_n, 0, 7 ) === "http://"

And in general:

substr($string, 0, strlen($query)) === $query

How to check if a string starts with another string - PHP

Using strpos function can be achieved.

if (strpos($yourString, "repair") === 0) {
//Starts with it
}

Using substr can work too:

if (substr($yourstring, 0, strlen($startString)) === $startString) {
//It starts with desired string
}

For multi-byte strings, consider using functions with mb_ prefix, so mb_substr, mb_strlen, etc.

Checking if a string begins with a given string

Write your StartHi method like below using Split

    public static bool StartHi(string str)
{
bool firstHi = false;
if (string.IsNullOrEmpty(str))
{
Console.WriteLine("The string is empty!");
Console.ReadLine();
return false;
}

var array = str.Split(new string[] {" "}, StringSplitOptions.None);
if (array[0].ToLower() == "hi")
{
firstHi = true;
Console.WriteLine("The string starts with \"hi\"");
}
else
{
firstHi = false;
Console.WriteLine("The string doesn't start with \"hi\"");
}

Console.ReadLine();

return firstHi;
}

Note:
If you have other strings like "hi!" or "Hi? Don't say hi to me", then you can extend the Split to something like below.

var array = str.Split(new string[] {" ", "!", "?"}, StringSplitOptions.None);
if (array[0].ToLower() == "hi") //convert to lower and check

Regex is probably your best bet if it gets more complicated and look towards it. I can't give it one since I'm not great with it.

How to check a string starts with a specified string in kotlin?


   val s = "hello"
println(s.startsWith("hel")) // true
println(s.startsWith("hal")) // false

How to check if a string starts with another string in C?

Apparently there's no standard C function for this. So:

bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0;
}

Note that the above is nice and clear, but if you're doing it in a tight loop or working with very large strings, it does not offer the best performance, as it scans the full length of both strings up front (strlen). Solutions like wj32's or Christoph's may offer better performance (although this comment about vectorization is beyond my ken of C). Also note Fred Foo's solution which avoids strlen on str (he's right, it's unnecessary if you use strncmp instead of memcmp). Only matters for (very) large strings or repeated use in tight loops, but when it matters, it matters.

How to check if a variable starts with a specific string?

You could use this, to match for any length string. Instead of hardcoding the string and it's length.

$string = "string to test";
$testfor = "strin";
if(substr( $string, 0, strlen($testfor) ) === $teststr) {
echo "Match";
}

Check if a string starts with another known string?

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/

How to check if a string StartsWith another string?

You can use ECMAScript 6's String.prototype.startsWith() method. It's supported in all major browsers. However, if you want to use it in a browser that is unsupported you'll want to use a shim/polyfill to add it on those browsers. Creating an implementation that complies with all the details laid out in the spec is a little complicated. If you want a faithful shim, use either:

  • Matthias Bynens's String.prototype.startsWith shim, or
  • The es6-shim, which shims as much of the ES6 spec as possible, including String.prototype.startsWith.

Once you've shimmed the method (or if you're only supporting browsers and JavaScript engines that already have it), you can use it like this: