# string function

**URL:** <https://community.unix.com/t/string-function/145384>\
**Category:** Programming\
**Created:** [February 11, 2003, 4:27pm UTC](https://community.unix.com/t/string-function/145384 "2003-02-11T16:27:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalburger](https://community.unix.com/letter_avatar/jalburger/32/5_5575768a8748004e209b776fc1b2916d.png) [@jalburger](https://community.unix.com/u/jalburger)\
**Post date:** [February 11, 2003, 4:27pm UTC](https://community.unix.com/t/string-function/145384/1 "2003-02-11T16:27:49Z")

</div>

I have a question concerning string functions. I have not been able to locate a function that does what I want, so I fugured I'd ask before I wrote on myself.  
Is there a function to which I can pass 2 strings (character string a and character string b) and have it tell me if string b appears anywhere in string a?

ie, can it determine if "test" exists in the string "this is a test"?

---

<div class="post-metadata">

**Author:** ![TioTony](https://community.unix.com/user_avatar/community.unix.com/tiotony/32/36_2.png) [@TioTony](https://community.unix.com/u/TioTony)\
**Post date:** [February 11, 2003, 9:43pm UTC](https://community.unix.com/t/string-function/145384/2 "2003-02-11T21:43:25Z")

</div>

grep will do what you want. If you just want a "yes it exists" or "no it doesn't" answer, just check $? after you have done the grep. This example might help :

stringa=test  
stringb="this is a test"  
echo $stringb | grep $stringa \>\> /dev/null  
if [$? = 0]  
then  
echo "Match Found"  
else  
echo "No Match Found"  
fi

TioTony

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/5_5575768a8748004e209b776fc1b2916d.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [February 12, 2003, 1:23am UTC](https://community.unix.com/t/string-function/145384/3 "2003-02-12T01:23:03Z")

</div>

I assume the original poster choose to post on the C programming forum, he/she may want an answer with the C programming language, not shell script.

Try the strstr() function.

From the man page of string.h functions:

#include \<string.h\>

char \*strstr(const char \*s1, const char \*s2);

strstr()  
The strstr() function locates the first occurrence of the  
string s2 (excluding the terminating null character) in  
string s1 and returns a pointer to the located string, or a  
null pointer if the string is not found. If s2 points to a  
string with zero length (that is, the string ""), the func-  
tion returns s1.

---

<div class="post-metadata">

**Author:** ![S.P.Prasad](https://community.unix.com/letter_avatar/s.p.prasad/32/5_5575768a8748004e209b776fc1b2916d.png) [@S.P.Prasad](https://community.unix.com/u/S.P.Prasad)\
**Post date:** [February 12, 2003, 6:38am UTC](https://community.unix.com/t/string-function/145384/4 "2003-02-12T06:38:53Z")

</div>

int CheckSubStr( char \* MainStr , char \* SearchStr )  
{  
int StartIndex,Index, Counter ;  
for (StartIndex=strlen(MainStr)-strlen( SearchStr ) ; StartIndex \>= 0 ; StartIndex --)  
for ( Index = 0 , Counter = StartIndex ; Index \< strlen ( SearchStr ) && MainStr[Counter++]==SearchStr[Index++]; )  
if ( SearchStr [Index] == '\0' )  
return 0 ; /\* Matched _/  
return 1 ; /_ Not Matched \*/  
}

---

<div class="post-metadata">

**Author:** ![jalburger](https://community.unix.com/letter_avatar/jalburger/32/5_5575768a8748004e209b776fc1b2916d.png) [@jalburger](https://community.unix.com/u/jalburger)\
**Post date:** [February 12, 2003, 8:11am UTC](https://community.unix.com/t/string-function/145384/5 "2003-02-12T08:11:40Z")

</div>

Yes, I was looking for a C function. Thanks, cbkihong. It looks like strstr() was exactly what I was looking for. 😃

---

<div class="post-metadata">

**Author:** ![TioTony](https://community.unix.com/user_avatar/community.unix.com/tiotony/32/36_2.png) [@TioTony](https://community.unix.com/u/TioTony)\
**Post date:** [February 12, 2003, 9:40pm UTC](https://community.unix.com/t/string-function/145384/6 "2003-02-12T21:40:49Z")

</div>

Sorry, I had Shell scripting on the brain and didn't notice you posted in the C forum.

---

<div class="post-metadata">

**Author:** ![SpanishPassion](https://community.unix.com/letter_avatar/spanishpassion/32/5_5575768a8748004e209b776fc1b2916d.png) [@SpanishPassion](https://community.unix.com/u/SpanishPassion)\
**Post date:** [February 26, 2006, 6:27pm UTC](https://community.unix.com/t/string-function/145384/7 "2006-02-26T18:27:55Z")

</div>

Actually I am after a C shell scripting solution myself.

Can you use the C functions of Unix, especially the string functions, within a C shell? How?

Thanks

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [February 27, 2006, 10:17am UTC](https://community.unix.com/t/string-function/145384/8 "2006-02-27T10:17:35Z")

</div>

No you cannot use C functions in the shell, unless you write C code to do it and then call it from the shell.

try man awk

awk has functions like substr, index. Plus printing variables with a space between them conctenates.

Most shells have good ways of handling strings, but most times they are not eexplicit functions like you have in either awk or C.
