Questions tagged [strstr]

A function that searches a string in another string.

A function that searches a string in another string.

Examples

php
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

(Example taken from php.net)

Resources

363 questions
90
votes
5 answers

Which method is preferred strstr or strpos?

I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?
johnlemon
  • 20,761
  • 42
  • 119
  • 178
52
votes
11 answers

Obtain first line of a string in PHP

In PHP 5.3 there is a nice function that seems to do what I want: strstr(input,"\n",true) Unfortunately, the server runs PHP 5.2.17 and the optional third parameter of strstr is not available. Is there a way to achieve this in previous versions in…
kinokijuf
  • 968
  • 1
  • 11
  • 33
20
votes
4 answers

strstr faster than algorithms?

I have a file that's 21056 bytes. I've written a program in C that reads the entire file into a buffer, and then uses multiple search algorithms to search the file for a token that's 82 chars. I've used all the implementations of the algorithms from…
Josh
  • 6,046
  • 11
  • 52
  • 83
16
votes
2 answers

Why is this call to strstr not returning false?

I've stumbled upon an issue with strstr in an old legacy codebase. There's lot of code, but basically the test case would come down to this: $value = 2660; $link =…
laurent
  • 88,262
  • 77
  • 290
  • 428
13
votes
5 answers

Optimized version of strstr (search has constant length)

My C program had a lot of strstr function calls. The standard library strstr is already fast but in my case the search string has always length of 5 characters. I replaced it with a special version to gain some speed: int strstr5(const char *cs,…
armakuni
  • 165
  • 1
  • 7
13
votes
3 answers

A pure bytes version of strstr?

Is there a version of strstr that works over a fixed length of memory that may include null characters? I could phrase my question like this: strncpy is to memcpy as strstr is to ?
brian
  • 3,344
  • 2
  • 26
  • 35
12
votes
3 answers

Implementing strnstr

I am trying to implement a strnstr function into C (strstr but it checks the length), for some reason it doesn't work (output is always no): #include char *searchingFor = "stackdummy"; char *in = "la da\ndoo a da\nnow here comes the…
Jimmay
  • 959
  • 3
  • 13
  • 27
12
votes
5 answers

how to check if '.'[dot] is present in string or not ? strstr is not working in php

I am trying to see if . [dot] is present in string or not. I have tried strstr but it returns false. here is my code :- I want to…
UserHex
  • 143
  • 1
  • 1
  • 10
10
votes
6 answers

JavaScript or jQuery equivalent of PHP's strstr() function

Is there a function in jQuery or JavaScript that does the same as strstr() in PHP? I have an AJAX response that should be 1,2,3,12,13,23 or 123. I want to check if 1 exists, then if 2 exists then if 3 exists.
medk
  • 9,233
  • 18
  • 57
  • 79
10
votes
6 answers

In C find position of substring in a string

Here is a program to accept a: Sentence from a user. Word from a user. How do I find the position of the word entered in the sentence? #include #include #include int main() { char sntnc[50], word[50],…
Mugambo
  • 101
  • 1
  • 1
  • 3
9
votes
4 answers

fuzzy DISTINCT Values

I have a database of real estate listings and need to return a list of neighborhoods. Right now I am using mysql DISTINCT which returns all of the distinct values. My probelm is that there is a lot of neighborhoods that have similar names: example:…
user982853
  • 2,470
  • 14
  • 55
  • 82
7
votes
3 answers

Using strstr inside a switch php

I just cannot think of the code. I have waay too many if statments which I want to change to be a switch statement, but I cannot find of the logic. At the moment I have: if(strstr($var,'texttosearch')) echo 'string contains…
William Jones
  • 75
  • 1
  • 3
7
votes
2 answers

How can I search for substring in a buffer that contains null?

Using C, I need to find a substring inside a buffer that may contain nulls. haystack = "Some text\0\0\0\0 that has embedded nulls". needle = "has embedded"r I need to return the start of the substring, or null, similat to…
crafter
  • 6,246
  • 1
  • 34
  • 46
7
votes
2 answers

What is behavior of NULL parameters to strstr?

What is the behavior when a NULL is passed as the parameters in strstr? Given: char * p = NULL; char * s = NULL; Case 1: strstr(p, "Hello"); Case 2: strstr("With my dog", p); Case 3: strstr(p, s); My understanding is that the behavior is…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
7
votes
1 answer

Performance std::strstr vs. std::string::find

Possible Duplicate: C++ string::find complexity Recently I noticed that the function std::string::find is an order of magnitude slower than the function std::strstr - in my environment with GCC 4.7 on Linux. The performance difference depends on…
nosid
  • 48,932
  • 13
  • 112
  • 139
1
2 3
24 25