2007年4月10日星期二

C++ string class

C++的string类里提供了很多的函数,包含字符串的查找,替换,等等操作。尽管它并没有比如javascript之类的字符操作函数多样,但是各种功能的字符查找都可以通过这些基本的函数实现。
javascript的字符串对象string object的相关内容见
http://www.w3schools.com/jsref/jsref_obj_string.asp
XPATH的字符串对象string object可以参考:
http://www.w3.org/TR/xpath#section-String-Functions
http://www.w3schools.com/xpath/xpath_functions.asp#string

http://www.w3schools.com 是个好网站




参考可以看
http://www.cppreference.com/cppstring/index.html
每一个条目的链接页都有示例。

C++ Strings

Display all entries for C++ Strings on one page, or view entries individually:

String constructors create strings from arrays of characters and other strings
String operators concatenate strings, assign strings, use strings for I/O, compare strings
append append characters and strings onto a string
assign give a string values from strings of characters and other C++ strings
at returns an element at a specific location
begin returns an iterator to the beginning of the string
c_str returns a standard C character array version of the string
capacity returns the number of elements that the string can hold
clear removes all elements from the string
compare compares two strings
copy copies characters from a string into an array
data returns a pointer to the first character of a string
empty true if the string has no elements
end returns an iterator just past the last element of a string
erase removes elements from a string
find find characters in the string
find_first_not_of find first absence of characters
find_first_of find first occurrence of characters
find_last_not_of find last absence of characters
find_last_of find last occurrence of characters
getline read data from an I/O stream into a string
insert insert characters into a string
length returns the length of the string
max_size returns the maximum number of elements that the string can hold
push_back add an element to the end of the string
rbegin returns a reverse_iterator to the end of the string
rend returns a reverse_iterator to the beginning of the string
replace replace characters in the string
reserve sets the minimum capacity of the string
resize change the size of the string
rfind find the last occurrence of a substring
size returns the number of items in the string
substr returns a certain substring
swap swap the contents of this string with another

这里还有一个列表,来自
http://www.bgsu.edu/departments/compsci/docs/string.html

C++ String Class

The C++ Standard Template Library (STL) contains a string class that is used in several computer science classes. In order to use the string class you should include the following statements:
#include
using std::string;

The following examples assume these declarations and initial values for each:

string s = "abc def abc";
string s2 = "abcde uvwxyz";
char c;
char ch[] = "aba daba do";
char *cp = ch;
unsigned int i;

Stream input cin >> s; Changes the value of s to the value read in. The value stops at whitespace.
Stream output cout <<> Writes the string to the specified output stream.
Line input getline(cin, s); Reads everything up to the next newline character and puts the result into the specified string variable.
Assignment s = s2;s = "abc";
s = ch; s = cp;
A string literal or a string variable or a character array can be assigned to a string variable. The last two assignments have the same effect.
Subscript s[1] = 'c';
c = s[1];
Changes s to equal "acc def abc"
Sets c to 'b'. The subscript operator returns a char value, not a string value.
Length i = s.length();
i = s.size();
Either example sets i to the current length of the string s
Empty? if(s.empty()) i++;
if(s =
= "") i++;
Both examples add 1 to i if string s is now empty
Relational operators if (s <> Uses ASCII code to determine which string is smaller. Here the condition is true because a space comes before letter d
Concatenation

s2 = s2 + "x";
s2 += "x";

Both examples add x to the end of s2
Substring

s = s2.substr(1,4);
s = s2.substr(1,50);

The first example starts in position 1 of s2 and takes 4 characters, setting s to "bcde". In the second example, s is set to "bcde uvwxyz". If the length specified is longer than the remaining number of characters, the rest of the string is used. The first position in a string is position 0.
Substring replace s.replace(4,3,"x"); Replaces the three characters of s beginning in position 4 with the character x. Variable s is set to "abc x abc".
Substring removal s.erase(4,5);
s.erase(4);
Removes the five characters starting in position 4 of s. The new value of s is "abc bc".
Remove from position 4 to end of string. The new value of s is "abc ".
Character array to string s = ch; Converts character array ch into string s.
String to character array cp = s.c_str(); Pointer cp points to a character array with the same characters as s.
Pattern matching

i = s.find("ab",4);

if(s.rfind("ab",4) != string::npos)
cout << "Yes" <<>

The first example returns the position of the substring "ab" starting the search in position 4. Sets i to 8. The find and rfind functions return the unsigned int string::npos if substring not found. The second example searches from right to left starting at position 4. Since the substring is found, the word Yes is printed.

Because of a bug in the bgunix version of the g++ compiler, writing a string variable under setw control doesn't work. You need to use the c_str member function, like this:
cout <<>


C语言的字符处理库如下
http://www.cplusplus.com/reference/clibrary/cstring/
cstring (string.h) header


C Strings

This header file defines several functions to manipulate C strings and arrays.

Functions

Copying:
memcpy Copy block of memory (function)
memmove Move block of memory (function)
strcpy Copy string (function)
strncpy Copy characters from string (function)

Concatenation:

strcat Concatenate strings (function)
strncat Append characters from string (function)

Comparison:

memcmp Compare two blocks of memory (function)
strcmp Compare two strings (function)
strcoll Compare two strings using locale (function)
strncmp Compare characters of two strings (function)
strxfrm Transform string using locale (function)

Searching:

memchr Locate character in block of memory (function)
strchr Locate first occurrence of character in string (function)
strcspn Get span until character in string (function)
strpbrk Locate character in string (function)
strrchr Locate last occurrence of character in string (function)
strspn Get span of character set in string (function)
strstr Locate substring (function)
strtok Split string into tokens (function)

Other:

memset Fill block of memory (function)
strerror Get pointer to error message string (function)
strlen Get string length (function)

Macros

NULL Null pointer (macro)

Types

size_t Unsigned integral type (type)

没有评论: