string字符串的常用方法

 一、创建并初始化字符串:

  1、使用字符串常量直接初始化 String s="hello!";

  2、使用构造方法创建并初始化 String();//初始化一个对象,表示空字符序列

  String(value);//利用已存在的字符串常量创建一个新的对象

  String (char[] value);//利用一个字符数组创建一个字符串

  String(char[] value,int offset,int count);//截取字符数组offset到count的字符创建一个非空串

  String(StringBuffer buffer);//利用StringBuffer对象初始化String对象

  二、String类主要方法的使用:

  1、获取长度 *.length();//这与数组中的获取长度不同,*.length;

  2、比较字符串(1) equals() //判断内容是否相同

  (2)compareTo() //判断字符串的大小关系

  (3)compareToIgnoreCase(String int) //在比较时忽略字母大小写

  (4)==   //判断内容与地址是否相同

  (5)equalsIgnoreCase() //忽略大小写的情况下判断内容是否相同

  如果想对字符串中的部分内容是否相同进行比较,可以用

  (6)reagionMatches() //有两种 public boolean regionMatches(int toffset, String other,int ooffset,int len);表示如果String对象的一个子字符串与参数other的一个子字符串是相同的字符序列,则为true.要比较的String 对象的字符串从索引toffset开始,other的字符串从索引ooffset开始,长度为len。

  public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//用布尔类型的参数指明两个字符串的比较是否对大小写敏感。

  三、查找字符串中某个位置的字符

  public char charAt(int index);//返回指定索引index位置上的字符,索引范围从0开始

  四、查找指定字符串在字符串中第一次或最后一词出现的位置

  在String类中提供了两种查找指定位置的字符串第一次出现的位置的方法

  (1)public int indexOf(String str);//从字符串开始检索str,并返回第一次出现的位置,未出现返回-1

  (2)public int indexOf(String str,int fromIndex);//从字符串的第fromIndex个字符开始检索str

  查找最后一次出现的位置有两种方法

  (1)public int lastIndexOf(String str);

  (2)public int lastIndexOf(String str,int fromIndex);

  如果不关心字符串的确切位置则可使用public boolean contains(CharSequence s);

  五、检查字符串的起始字符和结束字符

  开始的字符串两种方法

  (1)public boolean starWith(String prefix,int toffset);//如果参数prefix表示的字符串序列是该对象从索引toffset处开始的子字符串,则返回true

  (2)public boolean starWith(String prefix);

  结束的字符串方法

  public boolean endsWith(String suffix);

  六、截取子串

  (1)public String subString(int beginIndex);

  (2)public String subString(int beginIndex,int endIndex);//返回的字符串是从beginIndex开始到endIndex-1的串

  要返回后4位可以这样写Syetem.out.println(*.subString()(*.length()-4));

  七、字符串的替换

  两种方法

  (1)public String replace(char oldChar,char newChar);

  (2)public String replace(CharSequence target,CharSequence replacement);//把原来的etarget子序列替换为replacement序列,返回新串

  (3)public String replaceAll(String regex,String replacement);//用正则表达式实现对字符串的匹配

  八、字符串的大小写替转换

  (1)public String toLowerCase(Locale locale);

  (2)public String toLowerCase();

  (3)public String toupperCase(Locale locale);

  (4)public String toUpperCase();

  九、去除字符串首尾空格

  *.trim();

  十、字符串转换

  1、将字符串转换成字符数组

  public char[] toCharArray();

  2、将字符串转换成字符串数组

  public String[] split(String regex);//regex 是给定的匹配

  3、将其它数据类型转化为字符串

  (1)public static String valueOf(boolean b);

  (2)public static String valueOf(char c);

  (3)public static String valueOf(int i);

  (4)public static String valueOf(long i);

  (5)public static String valueOf(float f);

  (6)public static String valueOf(double d);

  (7)public static String valueOf(char[] data);

  (8)public static String valueOf(Object obj);

  可变字符串的创建和初始化

  两种方法:

  public StringBuffer();

  public StringBuffer(int caoacity);

  StringBuffer类主要方法的使用:

  一、获取可变字符串长度

  (1)public int length();

  (2)public int capacity();

  (3)public void setLength(int newLength);

  二、比较可变字符串

  用String 类的equals()方法比较,但是不同。

  类Object中的equals()方法比较的是两个对象的地址是否相等,而不仅仅是比较内容,但是String类在继承Object类的时候重写了equals()方法,只是比较两个对象的内容是否相等

  而在StringBuffer类中没有重写Object类的equals()方法,所以比较的是地址和内容。

  三、追加和插入字符串

  (1)追加 public StringBuffer append(type t);

  (2)插入 public StringBuffer insert(int offset,type t);//在offset处加入类型为type的字符串

  四、反转和删除字符串

  (1)反转 public StringBuffer reverse();

  (2)删除 public StringBuffer delete(int start,int end);

  五、减少用于可变字符序列的存储空间

  public void trimToSize();

  六、StringBuffer类转换成String类

  public String toString();


1、定义:
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迭代器之间的部分赋给字符串
 
2、append方法:
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之间的部分连接到当前字符串的结尾
 
3、比较compar方法:
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  
 
4、查找find方法:
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相似,只不过是从后向前查找
 
5、插入insert方法:
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
 
6、删除erase方法:
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
 
7、替换replace方法:
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)之间的字符串 
8、获得字串substr方法:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
 
9、交换swap的方法:
void swap(string &s2);    //交换当前字符串与s2的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值