string是C++、java、VB等编程语言中的字符串,用双引号引起来的几个字符,如"Abc","一天".字符串是一个特殊的对象,属于引用类型。 在java、C#中,String类对象创建后,字符串一旦初始化就不能更改,因为string类中所有字符串都是常量,数据是无法更改,由于string对象的不可变,所以可以共享。对String类的任何改变,都是返回一个新的String类对象。 C++标准库中string类以类型的形式对字符串进行封装,且包含了字符序列的处理操作

字符串数据类型,可包含单一字元或字符串的变数型态。需要注意的是在NoahWeb中要指定字符串给字符串变量,要在头尾加上单引号 (例如: '中国')。

中文名

字符串

外文名

String

所属领域

计算机

属 性

编程语言

注意

String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象。这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似。

  1. string s1,s2;
  2. s1="abc";
  3. s2=s1;
  4. s2="def";

//这样操作之后s1是"abc",s2是"def".

  1. string a="hello,world!";
  2. string b="hello,world!";
  3. string c="hello!";
  4. string a="hello,world!";
  5. string b="hello,world!";
  6. string c="hello!";

a 和 b 是不是指向同一个地址?其实很简单,跟下这些字符串的内存地址就好了。

  1. string a="hello,world!";
  2. 00000042moveax,dwordptrds:[02A62208h]
  3. 00000048movdwordptr[ebp-44h],eax

  1. string b="hello,world!";

  1. 0000004bmoveax,dwordptrds:[02A62208h]
  2. 00000051movdwordptr[ebp-48h],eax

  1. string c="hello!";

  1. 00000054moveax,dwordptrds:[02A756F8h]
  2. 0000005amovdwordptr[ebp-4Ch],eax

a的地址指向02A62208h,b的地址也是02A62208h,这说明创建b的时候,.net机制肯定是先去查找内存中是否有这个字符串的内存地址,如果有则指向,没有才创建。

字符类型

字符串数据类型,可包含单一字元或字符串的变数型态。需要注意的是在NoahWeb中要指定字符串给字符串变量,要在头尾加上单引号 (例如: '中国')。

可以使用“ADD”运算符将多个字符进行连接运算。

表现层示例

  1. <!--NoahValueValueName="'NoahWeb'"-->

示例输出

  1. NoahWeb

示例说明

输出一个字符。

逻辑层示例

  1. <SetVarName="actiondesc"Value="'编辑内容'"/>

示例说明

设置一个变量名为actiondesc的局部变量的内容为字符"编辑内容"

表现层示例

  1. <!--NoahValueValueName="'NoahWeb'ADD'1.1'"-->

示例输出

  1. NoahWeb1.1

示例说明

将两个字符串相加后输出。

逻辑层示例

  1. <SetVarName="actiondesc"Value="'编辑内容'ADD'动作'"/>

示例说明

设置一个变量名为actiondesc的局部变量的内容为字符"编辑内容动作"

作用

表示文本,即一系列 Unicode 字符。

命名空间

System

程序集

mscorlib(在 mscorlib.dll 中)

语法

  • Visual Basic(声明)
  1. <SerializableAttribute>_
  2. <ComVisibleAttribute(True)>_
  3. PublicNotInheritableClassString
  4. ImplementsIComparable,ICloneable,IConvertible,IComparable(OfString),_
  5. IEnumerable(OfString),IEnumerable,IEquatable(OfString)
  • Visual Basic (用法)

C#

  1. [SerializableAttribute]
  2. [ComVisibleAttribute(true)]
  3. publicsealedclassString:IComparable,ICloneable,IConvertible,
  4. IComparable<string>,IEnumerable<string>,IEnumerable,
  5. IEquatable<string>

C++

  1. [SerializableAttribute]
  2. [ComVisibleAttribute(true)]
  3. publicrefclassStringsealed:IComparable,ICloneable,IConvertible,
  4. IComparable<String^>,IEnumerable<String^>,IEnumerable,
  5. IEquatable<String^>

J#

  1. /**@attributeSerializableAttribute()*/
  2. /**@attributeComVisibleAttribute(true)*/
  3. publicfinalclassStringimplementsIComparable,ICloneable,
  4. IConvertible,IComparable<String>,IEnumerable<String>,
  5. IEnumerable,IEquatable<String>

JScript

  1. SerializableAttribute
  2. ComVisibleAttribute(true)
  3. publicfinalclassStringimplementsIComparable,ICloneable,
  4. IConvertible,IComparable<String>,IEnumerable<String>,
  5. IEnumerable,IEquatable<String>

XAML

不适用。

C++中

C++中的string类

MFC中的CString类使用起来非常的方便好用,但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯定的。也许有人会说,即使不用MFC框架,也可以想办法使用MFC中的API,具体的操作方法在本文最后给出操作方法。

其实,可能很多人很可能会忽略掉标准C++中string类的使用。标准C++中提供的string类得功能也是非常强大的,一般都能满足我们开发项目时使用。现将具体用法的一部分展示如下:

包含

要想使用标准C++中string类,必须要包含如下内容:

  1. #include <string>// <cstring>与<string.h>也可以
  2. using namespace std;//  using std::string;  using std::wstring;也可以

下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。

用法

string和wstring的用法是一样的,以下只用string作介绍。

  • string类的构造函数:

string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化

此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 。

  • string类的字符操作:

const char &operator[](int n)const; const char &at(int n)const; char &operator[](int n); char &at(int n);

operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

const char *data()const;//返回一个非null终止的c字符数组 const char *c_str()const;//返回一个以null终止的c字符串

int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

完整特性

详细讲解请看cplusplus:

  • string的特性描述:
  1. int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
  2. int max_size()const; //返回string对象中可存放的最大字符串的长度
  3. int size()const; //返回当前字符串的大小
  4. int length()const; //返回当前字符串的长度 
  5. bool empty()const; //当前字符串是否为空 
  6. void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分
  • string类的输入输出操作:

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。 函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符' '分开。

  • string的赋值:
  1. string &operator=(const string &s);//把字符串s赋给当前字符串
  2. string &assign(const char *s);//用c类型字符串s赋值
  3. string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
  4. string &assign(const string &s);//把字符串s赋给当前字符串
  5. string &assign(int n,char c);//用n个字符c赋值给当前字符串
  6. string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符
  7. string &assign(const_iterator first,const_itertor last); //把first和last迭代器之间的部分赋给字符串
  • string的连接:
  • string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
  • string &append(const char *s); //把c类型字符串s连接到当前字符串结尾
  • string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
  • string &append(const string &s); //同operator+=()
  • string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
  • string &append(int n,char c); //在当前字符串结尾添加n个字符c
  • string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
  • string的比较:
  1. bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等 运算符">","<",">=","<=","!="均被重载用于字符串的比较;
  2. int compare(const string &s) const;//比较当前字符串和s的大小
  3. int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
  4. int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中//pos2开始的n2个字符组成的字符串的大小
  5. int compare(const char *s) const;
  6. int compare(int pos, int n,const char *s) const;
  7. int compare(int pos, int n,const char *s, int pos2) const;//compare函数在大于(>)时返回1,小于(<)时返回-1,等于(==)时返回0
  • string的子串:
  1. string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
  • string的交换:
  1. void swap(string &s2); //交换当前字符串与s2的值
  • string类的查找函数:
  1. int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
  2. int find(const char *s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
  3. int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
  4. int find(const string &s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值
  5. int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
  6. int rfind(const char *s, int pos = npos) const;
  7. int rfind(const char *s, int pos, int n = npos) const;
  8. int rfind(const string &s,int pos = npos) const; //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
  9. int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
  10. int find_first_of(const char *s, int pos = 0) const;
  11. int find_first_of(const char *s, int pos, int n) const;
  12. int find_first_of(const string &s,int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
  13. int find_first_not_of(char c, int pos = 0) const;
  14. int find_first_not_of(const char *s, int pos = 0) const;
  15. int find_first_not_of(const char *s, int pos,int n) const;
  16. int find_first_not_of(const string &s,int pos = 0) const; //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
  17. int find_last_of(char c, int pos = npos) const;
  18. int find_last_of(const char *s, int pos = npos) const;
  19. int find_last_of(const char *s, int pos, int n = npos) const;
  20. int find_last_of(const string &s,int pos = npos) const;
  21. int find_last_not_of(char c, int pos = npos) const;
  22. int find_last_not_of(const char *s, int pos = npos) const;
  23. int find_last_not_of(const char *s, int pos, int n) const;
  24. int find_last_not_of(const string &s,int pos = npos) const; //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找。
  • string类的替换函数:
  1. string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
  2. string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
  3. string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
  4. string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
  5. string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
  6. string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
  7. string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符。
  8. string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
  9. string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
  10. string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串。
  • string类的插入函数:
  1. string &insert(int p0, const char *s);
  2. string &insert(int p0, const char *s, int n);
  3. string &insert(int p0,const string &s);
  4. string &insert(int p0,const string &s, int pos, int n); //前4个函数在p0位置插入字符串s中pos开始的前n个字符
  5. string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
  6. iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
  7. void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
  8. void insert(iterator it, int n, char c);//在it处插入n个字符c
  • string类的删除函数
  1. iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置。
  2. iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置。
  3. string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串。
  • string类的迭代器处理:

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。

用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

  1. const_iterator begin()const; iterator begin(); //返回string的起始位置
  2.  const_iterator end()const; iterator end(); //返回string的最后一个字符后面的位置
  3.  const_iterator rbegin()const; iterator rbegin(); //返回string的最后一个字符的位置
  4.  const_iterator rend()const; iterator rend(); //返回string第一个字符位置的前面

rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator或string::const_reverse_iterator实现

  • 字符串流处理:
  • 通过定义ostringstream和istringstream变量实现,在#include <sstream>头文件中。

例如:

  1. string input("hello,this is a test"); 
  2.  istringstream is(input); 
  3.  string s1,s2,s3,s4;
  4. is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" 
  5.  ostringstream os; 
  6.  os<<s1<<s2<<s3<<s4; 
  7.  cout<<os.str();

以上就是对C++ string类的一个简要介绍。用的好的话它所具有的功能不会比MFC中的CString类逊色多少。

MFC CString

最后要介绍如何在Win32 应用程序中引用MFC中的部分类,例如CString。

1.在工程目录下右键选择

"Properties”--->"Configuration Properties”--->“General”--->"Use of MFC"--->"Use MFC in a Static Library",

默认的是:"Use Standard Windows Libraries",如下图1所示:

2.在你所用的所有头文件之前包含#include <afxwin.h>。

例如:可以在stdafx.h文件的最前面包含#include <afxwin.h>头文件,这样在你的源代码中就可以使用CString类了,不过这样也有一个缺点,就是编译出来的程序要比原来的大很多。

语系沿革备注

String 对象称为不可变的(只读),因为一旦创建了该对象,就不能修改该对象的值。看来似乎修改了 String 对象的方法实际上是返回一个包含修改内容的新 String 对象。如果需要修改字符串对象的实际内容,请使用 System.Text.StringBuilder 类。

字符串中的每个 Unicode 字符都是由 Unicode 标量值定义的,Unicode 标量值也称为 Unicode 码位或者 Unicode 字符的序号(数字)值。每个码位都是使用 UTF-16 编码进行编码的,编码的每个元素的数值都用一个 Char 对象表示。

一个 Char 对象通常表示一个码位,即:Char 的数值等于该码位。但是,一个码位可能需要多个编码元素。例如,Unicode 辅助码位(代理项对)使用两个 Char 对象来编码。

索引

索引是 Char 对象在 String 中的位置,而不是 Unicode 字符的位置。索引是从零开始、从字符串的起始位置(其索引为零)计起的非负数字。连续的索引值可能并不与连续的 Unicode 字符相对应,这是因为一个 Unicode 字符可能会编码为多个 Char 对象。若要使用每个 Unicode 字符而不是每个 Char 对象,请使用 System.Globalization.StringInfo 类。

序号运算

String 类的成员对 String 对象执行序号运算或语义运算。序号运算是对每个 Char 对象的数值执行的。语义运算则对考虑了特定于区域性的大小写、排序、格式化和语法分析规则的 String 的值执行。语义运算在显式声明的区域性或者隐式当前区域性的上下文中执行。有关当前区域性的更多信息,请参见 CultureInfo.CurrentCulture 主题。

大小写规则决定如何更改 Unicode 字符的大小写,例如,从小写变为大写。

格式化规则决定如何将值转换为它的字符串表示形式,而语法分析规则则确定如何将字符串表示形式转换为值。

排序规则确定 Unicode 字符的字母顺序,以及两个字符串如何互相比较。例如,Compare 方法执行语义比较,而 CompareOrdinal 方法执行序号比较。因此,如果当前的区域性为美国英语,则 Compare 方法认为“a”小于“A”,而 CompareOrdinal 方法会认为“a”大于“A”。

.NET Framework 支持单词、字符串和序号排序规则。单词排序会执行区分区域性的字符串比较,在这种比较中,某些非字母数字 Unicode 字符可能会具有特殊的权重。例如,连字符(“-”)的权重非常小,因此“coop”和“co-op”在排序列表中是紧挨着出现的。字符串排序与单词排序相似,只是所有非字母数字符号均排在所有字母数字 Unicode 字符前面,没有特例。

区分运算

区分区域性的比较是显式或隐式使用 CultureInfo 对象的任何比较,包括由 CultureInfo.InvariantCulture 属性指定的固定区域性。当前隐式区域性由 Thread.CurrentCulture 属性指定。

序号排序基于字符串中每个 Char 对象的数值对字符串进行比较。序号比较自动区分大小写,因为字符的小写和大写版本有着不同的码位。但是,如果大小写在应用程序中并不重要,则可以指定忽略大小写的序号比较。这相当于使用固定区域性将字符串转换为大写,然后对结果执行序号比较。

区分区域性的比较通常适用于排序,而序号比较则不适合。序号比较通常适用于确定两个字符串是否相等(即,确定标识),而区分区域性的比较则不适用。

比较和搜索方法的“备注”指定方法是区分大小写、区分区域性还是两者区分。根据定义,任何字符串(包括空字符串 (""))的比较结果都大于空引用;两个空引用的比较结果为相等。

某些 Unicode 字符具有多个等效的二进制表示形式,这些表示形式中包含几组组合的和/或复合的 Unicode 字符。Unicode 标准定义了一个称为规范化的过程,此过程将一个字符的任何一种等价二进制表示形式转换为统一的二进制表示形式。可使用多种遵循不同规则的算法执行规范化,这些算法也称为范式。.NET Framework 当前支持范式 C、D、KC 和 KD。通常用序号比较来评估一对规范化的字符串。

安全注意事项

如果应用程序进行有关符号标识符(如文件名或命名管道)或持久数据(如 XML 文件中基于文本的数据)的安全决策,则该操作应该使用序号比较而不是区分区域性的比较。这是因为根据起作用的区域性的不同,区分区域性的比较可产生不同的结果,而序号比较则仅依赖于所比较字符的二进制值。

功能

String 类提供的成员执行以下操作:比较 String 对象;返回 String 对象内字符或字符串的索引;复制 String 对象的值;分隔字符串或组合字符串;修改字符串的值;将数字、日期和时间或枚举值的格式设置为字符串;对字符串进行规范化。

使用 Compare、CompareOrdinal、CompareTo、Equals、EndsWith 和 StartsWith 方法进行比较。

使用 IndexOf、IndexOfAny、LastIndexOf 和 LastIndexOfAny 方法可获取字符串中子字符串或 Unicode 字符的索引。

使用 Copy 和 CopyTo 可将字符串或子字符串复制到另一个字符串或 Char 类型的数组。

使用 Substring 和 Split 方法可通过原始字符串的组成部分创建一个或多个新字符串;使用 Concat 和 Join 方法可通过一个或多个子字符串创建新字符串。

使用 Insert、Replace、Remove、PadLeft、PadRight、Trim、TrimEnd 和 TrimStart 可修改字符串的全部或部分。

使用 ToLower、ToLowerInvariant、ToUpper 和 ToUpperInvariant 方法可更改字符串中 Unicode 字符的大小写。

使用 Length 属性可获取字符串中 Char 对象的数量;使用 Chars 属性可访问字符串中实际的 Char 对象。

使用 IsNormalized 方法可测试某个字符串是否已规范化为特定的范式。使用 Normalize 方法可创建规范化为特定范式的字符串。

获取字符

string 类型通过下标操作符([ ])来访问 string 对象中的单个字符。下标操作符需要取一个 size_type 类型的值,来标明要访问字符的位置。这个下标中的值通常被称为“下标”或“索引”(index).

可用下标操作符分别取出 string 对象的每个字符,分行输出:

string str("some string");for (string::size_type ix = 0; ix != str.size(); ++ix)cout << str[ix] << endl;

每次通过循环,就从 str 对象中读取下一个字符,输出该字符并换行。

实现的接口

String 类分别用于实现 IComparable、ICloneable、IConvertible、IEnumerable 和 IComparable 接口。使用 Convert 类进行转换,而不是使用此类型的 IConvertible 显式接口成员实现。

继承层次结构

System.Object

System.String

版本信息

.NET Framework

受以下版本支持:3.0、2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

XNA Framework

受以下版本支持:1.0

函式库

▪ C 标准函式库

▪ glibc

▪ Dietlibc

▪ uClibc

▪ Newlib

▪ EGLIBC

▪ Bionic

特性

▪ String

▪ Syntax

▪ Preprocessor

▪ Variable types and declarations

▪ Functions

延伸的相关编程语言

▪ C++

▪ Objective-C

▪ D

▪ C#

C 与其他的编程语言

▪ Compatibility

▪ 运算子

▪ Comparison of Pascal and C

▪ C to Java byte-code compiler

编译器

▪ Borland Turbo C

▪ Clang

▪ GCC

▪ LCC

▪ Pelles C

▪ PCC

▪ TCC

▪ Visual C++

▪ C++/CLI

▪ C++/CX

▪ Watcom C/C++ compiler