Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

strstr(3) [centos man page]

STRSTR(3)						     Linux Programmer's Manual							 STRSTR(3)

NAME
strstr, strcasestr - locate a substring SYNOPSIS
#include <string.h> char *strstr(const char *haystack, const char *needle); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <string.h> char *strcasestr(const char *haystack, const char *needle); DESCRIPTION
The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('') are not compared. The strcasestr() function is like strstr(), but ignores the case of both arguments. RETURN VALUE
These functions return a pointer to the beginning of the substring, or NULL if the substring is not found. CONFORMING TO
The strstr() function conforms to C89 and C99. The strcasestr() function is a nonstandard extension. BUGS
Early versions of Linux libc (like 4.5.26) would not allow an empty needle argument for strstr(). Later versions (like 4.6.27) work cor- rectly, and return haystack when needle is empty. SEE ALSO
index(3), memchr(3), rindex(3), strcasecmp(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strtok(3), wcsstr(3) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2011-09-28 STRSTR(3)

Check Out this Related Man Page

STRSTR(3)						   BSD Library Functions Manual 						 STRSTR(3)

NAME
strcasestr, strcasestr_l, strnstr, strstr -- locate a substring in a string LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * strcasestr(const char *s1, const char *s2); char * strnstr(const char *s1, const char *s2, size_t n); char * strstr(const char *s1, const char *s2); #include <string.h> #include <xlocale.h> char * strcasestr_l(const char *s1, const char *s2, locale_t loc); DESCRIPTION
The strstr() function locates the first occurrence of the null-terminated string s2 in the null-terminated string s1. The strcasestr() function is similar to strstr(), but ignores the case of both strings. The strnstr() function locates the first occurrence of the null-terminated string s2 in the string s1, where not more than n characters are searched. Characters that appear after a '' character are not searched. Since the strnstr() function is a FreeBSD specific API, it should only be used when portability is not a concern. While the strcasestr() function uses the current locale, the strcasestr_l() function may be passed a locale directly. See xlocale(3) for more information. RETURN VALUES
If s2 is an empty string, s1 is returned; if s2 occurs nowhere in s1, NULL is returned; otherwise a pointer to the first character of the first occurrence of s2 is returned. EXAMPLES
The following sets the pointer ptr to the "Bar Baz" portion of largestring: const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strstr(largestring, smallstring); The following sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched: const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strnstr(largestring, smallstring, 4); SEE ALSO
memchr(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strtok(3), wcsstr(3), xlocale(3) STANDARDS
The strstr() function conforms to ISO/IEC 9899:1990 (``ISO C90''). BSD
October 11, 2001 BSD
Man Page

14 More Discussions You Might Find Interesting

1. Programming

can i get a substring from a string?

for example, the string a is "abcdefg", can i get a substring "bcd" (from ato a) from string a? thank you (4 Replies)
Discussion started by: dell9
4 Replies

2. Shell Programming and Scripting

Removing substring from a string

Hi. I want to remove a substring from a string. If I use the "tr" command, it doesn't seem to to what I want it to: echo './somestring.dat' | tr -d './' results in somestringdat I want it to remove only occurances of the string './', rather than the individual character. The result... (1 Reply)
Discussion started by: dmilks
1 Replies

3. UNIX for Dummies Questions & Answers

How to get the substring from the string

Hi All, Can anybody help me to get the substring from the given string. (3 Replies)
Discussion started by: Anshu
3 Replies

4. Shell Programming and Scripting

using sed command...

Hello, I need to locate the last occurrence of a substring in a given string.NULL is returned if the substring is not found in the stringhow do i do this with a sed command..? (0 Replies)
Discussion started by: sharsin2001
0 Replies

5. Shell Programming and Scripting

help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is... (1 Reply)
Discussion started by: pankajd
1 Replies

6. UNIX for Dummies Questions & Answers

Substring not working if more than 1 space

I have a string which is something like this.. abcd efghijkl when I use this using substring() function 2 spaces are bein considered as one starngely. Is there any thing I am doing wroing the input file maintest has the string specified and the code is below while read... (3 Replies)
Discussion started by: prassu
3 Replies

7. UNIX for Dummies Questions & Answers

Verify if substring is found in string

Hi, I'm new to HP-UNIX programming(previously using C++). I am looking for an IF statement which would return TRUE if a substring is found in a string. For example, if 'header' is found in 'header_cp', the IF statement will result to true. Would appreciate any help... (1 Reply)
Discussion started by: miashaykoh
1 Replies

8. Shell Programming and Scripting

Using sed to get a substring

Hi, I have looked all over for this. I am attempting to get a the substring of a string using sed since it seemed the best solution for this. For example my string is: "zzz foo to you and bar123 or foo" I would like to extract the text between "and" and "or" (it could be anything, but... (2 Replies)
Discussion started by: CentaurAtlas
2 Replies

9. Shell Programming and Scripting

Searching a substring from a string in UNIX

Hi all. I need help with a command to locate a substring from a string. My problem is I am passing a direcotry name as a command line argument and I need to ensure that user should not pass certain directories. E.g ther are Directories under ==> /opt/projects/* which should not be passed... (3 Replies)
Discussion started by: Veenak15
3 Replies

10. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

11. Shell Programming and Scripting

how to remove this substring?

Hello, I need to remove a substring from a string in a ksh script, the string is like this: LOCATION=+DATADG1/CMSPRD1/datafile/system.235.456721 or LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf I need to strip the last file name from that path, and get: +DATADG1/CMSPRD1/datafile or,... (8 Replies)
Discussion started by: seafan
8 Replies

12. UNIX for Dummies Questions & Answers

If statement for substring within string (korn)

Hi all, Just looking for a simple if statement that searches for a substring within a varaible, and then performs some function. Problem is that I need it to work in Korn shell $var = *string* does not work in Korn i="xxxxxx00.00yyyyy.zzzzz" want to find 00.00 (2 Replies)
Discussion started by: jgrosecl
2 Replies

13. UNIX for Dummies Questions & Answers

get substring from string variable

Hi Dears, How to use variable in string location of expression ${string:index:length} to get substring from string? I encounter error "bad substitution" when I use expression ${$var:0:5} to get first 5 characters from $var. Could you please help me out of this? Thanks! (2 Replies)
Discussion started by: crest.boy
2 Replies

14. Programming

strstr() in Solaris System

Can any one help with the function strstr() in Solaris unix. The equivalent function of strstr() in Solaris Unix. This function is working in HP Unix but not in Solaris Unix. (16 Replies)
Discussion started by: jhon1257
16 Replies