Android的String用法

Android的String用法
Android的String用法

String : 字符串类型

一、构造函数

String(byte[ ]bytes):通过byte数组构造字符串对象。

String(char[ ]value):通过char数组构造字符串对象。

String(Sting original):构造一个original的副本。即:拷贝一个original。

String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。

例如:

byte[] b = {'a','b','c','d','e','f','g','h','i','j'};

char[] c = {'0','1','2','3','4','5','6','7','8','9'};

String sb = new String(b); //abcdefghij

String sb_sub = new String(b,3,2); //de

String sc = new String(c); //0123456789

String sc_sub = new String(c,3,2); //34

String sb_copy = new String(sb); //abcdefghij

System.out.println("sb:"+sb);

System.out.println("sb_sub:"+sb_sub);

System.out.println("sc:"+sc);

System.out.println("sc_sub:"+sc_sub);

System.out.println("sb_copy:"+sb_copy);

输出结果:sb:abcdefghij

sb_sub:de

sc:0123456789

sc_sub:34

sb_copy:abcdefghij

二、方法:

说明:①、所有方法均为public。

②、书写格式:[修饰符] <返回类型><方法名([参数列表])>

例如:static int parseInt(String s)

表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。

1. char charAt(int index):取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。

例如:String s = new String("abcdefghijklmnopqrstuvwxyz");

System.out.println("s.charAt(5): " + s.charAt(5) );

结果为:s.charAt(5): f

2.int compareTo(String anotherString):当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。

3. int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。

例如:String s1 = new String("abcdefghijklmn");

String s2 = new String("abcdefghij");

String s3 = new String("abcdefghijalmn");

System.out.println("https://www.360docs.net/doc/5c9198820.html,pareTo(s2): " + https://www.360docs.net/doc/5c9198820.html,pareTo(s2) ); //返回长度差

System.out.println("https://www.360docs.net/doc/5c9198820.html,pareTo(s3): " + https://www.360docs.net/doc/5c9198820.html,pareTo(s3) ); //返回'k'-'a'的差

结果为:https://www.360docs.net/doc/5c9198820.html,pareTo(s2): 4

https://www.360docs.net/doc/5c9198820.html,pareTo(s3): 10

4. String concat(String str):将该String对象与str连接在一起。

5. boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。

6. static String copyValueOf(char[] data) :

7. static String copyValueOf(char[] data, int offset, int count):这两个方法将char数组转换成String,与其中一个构造函数类似。

8. boolean endsWith(String suffix):该String对象是否以suffix结尾。

例如:String s1 = new String("abcdefghij");

String s2 = new String("ghij");

System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );

结果为:s1.endsWith(s2): true

9.boolean equals(Object anObject):当anObject不为空并且与当前String对象一样,返回true;否则,返回false。

10. byte[] getBytes():将该String对象转换成byte数组。

11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。

例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!

String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!

System.out.println( s1 );

结果为:I love you!

12. int hashCode():返回当前字符的哈希表码。

13. int indexOf(int ch):只找第一个匹配字符位置。

14. int indexOf(int ch, int fromIndex):从fromIndex开始找第一个匹配字符位置。

15. int indexOf(String str):只找第一个匹配字符串位置。

16. int indexOf(String str, int fromIndex):从fromIndex开始找第一个匹配字符串位置。

例如:String s = new String("write once, run anywhere!");

String ss = new String("run");

System.out.println("s.indexOf('r'): " + s.indexOf('r') );

System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );

System.out.println("s.indexOf(ss): " + s.indexOf(ss) );

结果为:s.indexOf('r'): 1

s.indexOf('r',2): 12

s.indexOf(ss): 12

17. int lastIndexOf(int ch)

18. int lastIndexOf(int ch, int fromIndex)

19. int lastIndexOf(String str)

20. int lastIndexOf(String str, int fromIndex)以上四个方法与13、14、15、16类似,不同的是:找最后一个匹配的内容。

public class CompareToDemo {

public static void main (String[] args) {

String s1 = new String("ac b de b fg");

System.out.println(https://www.360docs.net/doc/5c9198820.html,stIndexOf((int)'b',7));

}

}

运行结果:5

(其中fromIndex的参数为7,是从字符串acbdebf g的最后一个字符g开始往前数的位数。既是从字符c开始匹配,寻找最后一个匹配b的位置。所以结果为5)

21. int length():返回当前字符串长度。

22. String replace(char oldChar, char newChar):将字符号串中第一个oldChar替换成newChar。

23. boolean startsWith(String prefix):该String对象是否以prefix开始。

24. boolean startsWith(String prefix, int toffset):该String对象从toffset位置算起,是否以prefix开始。

例如:String s = new String("write once, run anywhere!");

String ss = new String("write");

String sss = new String("once");

System.out.println("s.startsWith(ss): " + s.startsWith(ss) );

System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );

结果为:s.startsWith(ss): true

s.startsWith(sss,6): true

25. String substring(int beginIndex):取从beginIndex位置开始到结束的子字符串。

26.String substring(int beginIndex, int endIndex):取从beginIndex位置开始到endIndex位置的子字符串。

27. char[ ] toCharArray():将该String对象转换成char数组。

28. String toLowerCase():将字符串转换成小写。

29. String toUpperCase():将字符串转换成大写。

例如:String s = new String("https://www.360docs.net/doc/5c9198820.html,ng.Class String");

System.out.println("s.toUpperCase(): " + s.toUpperCase() );

System.out.println("s.toLowerCase(): " + s.toLowerCase() );

结果为:s.toUpperCase(): https://www.360docs.net/doc/5c9198820.html,NG.CLASS STRING

s.toLowerCase(): https://www.360docs.net/doc/5c9198820.html,ng.class string

30. static String valueOf(boolean b)

31. static String valueOf(char c)

32. static String valueOf(char[] data)

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

34. static String valueOf(double d)

35. static String valueOf(float f)

36. static String valueOf(int i)

37. static String valueOf(long l)

38. static String valueOf(Object obj)

以上方法用于将各种不同类型转换成Java字符型。这些都是类方法。

Java中String类的常用方法:

public char charAt(int index)

返回字符串中第index个字符;

public int length()

返回字符串的长度;

public int indexOf(String str)

返回字符串中第一次出现str的位置;

public int indexOf(String str,int fromIndex)

返回字符串从fromIndex开始第一次出现str的位置;

public boolean equalsIgnoreCase(String another)

比较字符串与another是否一样(忽略大小写);

public String replace(char oldchar,char newChar)

在字符串中用newChar字符替换oldChar字符

public boolean startsWith(String prefix)

判断字符串是否以prefix字符串开头;

public boolean endsWith(String suffix)

判断一个字符串是否以suffix字符串结尾;

public String toUpperCase()

返回一个字符串为该字符串的大写形式;

public String toLowerCase()

返回一个字符串为该字符串的小写形式

public String substring(int beginIndex)

返回该字符串从beginIndex开始到结尾的子字符串;

public String substring(int beginIndex,int endIndex)

返回该字符串从beginIndex开始到endsIndex结尾的子字符串

public String trim()

返回该字符串去掉开头和结尾空格后的字符串

public String[] split(String regex)

将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

实例:

public class SplitDemo{

public static void main (String[] args) {

String date = "2008/09/10";

String[ ]dateAfterSplit= new String[3];

dateAfterSplit=date.split("/"); //以“/”作为分隔符来分割date字符串,并把结果放入3个字符串中。

for(int i=0;i

System.out.print(dateAfterSplit[i]+" ");

}

}

运行结果:2008 09 10 //结果为分割后的3个字符串实例:

TestString1.java:

程序代码

public class TestString1

{

public static void main(String args[]) {

String s1 = "Hello World" ;

String s2 = "hello world" ;

System.out.println(s1.charAt(1)) ;

System.out.println(s2.length()) ;

System.out.println(s1.indexOf("World")) ;

System.out.println(s2.indexOf("World")) ;

System.out.println(s1.equals(s2)) ;

System.out.println(s1.equalsIgnoreCase(s2)) ; String s = "我是J2EE程序员" ;

String sr = s.replace('我','你') ;

System.out.println(sr) ;

}

}

TestString2.java:

程序代码

public class TestString2

{

public static void main(String args[]) {

String s = "Welcome to Java World!" ;

String s2 = " magci " ;

System.out.println(s.startsWith("Welcome")) ;

System.out.println(s.endsWith("World")) ;

String sL = s.toLowerCase() ;

String sU = s.toUpperCase() ;

System.out.println(sL) ;

System.out.println(sU) ;

String subS = s.substring(11) ;

System.out.println(subS) ;

String s1NoSp = s2.trim() ;

System.out.println(s1NoSp) ;

}

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();

STRING类函数用法总结3

C++中的string类 前言:string的角色 1string使用 1.1充分使用string操作符 1.2眼花缭乱的string find函数 1.3string insert,replace,erase2string和C风格字符串 3string和Charactor Traits 4string建议 5小结 6附录前言:string的角色 C++语言是个十分优秀的语言,但优秀并不表示完美。还是有许多人不愿意使用C或者C++,为什么?原因众多,其中之一就是C/C++的文本处理功能太麻烦,用起来很不方便。以前没有接触过其他语言时,每当别人这么说,我总是不屑一顾,认为他们根本就没有领会C++的精华,或者不太懂C++,现在我接触perl,php,和Shell脚本以后,开始理解了以前为什么有人说C++文本处理不方便了。 举例来说,如果文本格式是:用户名电话号码,文件名name.txt Tom23245332 Jenny22231231 Heny22183942 Tom23245332 ... 现在我们需要对用户名排序,且只输出不同的姓名。 那么在shell编程中,可以这样用: awk'{print$1}'name.txt|sort|uniq 简单吧? 如果使用C/C++就麻烦了,他需要做以下工作: 先打开文件,检测文件是否打开,如果失败,则退出。 声明一个足够大得二维字符数组或者一个字符指针数组 读入一行到字符空间 然后分析一行的结构,找到空格,存入字符数组中。 关闭文件 写一个排序函数,或者使用写一个比较函数,使用qsort排序 遍历数组,比较是否有相同的,如果有,则要删除,copy... 输出信息 你可以用C++或者C语言去实现这个流程。如果一个人的主要工作就是处理这种

string类的使用教程

这个是string类的使用教程,可以参考一下 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用= 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。 好了,进入正题……… 首先,为了在我们的程序中使用string类型,我们必须包含头文件。如下:#include //注意这里不是string.h string.h是C字符串头文件 1.声明一个C++字符串 声明一个字符串变量很简单: string Str; 这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下: a) string s; //生成一个空字符串s b) string s(str) //拷贝构造函数生成str的复制品 c) string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值 d) string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值 e) string s(cstr) //将C字符串作为s的初值 f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s 的初值。 g) string s(num,c) //生成一个字符串,包含num个c字符 h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 i) s.~string() //销毁所有字符,释放内存 都很简单,我就不解释了。 2.字符串操作函数 这里是C++字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。 a) =,assign() //赋以新值 b) swap() //交换两个字符串的内容 c) +=,append(),push_back() //在尾部添加字符 d) insert() //插入字符 e) erase() //删除字符 f) clear() //删除全部字符 g) replace() //替换字符 h) + //串联字符串 i) ==,!=,<,<=,>,>=,compare() //比较字符串 j) size(),length() //返回字符数量

C++string类型总结

对C++中string类型的总结 string类对象的构造 简化构造函数原型如下(注意,为了简便,把模板中最后一个默认参数省略了): 1: explicit basic_string(); 2: string(const char *s); 3: string(const char *s, size_type n); 4: string(const string& str); 5: string(const string& str, size_type pos, size_type n); 6: string(size_type n, E c); 7: string(const_iterator first, const_iterator last); string对象的操作 字符串比较 支持六种关系运算符(==、!=、>、>=、<、<=),其采用字典排序策略(与C中字符串比较策略完全一样)。这六个关系运算符是非成员的重载运算符。而这些 运算符都支持三种操作数组合:string op string、string op const char*、cons t char* op string(其中op是前面六种关系运算符中任意一种)。解释:提供运算 符的三种重载版本主要是从效率角度考虑的,其避免了临时string对象的产生。 另外,string类还提供了各种重载版本的成员函数compare来比较,简化函数原型为: 1: int compare(const string& str) const; 2: int compare(size_type p0, size_type n0, const string& str); 3: int compare(size_type p0, size_type n0, const string& str, si ze_type pos, size_type n); 4: int compare(const char* s) const; 5: int compare(size_type p0, size_type n0, const char* s) const; 6: int compare(size_type p0, size_type n0, const char* s, size_t ype n) const; 返回值:如果调用该函数的对象的比较序列小于操作数比较序列,则返回负数; 若相等,则返回0;否则,返回正数。

C#中string用法

C#中String用法 标记 标记(tokenizing)是从文本中提取具体内容的过程。 下面的代码从句子中提取单词,并把它们输出到控制台。 class mytokenizing { static void main(string[ ] args) { string mystring="i like this food,are you?"; char[] separators={ ,,,?,:,!}; int startpos=0; int endpos=0; do { endpos=mystring.indexofany(separators,startpos); if ( endpos==-1 ) endpos=mystring.length; if ( endpos!=startpos ) console.writeline(mystring.substring( startpos,(endpos-startpos))); startpos=(endpos+1); }while(startpos

array.reverse(mychars); console.writeline(mystring); console.writeline(mychars); } } 任何继承于array的类都能利用reverse( )方法为数组中的元素重新排序。 字符串的插入、删除和替换 示例文件test.txt为字符串的来源。 下面代码以unicode格式读取文本。确保文件保存为读取时的格式。例如记事本允许将代码保存为unicode: aaaaaaaa,bbbbbbbb,cccccc dddddddd,eeeeeeee,ffffff gggggggg,hhhhhhhh,iiiiii jjjjjjjj,kkkkkkkk,llllll 下面代码加载数据并处理数据的测试工具。测试结果发送给控制台。 class myprocessfile { static void main(string [] args) { const string myname="test.txt"; stream readline; textwirter writeline; stringbuilder sb; readline=file.openread(myname); writeline=console.out; streamreader readlinesreader=new streamreader(readline,encoding.unicode); readlinesreader.basestream.seek(0,seekorigin.begin); while(readlinesreader.peek()>-1) { sb=new stringbuilder(readlinesreader.readline()); //插入字符串操作语句如:sb.append(",123"); console.writeline(sb.tostring()); } } } 在结尾添加一列内容: //displays aaaaaaaa,bbbbbbbb,cccccc,xxxxx //...... sb.append(",xxxxx");

CPPstring类常用函数

C++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类中函数介绍

标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用= 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。 好了,进入正题……… 首先,为了在我们的程序中使用string类型,我们必须包含头文件。 如下: #include //注意这里不是string.h string.h是C字符串头文件 #include using namespace std; 1.声明一个C++字符串 声明一个字符串变量很简单: string Str; 这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str 初始化为一个空字符串。String类的构造函数和析构函数如下: a) string s; //生成一个空字符串s b) string s(str) //拷贝构造函数生成str的复制品 c) string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值 d) string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值 e) string s(cstr) //将C字符串作为s的初值 f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。 g) string s(num,c) //生成一个字符串,包含num个c字符 h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 i) s.~string() //销毁所有字符,释放内存 都很简单,我就不解释了。 2.字符串操作函数 这里是C++字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。 a) =,assign() //赋以新值 b) swap() //交换两个字符串的内容 c) +=,append(),push_back() //在尾部添加字符

C 中的string常用函数用法总结.

C++中的string常用函数用法总结首先,为了在我们的程序中使用string类型,我们必须包含头文件。 如下: #include //注意这里不是string.h string.h是C字符串头文件 #include using namespace std; 1.声明一个C++字符串 声明一个字符串变量很简单: string Str; 这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str 初始化为一个空字符串。String类的构造函数和析构函数如下: a) string s; //生成一个空字符串s b) string s(str) //拷贝构造函数生成str的复制品 c) string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值 d) string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多st rlen”的部分作为字符串的初值 e) string s(cstr) //将C字符串作为s的初值 f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。 g) string s(num,c) //生成一个字符串,包含num个c字符 h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 i) s.~string() //销毁所有字符,释放内存 都很简单,我就不解释了。

C++string类标准库常用函数

C++ string类标准库常用函数 [string类的构造函数] string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 [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_iterator 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之间的部分连接到当前字符串的结尾

java 字符串常用函数及其用法

java中的字符串也是一连串的字符。但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理。将字符串作为内置的对象处理允许Java提供十分丰富的功能特性以方便处理字符串。下面是一些使用频率比较高的函数及其相关说明。 String相关函数 1)substring() 它有两种形式,第一种是:String substring(int startIndex) 第二种是:String substring(int startIndex,int endIndex) 2)concat() 连接两个字符串 例:String s="Welcome to "; String t=s.concat("AnHui"); 3)replace() 替换 它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下: String replace(char original,char replacement) 例如:String s=”Hello”.replace(’l',’w'); 第二种形式是用一个字符序列替换另一个字符序列,形式如下: String replace(CharSequence original,CharSequence replacement) 4)trim() 去掉起始和结尾的空格 5)valueOf() 转换为字符串 6)toLowerCase() 转换为小写 7)toUpperCase() 转换为大写 8)length() 取得字符串的长度 例:char chars[]={’a',’b’.’c'}; String s=new String(chars); int len=s.length(); 9)charAt() 截取一个字符 例:char ch; ch=”abc”.charAt(1); 返回值为’b’ 10)getChars() 截取多个字符 void getChars(int sourceStart,int sourceEnd,char target[],int targetStart) sourceStart 指定了子串开始字符的下标 sourceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。

c++中string的用法

之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。 首先,为了在我们的程序中使用string类型,我们必须包含头文件 。如下: #include //注意这里不是string.h string.h是C字符串头文件 1.声明一个C++字符串 声明一个字符串变量很简单: string Str; 这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str 初始化为一个空字符串。String类的构造函数和析构函数如下: a) string s; //生成一个空字符串s b) string s(str) //拷贝构造函数生成str的复制品 c) string s(str,stridx) //将字符串str内"始于位置stridx"的部分当作字符串的初值 d) string s(str,stridx,strlen) //将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值 e) string s(cstr) //将C字符串作为s的初值 f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。 g) string s(num,c) //生成一个字符串,包含num个c字符 h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 i) s.~string() //销毁所有字符,释放内存 都很简单,我就不解释了。 2.字符串操作函数 这里是C++字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。 a) =,assign() //赋以新值 b) swap() //交换两个字符串的内容 c) +=,append(),push_back() //在尾部添加字符 d) insert() //插入字符 e) erase() //删除字符 f) clear() //删除全部字符 g) replace() //替换字符 h) + //串联字符串 i) ==,!=,<,<=,>,>=,compare() //比较字符串 j) size(),length() //返回字符数量 k) max_size() //返回字符的可能最大个数 l) empty() //判断字符串是否为空 m) capacity() //返回重新分配之前的字符容量

Java中string的相关函数

Java中string的相关函数 字串与字元 文字字串是一个相当基本且经常被使用到的资料型态,然而在Java 中字串不象char、int 与float 一样是个基本资料型态,而是使用https://www.360docs.net/doc/5c9198820.html,ng.String 类别来加以表示,该类别定义了许多有用的方法来操作字串。String 物件是固定不变的(immutable):一旦一个String 物件被建立了,则没有任何方法可以改变它所代表的文字,因此,每个运作字串的方法会传回一个新的String 物件,而所修正过后的字串便是储存在此新物件里。 以下的程式码展示了你可以对字串所执行的运作: // 建立字串 String s = "Now "; // String 物件有个特殊的写法 String t = s + "is the time. "; // 使用+ 运算子来串连字串 String t1 = s + " " + 23.4; // + 将其它值转换为字串 t1 = String.valueOf( 'c '); // 从字元值获得对应的字串 t1 = String.valueOf(42); // 获得整数或其他任何数值的字串版本 t1 = Object.toString(); // 使用toString() 将物件转换为字串 // 字串长度 int len = t.length(); // 字串中的字元数:16 // 字串中的子字串 String sub = t.substring(4); // 传回从char 4 到最后的子字串:"is the time. " sub = t.substring(4, 6); // 传回chars 4 与5:"is " sub = t.substring(0, 3); // 传回chars 0 到2:"Now " sub = t.substring(x, y); // 传回从位置x 到y-1 间的子字串 int numchars = sub.length(); // 子字串的长度永远是(y-x) // 从一个字串中撷取(extract)出字元 char c = t.charAt(2); // 取得t 的第三个字元:w char[] ca = t.toCharArray(); // 将字串转换为一个字元阵列 t.getChars(0, 3, ca, 1); // 将t 中的前三个字元放到ca[1] 到ca[3] 中 // 大小写转换 String caps = t.toUpperCase(); // 转换为大写 String lower = t.toLowerCase(); // 转换为小写 // 字串比较 boolean b1 = t.equals( "hello "); // 传回flase:两字串并不相等 boolean b2 = t.equalsIgnoreCase(caps); // 忽略大小写的字串比较:true boolean b3 = t.startsWith( "Now "); // 传回true boolean b4 = t.endsWith( "time. "); // 传回true int r1 = https://www.360docs.net/doc/5c9198820.html,pareTo( "Pow "); // 传回值<0:s 在"Pow "之前 int r2 = https://www.360docs.net/doc/5c9198820.html,pareTo( "Now "); // 传回值0:两字串相等

string的用法

1.声明一个C++字符串 声明一个字符串变量很简单: string Str; 这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str 初始化为一个空字符串。String类的构造函数和析构函数如下: a) string s; //生成一个空字符串s b) string s(str) //拷贝构造函数生成str的复制品 c) string s(str,stridx) //将字符串str内"始于位置stridx"的部分当作字符串的初值 d) string s(str,stridx,strlen) //将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值 e) string s(cstr) //将C字符串作为s的初值 f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。 g) string s(num,c) //生成一个字符串,包含num个c字符 h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 i) s.~string() //销毁所有字符,释放内存 都很简单,我就不解释了。 2.字符串操作函数 这里是C++字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。 a) =,assign() //赋以新值 b) swap() //交换两个字符串的内容 c) +=,append(),push_back() //在尾部添加字符 d) insert() //插入字符 e) erase() //删除字符 f) clear() //删除全部字符 g) replace() //替换字符 h) + //串联字符串 i) ==,!=,<,<=,>,>=,compare() //比较字符串 j) size(),length() //返回字符数量 k) max_size() //返回字符的可能最大个数 l) empty() //判断字符串是否为空 m) capacity() //返回重新分配之前的字符容量 n) reserve() //保留一定量内存以容纳一定数量的字符 o) [ ], at() //存取单一字符 p) >>,getline() //从stream读取某值 q) << //将谋值写入stream r) copy() //将某值赋值为一个C_string s) c_str() //将内容以C_string返回 t) data() //将内容以字符数组形式返回 u) substr() //返回某个子字符串

Android的String用法

String : 字符串类型 一、构造函数 String(byte[ ]bytes):通过byte数组构造字符串对象。 String(char[ ]value):通过char数组构造字符串对象。 String(Sting original):构造一个original的副本。即:拷贝一个original。 String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。 例如: byte[] b = {'a','b','c','d','e','f','g','h','i','j'}; char[] c = {'0','1','2','3','4','5','6','7','8','9'}; String sb = new String(b); //abcdefghij String sb_sub = new String(b,3,2); //de String sc = new String(c); //0123456789 String sc_sub = new String(c,3,2); //34 String sb_copy = new String(sb); //abcdefghij System.out.println("sb:"+sb); System.out.println("sb_sub:"+sb_sub); System.out.println("sc:"+sc); System.out.println("sc_sub:"+sc_sub); System.out.println("sb_copy:"+sb_copy); 输出结果:sb:abcdefghij sb_sub:de sc:0123456789 sc_sub:34 sb_copy:abcdefghij 二、方法: 说明:①、所有方法均为public。 ②、书写格式:[修饰符] <返回类型><方法名([参数列表])> 例如:static int parseInt(String s) 表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。 1. char charAt(int index):取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。 例如:String s = new String("abcdefghijklmnopqrstuvwxyz"); System.out.println("s.charAt(5): " + s.charAt(5) ); 结果为:s.charAt(5): f 2.int compareTo(String anotherString):当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。 3. int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。 例如:String s1 = new String("abcdefghijklmn"); String s2 = new String("abcdefghij"); String s3 = new String("abcdefghijalmn"); System.out.println("https://www.360docs.net/doc/5c9198820.html,pareTo(s2): " + https://www.360docs.net/doc/5c9198820.html,pareTo(s2) ); //返回长度差

String常见的操作和方法

String常见的操作和方法 String类适用于描述字符串事物 那么它就提供了多个方法对字符串进行操作。 常见的操作有哪些? “afbs” 1、获取 1.1 字符串中包含的字符数,也就是字符串的长度。 int length(): 获取长度。 1.2 根据位置获取位置上某个字符。 char charAt(int index): 1.3 根据字符获取字符在字符串中位置。 int indexOf(int ch): 返回的是ch在字符串中第一次出现的位置。 int indexOf(int ch, int fromIndex): 从fromIndex指定位置开始,获取ch在字符串中出现的位置。 int indexOf(int str): 返回的是str在字符串中第一次出现的位置。 int indexOf(int str, int fromIndex): 从fromIndex指定位置开始,获取str在字符串中出现的位置。 int lastIndexOf(int ch); 2、判断。 2.1 字符串中是否包含某一个子串。 boolean contains(str): 特殊之处:indexOf(str):可以索引str第一次出现的位置,如果返回-1.表示该str不存在字符串中。 所以,也可以用于对指定判断是否包含。 if(str.indexOf("aa")!=-1) 而且该方法既可以判断,又可以获取出现的位置。 2.2 字符串中是否有内容。

boolean ifEmpty(): 原理就是判断长度是否为0. 2.3 字符串是否是以指定内容开头。 boolean startsWith(str); 2.4 字符串是否是以指定内容结尾。 boolean endsWith(str); 2.5判断字符串内容是否相同。复写了Object类中的equals方法。boolean equals(str); 2.6判断内容是否相同,并忽略大小写。 boolean equalsIgnoreCase(); 3、转换 3.1 将字符数组转成字符串。 构造函数:String(char[]) String(char[],offset,count):将字符数组中的一部分转成字符串。 静态方法: static String copyValueOf(char[]); static String copyValueOf(char[] data,int offset,int count) static String valueOf(char[]): 3.2 将字符串转成字符数组。 char[] toCharArray(): 3.3 将字节数组转成字符串。 String(byte[]) String(byte[],offset,count):将字节数组中的一部分转成字符串。 3.4 将字符串转成字节数组。** byte[] getBytes(): 3.5 将基本数据类型转成字符串。 static String valueOf(int) static String valueOf(double) 3+"";//String.valueOf(3); 特殊:字符串和字节数组在转换过程中,是可以指定编码表的。

C 中的STRING用法

#include//注意是,不是,带.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'分开。

相关文档
最新文档