Java中String的用法总结

时间:2024.5.2

构造方法:

String()

初始化一个新创建的 String 对象,它表示一个空字符序列。

String(byte[] bytes)

构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。 String(byte[] bytes, int offset, int length)

构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。 String(byte[] bytes, int offset, int length, String charsetName)

构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。 String(byte[] bytes, String charsetName)

构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。 String(char[] value)

分配一个新的 String,它表示当前字符数组参数中包含的字符序列。 String(char[] value, int offset, int count)

分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。 String(int[] codePoints, int offset, int count)

分配一个新的 String,它包含该 Unicode 代码点数组参数的一个子数组的字符。

String(String original)

初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。

String(StringBuffer buffer)

分配一个新的字符串,它包含当前包含在字符串缓冲区参数中的字符序列。 String(StringBuilder builder)

分配一个新的字符串,它包含当前包含在字符串生成器参数中的字符序列 方法总结:

char charAt(int index)

返回指定索引处的 char 值。

int codePointAt(int index)

返回指定索引处的字符(Unicode 代码点)。

int codePointBefore(int index)

返回指定索引之前的字符(Unicode 代码点)。

int codePointCount(int beginIndex, int endIndex)

返回此 String 的指定文本范围中的 Unicode 代码点数。

int compareTo(String anotherString)

按字典顺序比较两个字符串(区分大小写)。

int compareToIgnoreCase(String str)

不考虑大小写,按字典顺序比较两个字符串。

String concat(String str)

将指定字符串联到此字符串的结尾。

boolean contains(CharSequence s)

当且仅当此字符串包含 char 值的指定序列时,才返回 true。

boolean contentEquals(CharSequence cs)

当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true。

boolean contentEquals(StringBuffer sb)

当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true。

static String copyValueOf(char[] data)

返回指定数组中表示该字符序列的字符串。

static String copyValueOf(char[] data, int offset, int count)

返回指定数组中表示该字符序列的字符串。

boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结束。

boolean equals(Object anObject)

比较此字符串与指定的对象。

boolean equalsIgnoreCase(String anotherString)

将此 String 与另一个 String 进行比较,不考虑大小写。

static String format(Locale l, String format, Object... args)

使用指定的语言环境、格式字符串和参数返回一个格式化字符串。

static String format(String format, Object... args)

使用指定的格式字符串和参数返回一个格式化字符串。

byte[] getBytes()

使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。

byte[] getBytes(String charsetName)

使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

将字符从此字符串复制到目标字符数组。

int hashCode()

返回此字符串的哈希码。

int indexOf(int ch)

返回指定字符在此字符串中第一次出现处的索引。

int indexOf(int ch, int fromIndex)

从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。 int indexOf(String str)

返回第一次出现的指定子字符串在此字符串中的索引。

int indexOf(String str, int fromIndex)

从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 String intern()

返回字符串对象的规范化表示形式。

int lastIndexOf(int ch)

返回最后一次出现的指定字符在此字符串中的索引。

int lastIndexOf(int ch, int fromIndex)

从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。

int lastIndexOf(String str)

返回在此字符串中最右边出现的指定子字符串的索引。

int lastIndexOf(String str, int fromIndex)

从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

int length()

返回此字符串的长度。

boolean matches(String regex)

通知此字符串是否匹配给定的正则表达式。

int offsetByCodePoints(int index, int codePointOffset)

返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 测试两个字符串区域是否相等。

boolean regionMatches(int toffset, String other, int ooffset, int len)

测试两个字符串区域是否相等。

String replace(char oldChar, char newChar)

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。

String replace(CharSequence target, CharSequence replacement)

使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每个子字符串。 String replaceAll(String regex, String replacement)

使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。

String replaceFirst(String regex, String replacement)

使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的第一个子字符串。

String[] split(String regex)

根据给定的正则表达式的匹配来拆分此字符串。

String[] split(String regex, int limit)

根据匹配给定的正则表达式来拆分此字符串。

boolean startsWith(String efix)

测试此字符串是否以指定的前缀开始。

boolean startsWith(String prefix, int toffset)

测试此字符串是否以指定前缀开始,该前缀以指定索引开始。

CharSequence subSequence(int beginIndex, int endIndex)

返回一个新的字符序列,它是此序列的一个子序列。

String substring(int beginIndex)

返回一个新的字符串,它是此字符串的一个子字符串。

String substring(int beginIndex, int endIndex)

返回一个新字符串,它是此字符串的一个子字符串。

char[] toCharArray()

将此字符串转换为一个新的字符数组。

String toLowerCase()

使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 String toLowerCase(Locale locale)

使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。

String toString()

返回此对象本身(它已经是一个字符串!)。

String toUpperCase()

使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 String toUpperCase(Locale locale)

使用给定的 Locale 规则将此 String 中的所有字符都转换为大写。 String trim()

返回字符串的副本,忽略前导空白和尾部空白。

static String valueOf(boolean b)

返回 boolean 参数的字符串表示形式。

static String valueOf(char c)

返回 char 参数的字符串表示形式。

static String valueOf(char[] data)

返回 char 数组参数的字符串表示形式。

static String valueOf(char[] data, int offset, int count)

返回 char 数组参数的特定子数组的字符串表示形式。

static String valueOf(double d)

返回 double 参数的字符串表示形式。

static String valueOf(float f)

返回 float 参数的字符串表示形式。

static String valueOf(int i)

返回 int 参数的字符串表示形式。

static String valueOf(long l)

返回 long 参数的字符串表示形式。

static String valueOf(Object obj)

返回 Object 参数的字符串表示形式。


第二篇:string用法总结


标准C++中的string类的用法总结

相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用。但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯定的。也许有人会说,即使不用MFC框架,也可以想办法使用MFC中的API,具体的操作方法在本文最后给出操作方法。其实,可能很多人很可能会忽略掉标准C++中string类的使用。标准C++中提供的string类得功能也是非常强大的,一般都能满足我们开发项目时使用。现将具体用法的一部分罗列如下,只起一个抛砖引玉的作用吧,好了,废话少说,直接进入正题吧! 要想使用标准C++中string类,必须要包含

#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 using std::string;

using std::wstring;

using namespace std;

下面你就可以使用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为起始位置的字符数组中,返回实际拷贝的数目

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个字符c

string &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::npos

int 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::npos

int 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处插入串s

string &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处插入串s

string &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个字符c

string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s

string &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)之间的部分替换为串s

string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c

string &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个字符c

iterator 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不允许改变迭代的内容。常用迭代器函数有:

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

字符串流处理:

通过定义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类逊色多少,呵呵,个人意见!

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

1.在工程目录下右键选择"Properties”--->"Configuration Properties”--->“General”--->"Use of MFC"--->"Use MFC in a Static Library",

默认的是:"Use Standard Windows Libraries",如下图: 和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

更多相关推荐:
in的典型用法小结

in的典型用法小结in是最常用的介词之一其意义和用法不下二十余种由它构成的短语更是不计其数是高考试题中常出现的考点它还可用作副词和形容词作为副词的用法也有近十种下面我们一起总结一下in的几种典型用法一in用作介...

介词in的用法汇总

介词“in”的用法“in”是英语中最为活跃的介词之一,所以它给我们的学习带了很大的挑战。细分下来,它有如下用法年、月、年月、季节、周即在来年,在某月,在某年某月(但在某年某月某日则用on),在四季,在第几周等都…

in case用法小结

incase用法小结原题如下1JohnmayphonetonightIdon39twanttogoouthephonesNMET20xx春季AaslongasBinorderthatCincaseDsothat...

in,after.since.by.for用法小结

1inafterWe39llmeetagaintwoweeks两周后我们还会再见in时间段表示将来即在多长时间以后常用将来时且是非延续性动词after时间段表示过了段时间以后常指从过去某时算起的一段时间之后所以...

At on in的用法

Atonin时间用法总1At后加具体的时间小时分钟2on后加天日子3in后加较长的时间年月季习惯用法InIntheeveninginthemorningintheafternoonAt1Atdaybrestat...

in 的用法

1介词prep在里在中Thereisnocloudinthesky晴空万里无云GlasgowisinScotland格拉斯哥在苏格兰2介词prep进入Rayputhishandinthewater雷把手放进水中...

in的用法 - 副本

In的用法1介词prep在里在中Thereisnocloudinthesky晴空万里无云GlasgowisinScotland格拉斯哥在苏格兰2介词prep进入Rayputhishandinthewater雷把...

介词in,on,to 表示方位的用法

介词in,on,to表示方位的用法介词in,on,to都可以用来表示某个位置的方向,它们的意义不同,故表示的方向及范围也不同:1.in表示方位,含义是“在……之内”,即一个小地方处在一个大地方的范围(疆域)之内…

介词on、in、at 的用法

1)in把地方、地点、位置当作一个范围或一个封闭的空间:1.IliveinBeijing.我住在北京。(大城市用in)2.IliveinEngland,atLondon.我住在英国伦敦。(England大过Lo…

at in on 等介词的用法

英语老师们拿去吧口诀1年月周前要用in日子前面却不行遇到几号要用on上午下午又是in要说某日上下午用on换in才能行午夜黄昏用at黎明用它也不借at也在时分前说差用to说过要用part口诀2in在里out在外在...

as though (as if)的虚拟语气用法

asif(though)从句用虚拟语气一、基本用法以asif(asthough)引导的方式状语从句或表语从句,有时用虚拟语气,若表示与现在事实相反,谓语动词用一般过去时;若表示与过去事实相反,用过去完成时;表示…

as if 的用法

asif的用法一asif从句的作用1在lookseem等系动词后引导表语从句如Shelooksasifsheweretenyearsyounger她看起来好像年轻了十岁Itseemsasifourteamisg...

in的用法总结(32篇)