String类提供处理字符串的方法
2009-07-24 11:50
package linhui1;
public class P3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "welcome to java";
String s2=s1;
String s3=new String("welcome to java");
String s4=s3.intern();
System.out.println(s1==s2);//1
System.out.println(s1==s3);//2
System.out.println(s1.equals(s2));//3
System.out.println(s2.equals(s3));//4
System.out.println(s1.compareTo(s2));//5
System.out.println(s2.compareTo(s3));//6
System.out.println(s1==s4);//7
System.out.println(s1.charAt(0));//8
System.out.println(s1.indexOf('j'));//9
System.out.println(s1.indexOf("to"));//10
System.out.println(s1.lastIndexOf('a'));//11
System.out.println(s1.lastIndexOf("o",15));//12
System.out.println(s1.length());//13
System.out.println(s1.substring(5));//14
System.out.println(s1.substring(5,11));//15
System.out.println(s1.startsWith("welc"));//16
System.out.println(s1.endsWith("java"));//17
System.out.println("welcome".trim());//18
System.out.println(s1.toLowerCase());//19
System.out.println(s1.toUpperCase());//20
System.out.println(s1.replace('o','T'));//21
System.out.println(s1.replaceFirst("o", "T"));//22
System.out.println(s1.replaceAll("o","T"));//23
System.out.println(s1.toCharArray());
}
}
结果:
true //1
false //2
true //3
true //4
0 //5
0 //6
true //7
w //8
11 //9
8 //10
14 //11
9 //12
15 //13
me to java //14
me to //15
true //16
true //17
welcome //18
welcome to java //19
WELCOME TO JAVA //20
welcTme tT java //21
welcTme to java //22
welcTme tT java //23
welcome to java //24
1.通常,在一个程序中需要比较两个字符串的内容,可以尝试使用运算符==,然而,==运算符只能检测s2和s3是否指向同一个对象,不能判断是否
具有相同的内容。因而,不能使用==运算符判断两个字符串是否具有相同的内容。取而代之,可以使用equals()方法对对象的内容进行比较。
2.可以通过调用length()方法得到字符串的长度。方法s.charAt(index)可用于提取字符串s中的一个指定字符,其中index在0到s.length()-1之间。
3.可以使用charAt方法从字符串中提取单字符,使用String类中的substring方法,也可以从字符串中提取字串。substring方法有两个版本:
a. public String substring (int beginIndex,int endIndex)返回一个新的字符串,它也是改字符串的字串。字串从指定的下标的字符下标开始,扩展到字符串为endIndex-1结束的字符。
b. public String substring (int beginTndex) 一直扩展到字符串的结尾。
4. 利用indexOf()和lastIndexOf方法可以在字符串中得到一个字符串或一个字串。String类中定义了4个重载的indexOf方法和4个重载的indexOf方法。
public int indexOf(int ch) (public int lastIndexOf(int ch))
public int indexOf(int ch,int formIndex) (public int lastIndexOf(int ch,int endIndex))
pubilc int indexOf(String str) (public int lastIndexOf(String str))
public int indexOf(String str,int fromIndex) (public int lastIndexOf(String str,int endIndex))
5.字符串不是数组,但是字符串能够转换成数组,份儿亦然。使用toCharArray方法可以将字符串转换成一个字符的数组。
如:char [] chars="java".toCharArry();
因此,chars[0]为'j' ,chars[1]为'a',chars[2]为'v',chars[3]为'a'。
分享到:
相关推荐
在C++中,std::string 类提供了一系列用于处理字符串的方法。以下是一些 std::string 类的常用方法及其实例
Delphi中处理字符串的相关方法 1、字符集转换方法 (1)stringtowidechar function stringtowidechar(const source:string;dest :pwidechar;destsize :integer) :pwidechar; 将默认string类型的字符串转换为unicode...
自己整理的StringUtil ,字符串处理工具类,很全面的方法,对象之间的数据转换
在第一版的基础上做了改进,规范了编码和命名以及注释。添加了一些函数
Mid Mid(string,start,length) 从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 Left Left(string,length) 从string字符串的左边取得length长度...
将string类型的xml转为JSON字符串,利用JSONObject处理JSON串
like-string 简单的相似字符串处理库
string类的常用方法
/// 字符串分割方法. /// /// <param name="source">The source. /// <param name="keyword">The keyword. /// <returns>List<System.String>. public static List<string> StringSplit(string source,...
字符串操作类,与VC CString类接口差不多,线程安全,跨平台,已经在windows 下VC和linux下g++编译通过
Test1 介绍的是字符串获取长度截取的问题 Test2 介绍的是字符串之间的比较问题 Tes3 介绍的字符串之间的分割 Tes4 介绍格式化字符串,日期和时间 Test5 介绍使用正则表达式 Test6 字符串生成器
c++封装的String类,采用了预分配内存和Copybeforewrite技术。效率很高。
适合刚刚进入(JavaScript,Vue)编程领域的小白使用
string类的常用方法 std::string 是 C++ 标准库中用于处理字符串的类,提供了丰富的成员函数和操作符来进行字符串的操作。
java字符串处理取出括号内的字符串 都是我自己试过可以用的j
无论是用户输入、文件读取还是网络传输,字符串操作无处不在。掌握String类的常用方法,对于提高编程效率和代码质量至关重要。本篇博文将带你深入了解String类的核心方法,并通过实例演示它们的用法。
string字符串处理,包括运算符重载、字符串拼接、倒序输出等。
unique-string:生成一个唯一的随机字符串
将输入的字符串解析为16进制输出
C# 是一种强类型、面向对象的编程语言,在处理字符串方面具有很多优势,使得字符串操作更加高效和方便。以下是 C# 处理字符串的一些优势: 1. **丰富的字符串处理方法**:C#提供了丰富的字符串处理方法,如 `...