December 7,2005

c++ some mothod

Parameters.

string1
Null-terminated string to search.
string2
Null-terminated string containing the substring to search for.
Return Value.
A pointer to the first occurrence of string2 in string1.
If string2 is not found in string1 the function returns NULL.

const char * strstr ( const char * string1, const char * string2 );
char * strstr ( char * string1, const char * string2 );

Both have the same behavior as the original declaration.
Example.

/* strstr example */
#include
#include

int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",5);
puts (str);
return 0;
}


Code:
#include "string"



and


Code:
string test("This is a test");



...as a test string, then

1. Substring


Code:
cout << "Substring: " << test.substr(8) << endl;



This will display "a test". Substring may also be used as:


Code:
cout << "Substring: " << test.substr(8, 1) << endl;



This will display "a".

2. Contains:


Code:
size_t pos = test.find("is");
if(pos == string::npos)
{
cout << "String does not contain substring 'is'." << endl;
}
else
{
cout << "String 'is' found at position " << pos << endl;
}



3. Compare:


Code:
if(test.compare("This is a test") == 0)
{
cout << "The string matches." << endl;
}
else
{
cout << "The string does not match." << endl;
}



See http://www.cppreference.com/cppstring/

Posted by yam_javanull at 樂多Roodo! │18:33 │回應(0)引用(0)C++
樂多分類:網路/3C 共同主題:程式設計 工具:編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/835052