string(C++,java,VB等程式语言中的字元串)
C++、java、VB等程式语言中的字元串。 在java、C#中,String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。 String 对象是 System.Char 对象的有序集合,用于表示字元串。String 对象的值是该有序集合的内容,并且该值是不可变的
基本介绍
- 中文名:字元串
- 外文名:String
- 所属领域:计算机
- 属性:程式语言
基本信息
String就是C++、java、VB等程式语言中的字元串,用双引号引起来的几个字元.如"Abc","一天".
特别注意
String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象.这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似。
string s1,s2;s1="abc";s2=s1;s2="def";
//这样操作之后s1是"abc",s2是"def".
string a="hello,world!";string b="hello,world!";string c="hello!";string a="hello,world!";string b="hello,world!";string c="hello!";
a 和 b 是不是指向同一个地址?其实很简单,跟下这些字元串的记忆体地址就好了。
string a="hello,world!";
00000042moveax,dwordptrds:[02A62208h]00000048movdwordptr[ebp-44h],eax
string b="hello,world!";
0000004bmoveax,dwordptrds:[02A62208h]00000051movdwordptr[ebp-48h],eax
string c="hello!";
00000054moveax,dwordptrds:[02A756F8h]0000005amovdwordptr[ebp-4Ch],eax
a的地址指向02A62208h,b的地址也是02A62208h,这说明创建b的时候,.net机制肯定是先去查找记忆体中是否有这个字元串的记忆体地址,如果有则指向,没有才创建。
字元类型
概述
字元串数据类型,可包含单一字元或字元串的变数型态。需要注意的是在NoahWeb中要指定字元串给字元串变数,要在头尾加上单引号 (例如: '中国')。
可以使用“ADD”运算符将多个字元进行连线运算。
表现层示例
<!--NoahValueValueName="'NoahWeb'"-->
示例输出
NoahWeb
示例说明
输出一个字元。
逻辑层示例
<SetVarName="actiondesc"Value="'编辑内容'"/>
示例说明
设定一个变数名为actiondesc的局部变数的内容为字元"编辑内容"
表现层示例
<!--NoahValueValueName="'NoahWeb'ADD'1.1'"-->
示例输出
NoahWeb1.1
示例说明
将两个字元串相加后输出。
逻辑层示例
<SetVarName="actiondesc"Value="'编辑内容'ADD'动作'"/>
示例说明
设定一个变数名为actiondesc的局部变数的内容为字元"编辑内容动作"
类
作用:
表示文本,即一系列 Unicode 字元。
命名空间
System
程式集
mscorlib(在 mscorlib.dll 中)
语法
Visual Basic(声明)
<SerializableAttribute>_<ComVisibleAttribute(True)>_PublicNotInheritableClassStringImplementsIComparable,ICloneable,IConvertible,IComparable(OfString),_IEnumerable(OfString),IEnumerable,IEquatable(OfString)
Visual Basic (用法)
DiminstanceAsString
C#
[SerializableAttribute][ComVisibleAttribute(true)]publicsealedclassString:IComparable,ICloneable,IConvertible,IComparable<string>,IEnumerable<string>,IEnumerable,IEquatable<string>
C++
[SerializableAttribute][ComVisibleAttribute(true)]publicrefclassStringsealed:IComparable,ICloneable,IConvertible,IComparable<String^>,IEnumerable<String^>,IEnumerable,IEquatable<String^>
J#
/**@attributeSerializableAttribute()*//**@attributeComVisibleAttribute(true)*/publicfinalclassStringimplementsIComparable,ICloneable,IConvertible,IComparable<String>,IEnumerable<String>,IEnumerable,IEquatable<String>
JScript
SerializableAttributeComVisibleAttribute(true)publicfinalclassStringimplementsIComparable,ICloneable,IConvertible,IComparable<String>,IEnumerable<String>,IEnumerable,IEquatable<String>
XAML
不适用。
C++ 中
C++ 中的 string 类
MFC中的CString类使用起来非常的方便好用,但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯定的。也许有人会说,即使不用MFC框架,也可以想办法使用MFC中的API,具体的操作方法在本文最后给出操作方法。
其实,可能很多人很可能会忽略掉标準C++中string类的使用。标準C++中提供的string类得功能也是非常强大的,一般都能满足我们开发项目时使用。现将具体用法的一部分罗列如下:
包含
要想使用标準C++中string类,必须要包含如下内容:
#include <string>// <cstring>与<string.h>也可以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的特性描述:
int capacity()const; //返回当前容量(即string中不必增加记忆体即可存放的元素个数)int max_size()const; //返回string对象中可存放的最大字元串的长度int size()const; //返回当前字元串的大小int length()const; //返回当前字元串的长度bool empty()const; //当前字元串是否为空void resize(int len,char c);//把字元串当前大小置为len,并用字元c填充不足的部分
- string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。 函式getline(istream &in,string &s);用于从输入流in中读取字元串到s中,以换行符'\n'分开。
- string的赋值:
string &operator=(const string &s);//把字元串s赋给当前字元串string &assign(const char *s);//用c类型字元串s赋值string &assign(const char *s,int n);//用c字元串s开始的n个字元赋值string &assign(const string &s);//把字元串s赋给当前字元串string &assign(int n,char c);//用n个字元c赋值给当前字元串string &assign(const string &s,int start,int n);//把字元串s中从start开始的n个字元赋给当前字元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个字元cstring &append(const_iterator first,const_iterator last);//把叠代器first和last之间的部分连线到当前字元串的结尾
- string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字元串是否相等 运算符">","<",">=","<=","!="均被重载用于字元串的比较;int compare(const string &s) const;//比较当前字元串和s的大小int compare(int pos, int n,const string &s)const;//比较当前字元串从pos开始的n个字元组成的字元串与s的大小int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字元串从pos开始的n个字元组成的字元串与s中//pos2开始的n2个字元组成的字元串的大小int compare(const char *s) const;int compare(int pos, int n,const char *s) const;int compare(int pos, int n,const char *s, int pos2) const;//compare函式在大于(>)时返回1,小于(<)时返回-1,等于(==)时返回0
- string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字元组成的字元串
- string的交换:
void swap(string &s2); //交换当前字元串与s2的值
- string类的查找函式:
int find(char c, int pos = 0) const;//从pos开始查找字元c在当前字元串的位置int find(const char *s,int pos = 0) const;//从pos开始查找字元串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字元串s中前n个字元在当前串中的位置int find(const string &s,int pos = 0) const;//从pos开始查找字元串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字元c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const; //从pos开始从后向前查找字元串s中前n个字元组成的字元串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值int find_first_of(char c, int pos = 0) const;//从pos开始查找字元c第一次出现的位置int find_first_of(const char *s, int pos = 0) const;int find_first_of(const char *s, int pos, int n) const;int find_first_of(const string &s,int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字元组成的数组里的字元的位置。查找失败返回string::nposint find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_first_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const; //从当前串中查找第一个不在串s中的字元出现的位置,失败返回string::nposint find_last_of(char c, int pos = npos) const;int find_last_of(const char *s, int pos = npos) const;int find_last_of(const char *s, int pos, int n = npos) const;int find_last_of(const string &s,int pos = npos) const;int find_last_not_of(char c, int pos = npos) const;int find_last_not_of(const char *s, int pos = npos) const;int find_last_not_of(const char *s, int pos, int n) const;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类的替换函式:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字元,然后在p0处插入串sstring &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字元,然后在p0处插入字元串s的前n个字元string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字元,然后在p0处插入串sstring &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字元,然后在p0处插入串s中从pos开始的n个字元string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字元,然后在p0处插入n个字元cstring &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字元串sstring &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字元。string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串sstring &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字元cstring &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字元串。
- string类的插入函式:
string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0,const string &s);string &insert(int p0,const string &s, int pos, int n); //前4个函式在p0位置插入字元串s中pos开始的前n个字元string &insert(int p0, int n, char c);//此函式在p0处插入n个字元citerator insert(iterator it, char c);//在it处插入字元c,返回插入后叠代器的位置void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字元void insert(iterator it, int n, char c);//在it处插入n个字元c
- string类的删除函式
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字元,返回删除后叠代器的位置。iterator erase(iterator it);//删除it指向的字元,返回删除后叠代器的位置。string &erase(int pos = 0, int n = npos);//删除pos开始的n个字元,返回修改后的字元串。
- string类的叠代器处理:
string类提供了向前和向后遍历的叠代器iterator,叠代器提供了访问各个字元的语法,类似于指针操作,叠代器不检查範围。
用string::iterator或string::const_iterator声明叠代器变数,const_iterator不允许改变叠代的内容。常用叠代器函式有:
用string::iterator或string::const_iterator声明叠代器变数,const_iterator不允许改变叠代的内容。常用叠代器函式有:
const_iterator begin()const; iterator begin(); //返回string的起始位置const_iterator end()const; iterator end(); //返回string的最后一个字元后面的位置const_iterator rbegin()const; iterator rbegin(); //返回string的最后一个字元的位置const_iterator rend()const; iterator rend(); //返回string第一个字元位置的前面
rbegin和rend用于从后向前的叠代访问,通过设定叠代器string::reverse_iterator或string::const_reverse_iterator实现
- 字元串流处理:
通过定义ostringstream和istringstream变数实现,在#include <sstream>头档案中。
例如:
string input("hello,this is a test"); istringstream is(input); string s1,s2,s3,s4;is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" ostringstream os; os<<s1<<s2<<s3<<s4; 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",如下图:

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 对象的数值对字元串进行比较。序号比较自动区分大小写,因为字元的小写和大写版本有着不同的码位。但是,如果大小写在应用程式中并不重要,则可以指定忽略大小写的序号比较。这相当于使用固定区域性将字元串转换为大写,然后对结果执行序号比较。
有关单词、字元串和序号排序规则的更多信息,请参见 System.Globalization.CompareOptions 主题。
区分区域性的比较通常适用于排序,而序号比较则不适合。序号比较通常适用于确定两个字元串是否相等(即,确定标识),而区分区域性的比较则不适用。
比较和搜寻方法的“备注”指定方法是区分大小写、区分区域性还是两者区分。根据定义,任何字元串(包括空字元串 (""))的比较结果都大于空引用;两个空引用的比较结果为相等。
规範化
某些 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
执行绪安全
此类型是执行绪安全的。
平台Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。
版本信息
.NET Framework
受以下版本支持:3.0、2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
XNA Framework
受以下版本支持:1.0