导航:首页 > 使用方法 > 列举字符串的常用方法并解释

列举字符串的常用方法并解释

发布时间:2022-05-01 05:09:55

A. java中字符、字符串的常用方法

char的所有方法:
static int charCount(int codePoint)
确定表示指定字符(Unicode 代码点)所需的 char 值的数量。
char charValue()
返回此 Character 对象的值。
static int codePointAt(char[] a, int index)
返回 char 数组的给定索引上的代码点。
static int codePointAt(char[] a, int index, int limit)
返回 char 数组的给定索引上的代码点,该数组中只有那些具有小于 limit 的 index 值的数组元素可以使用。
static int codePointAt(CharSequence seq, int index)
返回 CharSequence 的给定索引上的代码点。
static int codePointBefore(char[] a, int index)
返回 char 数组的给定索引前面的代码点。
static int codePointBefore(char[] a, int index, int start)
返回 char 数组的给定索引前面的代码点,该数组中只有那些具有大于等于 start 的 index 值的数组元素可以使用。
static int codePointBefore(CharSequence seq, int index)
返回 CharSequence 的给定索引前面的代码点。
static int codePointCount(char[] a, int offset, int count)
返回 char 数组参数的子数组中 Unicode 代码点的数量。
static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
返回指定字符序列的文本范围内的 Unicode 代码点数量。
int compareTo(Character anotherCharacter)
根据数字比较两个 Character 对象。
static int digit(char ch, int radix)
返回使用指定基数的字符 ch 的数值。
static int digit(int codePoint, int radix)
返回使用指定基数的指定字符(Unicode 代码点)的数值。
boolean equals(Object obj)
将此对象与指定对象比较。
static char forDigit(int digit, int radix)
确定使用指定基数的特定数字的字符表示形式。
static byte getDirectionality(char ch)
返回给定字符的 Unicode 方向属性。
static byte getDirectionality(int codePoint)
返回给定字符(Unicode 代码点)的 Unicode 方向属性。
static int getNumericValue(char ch)
返回指定的 Unicode 字符表示的 int 值。
static int getNumericValue(int codePoint)
返回指定字符(Unicode 代码点)表示的 int 值。
static int getType(char ch)
返回一个指示字符的常规类别的值。
static int getType(int codePoint)
返回一个指示字符的常规类别的值。
int hashCode()
返回此 Character 的哈希码。
static boolean isDefined(char ch)
确定字符是否被定义为 Unicode 中的字符。
static boolean isDefined(int codePoint)
确定字符(Unicode 代码点)是否被定义为 Unicode 中的字符。
static boolean isDigit(char ch)
确定指定字符是否为数字。
static boolean isDigit(int codePoint)
确定指定字符(Unicode 代码点)是否为数字。
static boolean isHighSurrogate(char ch)
确定给出的 char 值是否为一个高代理项代码单元(也称为前导代理项代码单元)。
static boolean isIdentifierIgnorable(char ch)
确定是否应该认为指定字符是 Java 标识符或 Unicode 标识符中可忽略的一个字符。
static boolean isIdentifierIgnorable(int codePoint)
确定是否应该认为指定字符(Unicode 代码点)是 Java 标识符或 Unicode 标识符中可忽略的一个字符。
static boolean isISOControl(char ch)
确定指定字符是否为 ISO 控制字符。
static boolean isISOControl(int codePoint)
确定引用的字符(Unicode 代码点)是否为 ISO 控制字符。
static boolean isJavaIdentifierPart(char ch)
确定指定字符是否可以是 Java 标识符中首字符以外的部分。
static boolean isJavaIdentifierPart(int codePoint)
确定字符(Unicode 代码点)是否可以是 Java 标识符中首字符以外的部分。
static boolean isJavaIdentifierStart(char ch)
确定是否允许将指定字符作为 Java 标识符中的首字符。
static boolean isJavaIdentifierStart(int codePoint)
确定是否允许将字符(Unicode 代码点)作为 Java 标识符中的首字符。
static boolean isJavaLetter(char ch)
已过时。 由 isJavaIdentifierStart(char) 取代。
static boolean isJavaLetterOrDigit(char ch)
已过时。 由 isJavaIdentifierPart(char) 取代。
static boolean isLetter(char ch)
确定指定字符是否为字母。
static boolean isLetter(int codePoint)
确定指定字符(Unicode 代码点)是否为字母。
static boolean isLetterOrDigit(char ch)
确定指定字符是否为字母或数字。
static boolean isLetterOrDigit(int codePoint)
确定指定字符(Unicode 代码点)是否为字母或数字。
static boolean isLowerCase(char ch)
确定指定字符是否为小写字母。
static boolean isLowerCase(int codePoint)
确定指定字符(Unicode 代码点)是否为小写字母。
static boolean isLowSurrogate(char ch)
确定给定 char 值是否一个低代理项代码单元(也称为尾部代理项代码单元)。
static boolean isMirrored(char ch)
确定指定字符依据 Unicode 规范是否对称。
static boolean isMirrored(int codePoint)
确定指定字符(Unicode 代码点)依据 Unicode 规范是否对称。
static boolean isSpace(char ch)
已过时。 由 isWhitespace(char) 取代。
static boolean isSpaceChar(char ch)
确定指定字符是否为 Unicode 空白字符。
static boolean isSpaceChar(int codePoint)
确定指定字符(Unicode 代码点)是否为 Unicode 空白字符。
static boolean isSupplementaryCodePoint(int codePoint)
确定指定字符(Unicode 代码点)是否在增补字符范围内。
static boolean isSurrogatePair(char high, char low)
确定指定的 char 值对是否为有效的代理项对。
static boolean isTitleCase(char ch)
确定指定字符是否为首字母大写字符。
static boolean isTitleCase(int codePoint)
确定指定字符(Unicode 代码点)是否为首字母大写字符。
static boolean isUnicodeIdentifierPart(char ch)
确定指定字符是否可以是 Unicode 标识符中首字符以外的部分。
static boolean isUnicodeIdentifierPart(int codePoint)
确定指定字符(Unicode 代码点)是否可以是 Unicode 标识符中首字符以外的部分。
static boolean isUnicodeIdentifierStart(char ch)
确定是否允许将指定字符作为 Unicode 标识符中的首字符。
static boolean isUnicodeIdentifierStart(int codePoint)
确定是否允许将指定字符(Unicode 代码点)作为 Unicode 标识符中的首字符。
static boolean isUpperCase(char ch)
确定指定字符是否为大写字母。
static boolean isUpperCase(int codePoint)
确定指定字符(Unicode 代码点)是否为大写字母。
static boolean isValidCodePoint(int codePoint)
确定指定的代码点是否为从 0x0000 到 0x10FFFF 范围之内的有效 Unicode 代码点值。
static boolean isWhitespace(char ch)
确定指定字符依据 Java 标准是否为空白字符。
static boolean isWhitespace(int codePoint)
确定指定字符(Unicode 代码点)依据 Java 标准是否为空白字符。
static int offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)
返回给定 char 子数组中的索引,它是从给定 index 到 codePointOffset 代码点的偏移量。
static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
返回给定字符序列中的索引,它是从给定 index 到 codePointOffset 代码点的偏移量。
static char reverseBytes(char ch)
返回通过反转指定 char 值中的字节顺序而获得的值。
static char[] toChars(int codePoint)
将指定的字符(Unicode 代码点)转换成其存储在 char 数组中的 UTF-16 表示形式。
static int toChars(int codePoint, char[] dst, int dstIndex)
将指定字符(Unicode 代码点)转换为其 UTF-16 表示形式。
static int toCodePoint(char high, char low)
将指定的代理项对转换为其增补代码点值。
static char toLowerCase(char ch)
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为小写。
static int toLowerCase(int codePoint)
使用取自 UnicodeData 文件的大小写映射信息将字符(Unicode 代码点)参数转换为小写。
String toString()
返回表示此 Character 值的 String 对象。
static String toString(char c)
返回一个表示指定 char 值的 String 对象。
static char toTitleCase(char ch)
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为首字母大写。
static int toTitleCase(int codePoint)
使用取自 UnicodeData 文件的大小写映射信息将字符(Unicode 代码点)参数转换为首字母大写。
static char toUpperCase(char ch)
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为大写。
static int toUpperCase(int codePoint)
使用取自 UnicodeData 文件的大小写映射信息将字符(Unicode 代码点)参数转换为大写。
static Character valueOf(char c)
返回一个表示指定 char 值的 Character 实例

string所有方法:
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)
将此字符串与指定的 CharSequence 比较。
boolean contentEquals(StringBuffer sb)
将此字符串与指定的 StringBuffer 比较。
static String ValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
static String ValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。
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 序列,并将结果存储到一个新的 byte 数组中。
byte[] getBytes(Charset charset)
使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。
byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
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()
返回字符串对象的规范化表示形式。
boolean isEmpty()
当且仅当 length() 为 0 时返回 true。
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 prefix)
测试此字符串是否以指定的前缀开始。
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 参数的字符串表示形式

B. 2、列出至少3种常用的字符串操作方法,并举例应用

1、LEFT:从左侧截取字符串。

如:=LEFT("ABCD",2) 从左侧截取2位,结果是AB
2、RIGHT:从右侧截取字符串。
如:=RIGHT("ABCD",2) 从右侧截取2位,结果是CD
3、LEN:获取字符串的长度。
如=LEN("ABCD") 结果是4

C. 在C语言中处理字符的几种常用方法

基本上两种:字符数组和字符指针。字符数组名是常量,字符指针是变量
1、用字符数组:一个一维字符数组存储一个字符串,二维数组存储多个字符串。如:
int i;
char s1[]="abc",s2[][80]={"ABCD","XYZ"},s3[80];
scanf("%s",s3);
printf("%s,%s\n",s1,s3);
for(i=0;i<2;i++)printf("%s\n",s2[i]);
2、用字符指针:字符指针指向一个字符串,字符指针数组处理多个字符串。如:
int i;
char *p1="abc",*p2[]={"ABCD","XYZ"},**p;
printf("%s\n",p1);
for(i=0;i<2;i++)printf("%s\n",p2[i]);
p=p2;
for(i=0;i<2;i++)printf("%s\n",*p++);
3、字符数组与字符指针配合一起使用:字符数组提供字符串的存储,处理字符串用字符指针
char s[80],*p;
p=s;
scanf("%s",p);
printf("%s",p);
在string.h中所有处理字符串函数,对字符数组和字符指针都适用。

D. String中几个常用的方法

length():求字符串的长度

indexOf():求某个字符在字符串中的位置

charAt():求一个字符串中某个位置的值

equals():比较两个字符串是否相同

replace():将字符串中的某些字符用别的字符替换掉。形如replace(“abc”,”ddd”);字符串中的abc将会被ddd替换掉。

split():根据给定正则表达式的匹配拆分此字符串。形如 String s = "The time is going quickly!"; str1=s.split(" ");

substring():输出一个新的字符串,它是此字符串中的子串,形如substring(3,7);它将字符串中的第四个第五个第六个输出。

trim():将字符串开头的空白(空格)和尾部的空白去掉。

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

toLowerCase():将字符串中所有的大写改变成小写

toUpperCase():将字符串中所有的小写改变为大写
public static void main(String[] args) {
String str1 = new String("abcdef");//初始化一个String对象
System.out.println(str1.length());//输出字符串的长度
System.out.println(str1.indexOf("c"));//输出指定字符在字符串中的位置
System.out.println(str1.charAt(3));//输出指定位置的值
String str2 = new String("wangmeixi");
System.out.println(str1.equals(str2));//比较两个字符串是否是相等的。

String str3 = new String("abcdefghijk");
System.out.println(str3);//输出str3的字符串
System.out.println( str3.replace("def","fed"));//输出替换之后的字符串
}

E. Python中字符串常用操作有哪些

字符串是 Python
中常用的数据类型,我们可以使用引号('或")来创建字符串,对字符串进行使用和操作,需要用到特定的函数,以下是常用的Python字符串操作方法:
1. capitalize()
作用:capitalize() 主要是用来实现字符串首字母大写,其他字母小写的功能。
实例:
1
2str1 = "oldboy"
print(str1.capitalize())
输出结果:Oldboy
2. swapcase()
作用:swapcase() 主要是用来实现字符串大小写反转。
实例:
1
2str1 = " Oldboy"
print(str1.swapcase())
输出结果:oLDBOY
3. title()
作用:title() 主要是用来实现字符串非字母隔开的部分,首字母大写,其余字母小写。
实例:
1
2str1 = "Old boy e com"
print(str1.title())
输出结果:Old Boy E Com
4. upper()
作用:upper() 主要是用来实现字符串所有字母全部大写。
实例:
1
2str1 = "Oldboye"
print(str1.upper())
输出结果:OLDBOYEDU
5. lower()
作用:lower() 主要是用来实现字符串所有字母全部小写。
实例:
1
2str1 = "oLDBOYEDU"
print(str1.lower())
输出结果:oldboye
6. center()
作用:center() 主要是用来实现字符串内容居中,填充物默认为空。
实例:
1
2
3str1 = "Oldboye"
print(str1.center(15))
print(str1.center(15,"*"))
输出结果:
Oldboye
***Oldboye***
7. find()
作用:find() 主要作用是通过元素找索引,可以整体找,可以切片,找不到则返回-1。
实例:
1
2
3str1 = "Oldboye"
print(str1.find('b'))
print(str1.find('A'))
输出结果:3 -1
8. index()
作用:index() 主要作用是通过元素找索引,可以整体找,可以切片,找不到会报错。
实例:
1
2
3str1 = " Oldboye "
print(str1.index("b"))
print(str1.index("A"))
输出结果:
0
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
9. startswith(obj)
作用:startswith(obj) 主要作用是检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。
实例:
1
2str1 = "Oldboye"
print(str1.startswith("O"))
输出结果:True
10. endswith(obj)
作用:endswith(obj) 主要作用是检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。
实例:
1
2str1 = " Oldboye "
print(str1.endswith("e"))
输出结果:True
11. strip()
作用:strip() 主要作用是去除字符串前后两端的空格或其他字符、换行符、tab键等。
实例:
1
2
3
4str1 = "***Oldboy***"
print(str1.strip("*")) #去除两边的*
print(str1.lstrip("*")) #去除左边的*
print(str1.rstrip("*")) #去除右边的*
输出结果:
Oldboy
Oldboy***
***Oldboy
12. replace(oldstr, newstr)
作用:replace(oldstr, newstr)主要作用是替换字符串。
实例:
1
2str1 = "Oldboye"
print(str1.replace("boy","man"))
输出结果:Oldmane
13. isalpha()
作用:isalpha()主要作用是要判断字符串是否只由字母组成,是返回Ture,否返回False。
实例:
1
2
3
4str1 = "Oldboye"
str2 = “Old boy e”
print(str1.isalpha())
print(str2.isalpha())
输出结果:True False
14. isdigit()
作用:isdigit()主要作用是判断字符串是否只由数字组成,是返回Ture,否返回False。
实例:
1
2
3
4str1 = "Oldboye"
str2 = “520”
print(str1.isdigit())
print(str2.isdigit())
输出结果:False True
15. format()
作用:format()主要作用是格式化字符串。
方式一:按位置传参
1
2str1 = '我叫{},今年{}岁'.format('oldboy',30)
print(str1)
输出结果:我叫oldboy,今年30岁
方式二:按索引传参
1
2str1 = '我叫{0},今年{1}岁'.format('oldboy',30)
print(str1)
输出结果:我叫oldboy,今年30岁
方式三:按key传参
1
2str1 = '我叫{name},今年{age}岁'.format(age=30,name='oldboy')
print(str1)
输出结果:我叫oldboy,今年30岁
16. count()
作用:count()主要作用是统计元素在字符串出现的次数。
1
2str1 = "oldboye"
print(str1.count(‘o’)) #统计字符o在字符串中出现的次数
数据结果:2

F. C#字符串的几种常用方法

字符串是使用引号声明的,如下例所示: strings="Hello,World!"; 字符串对象是“不可变的”,即它们一旦创建就无法更改。对字符串进行操作的方法实际上返回的是新的字符串对象。因此,出于性能方面的原因,大量的连接或其他涉及字符串的操作应当用StringBuilder类执行,如下所示:System.Text.StringBuildersb=newSystem.Text.StringBuilder(); sb.Append("one"); sb.Append("two"); sb.Append("three"); stringstr=sb.ToString(); 二、C#字符串使用1、转义字符“\” 字符串中可以包含转义符,如“\n”(新行)和“\t”(制表符)。 如果希望包含反斜杠,则它前面必须还有另一个反斜杠,如“\\”。2、“@”符号 @符号会告知字符串构造函数忽略转义符和分行符。 因此,以下两个字符串是完全相同的:stringp1="\\\\MyDocuments\\MyFiles\\"; stringp2=@"\\MyDocuments\MyFiles\"; 3、ToString()如同所有从Object派生的对象一样,字符串也提供了ToString方法,用于将值转换为字符串。此方法可用于将数值转换为C#字符串,如下所示:intyear=1999; stringmsg="Evewasbornin"+year.ToString(); System.Console.WriteLine(msg);//outputs"Evewasbornin1999"另外,可以通过参数格式化ToString()的显示输出。如,对于时间类型格式,可以通过ToString()方法自定义时间显示格式。如:System.DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss.fff"); //outputs"2009-03-1118:05:16.345" //"MM":指定月份为2位字符串,不足2位则前方补"0";"M":为月份数值转换的字符串; //"HH":表示24小时制的小时;"hh"表示12小时制的小时;4、SubString() 格式:Substring(intstartindex,intlen) 用于获取源字符串指定起始位置startindex,指定长度len的字符串。 参数Startindex索引从0开始,且最大值必须小于源字符串的长度,否则会编译异常; 参数len的值必须不大于源字符串索引指定位置开始,之后的字符串字符总长度,否则会出现异常; 示例:strings4="VisualC#Express"; System.Console.WriteLine(s4.Substring(7,2));//outputs"C#" System.Console.WriteLine(s4.Replace("C#","Basic"));//outputs"VisualBasicExpress"5、Replace() 格式:Replace(stringoldValue,stringnewValue) 用于C#字符串中特定字符串组合的替换,即将源字符串中的所有oldValue字符串替换为newValue字符串。 示例:strings5="VisualC#Express"; System.Console.WriteLine(s5.Replace("C#","VB"));//outputs"VisualVBExpress"6、Split() 将字符串拆分为子字符串(如将句子拆分为各个单词)是一个常见的编程任务。Split()方法使用分隔符(如空格字符)char数组,并返回一个子字符串数组。您可以使用foreach访问此数组。 示例:char[]delimit=newchar[]{''}; strings14="Thecatsatonthemat."; foreach(stringsubstrins14.Split(delimit)) { System.Console.WriteLine(substr); } 此代码将在单独的行上输出每个单词,如下所示: The cat sat on the mat.下面的代码示例演示如何使用System.String.Split方法分析字符串。此方法返回一个字符串数组,其中每个元素是一个单词。作为输入,Split采用一个字符数组指示哪些字符被用作分隔符。本示例中使用了空格、逗号、句点、冒号和制表符。一个含有这些分隔符的数组被传递给Split,并使用结果字符串数组分别显示句子中的每个单词。示例:classTestStringSplit { staticvoidMain() { char[]delimiterChars={'',',','.',':','\t'}; stringtext="one\ttwothree:four,fivesixseven"; System.Console.WriteLine("Originaltext:'{0}'",text); string[]words=text.Split(delimiterChars); System.Console.WriteLine("{0}wordsintext:",words.Length); foreach(stringsinwords) { System.Console.WriteLine(s); } } } 输出: Originaltext:'onetwothree:four,fivesixseven' 7wordsintext: one two three four five six seven 另外,还可通过正则表达式Regex.Split()的方法,通过C#字符串分隔字符串。 示例:usingSystem.Text.RegularExpressions;//需要引用正则表达式的命名空间 stringstr="aaajsbbbjsccc"; string[]sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);//正则表达式 //RegexOptions.IgnoreCase表示忽略字母大小写 foreach(stringiinsArray)Response.Write(i.ToString()+""); 输出: aaa bbb ccc7、Trim() Trim()从当前String对象移除所有前导空白字符和尾部空白字符。示例:strings7="VisualC#Express"; System.Console.WriteLine(s7);//outputs"VisualC#Express" System.Console.WriteLine(s7.Trim());//outputs"VisualC#Express"8、ToCharArray() 格式:ToCharArray(intstartindex,intlen) 用于将字符复制到字符数组。示例:strings8="Hello,World"; char[]arr=s8.ToCharArray(0,s8.Length); foreach(charcinarr) { System.Console.Write(c);//outputs"Hello,World" } 示例:修改字符串内容字符串是不可变的,因此不能修改字符串的内容。但是,可以将字符串的内容提取到非不可变的窗体中,并对其进行修改,以形成新的字符串实例。 下面的示例使用ToCharArray方法来将字符串的内容提取到char类型的数组中。然后修改此数组中的某些元素。之后,使用char数组创建新的字符串实例。

G. String常使用的方法有哪些

这些是最常用的:
char charAt (int index) 返回index所指定的字符
String concat(String str) 将两字符串连接
boolean endsWith(String str) 测试字符串是否以str结尾
boolean equals(Object obj) 比较两对象
char[] getBytes 将字符串转换成字符数组返回
char[] getBytes(String str) 将指定的字符串转成制服数组返回
boolean startsWith(String str) 测试字符串是否以str开始
int length() 返回字符串的长度
String replace(char old ,char new) 将old用new替代
char[] toCharArray 将字符串转换成字符数组
String toLowerCase() 将字符串内的字符改写成小写
String toUpperCase() 将字符串内的字符改写成大写
String valueOf(Boolean b) 将布尔方法b的内容用字符串表示
String valueOf(char ch) 将字符ch的内容用字符串表示
String valueOf(int index) 将数字index的内容用字符串表示
String valueOf(long l) 将长整数字l的内容用字符串表示
String substring(int1,int2) 取出字符串内第int1位置到int2的字符串

=============
以下解释的十分清楚了,还有例子
1、length() 字符串的长度
例:char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();
2、charAt() 截取一个字符
例:char ch;
ch="abc".charAt(1); 返回'b'
3、 getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。因此, 子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。
例:String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);
4、getBytes()
替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。

5、toCharArray()
6、equals()和equalsIgnoreCase() 比较两个字符串
7、regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8、startsWith()和endsWith()
startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束
9、equals()和==
equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比较字符串
11、indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。
12、substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种是:String substring(int startIndex,int endIndex)
13、concat() 连接两个字符串
14 、replace() 替换
它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
15、trim() 去掉起始和结尾的空格
16、valueOf() 转换为字符串
17、toLowerCase() 转换为小写
18、toUpperCase() 转换为大写
19、StringBuffer构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得到。
(2)、ensureCapacity() 设置缓冲区的大小
void ensureCapacity(int capacity)
(3)、setLength() 设置缓冲区的长度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、append() 可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();
(7)、insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定将字符串插入到StringBuffer对象中的位置的下标。
(8)、reverse() 颠倒StringBuffer对象中的字符
StringBuffer reverse()
(9)、delete()和deleteCharAt() 删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替换
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)

H. 字符串中几个常用方法有什么作用,比如subString(a,b),equals(t),charAt(t)

这三个确实是String中非常常用的方法,以字符串对象s为例,用s调用上面三个方法,其作用如下
1. subString(a,b)作用是截取调用该方法的字符串s中第a(包含)位到第b(不包含)位的子字符串;
2. equals(t)作用是比较调用该方法的字符串s与t的大小;
3. charAt(t)作用是返回字符串t中第t个(从0开始计)位置的字符。

阅读全文

与列举字符串的常用方法并解释相关的资料

热点内容
自拍杆拍手机的方法 浏览:549
bod5分析方法名称 浏览:255
小米5无线显示在哪里设置方法 浏览:445
炖汆闷属于什么加热方法 浏览:209
激光方法治疗胃息肉有没有伤口 浏览:571
一个人转移注意力的方法有哪些 浏览:211
鱼缸除油膜最简单的方法 浏览:440
咳嗽小便失禁锻炼方法 浏览:904
简单做鱼方法 浏览:104
大小脸自我矫正方法图片集 浏览:80
从台账中快速抓取数据的方法 浏览:785
高血压的剁辽方法有哪些 浏览:95
幼儿心理发展研究最基本的方法 浏览:51
商业研究方法和人力资源管理问题 浏览:247
帆布包变黄有什么方法解决 浏览:786
轻感冒怎么办速效方法 浏览:10
焦油含量检测方法 浏览:290
草酸用什么方法能够去掉 浏览:225
红参原液的使用方法 浏览:1004
用电脑键盘关机操作方法 浏览:741