String类常用的方法
(1)int length():返回字符串的长度,例如:
String s1="hello";
System.out.println(s1.length());//显示为5
(2)char charAt(int index):获取字符串中指定位置的字符,index的取值范围是0~字符串长度-1,例如:
String s1="hello world",
System.out.println(s1.charAt(6));//显示结果为w
(3)int compareTo(String another):按Unicode码值逐字符比较两个字符串的大小。如果源串较小就返回一个小于0的值;两串相等返回0;否则返回大于0的值,例如:
System.out.println("hello".compareTo("Hello"));//显示-4
System.out.println("hello".compareTo("hello"));//显示0
System.out.println("Hello".compareTo("hello"));//显示1
(4)String concat(Striing str):把字符串str附加在当前字符串末尾,例如:
String str ="hello";
String str2="world";
System.out.println(str);//显示hello
System.out.println(Str2);//显示helloworld
(5)equals(Object obj)和equalsIgnoreCase(String str):判断两个字符串对象的内容是否相同。两者区别在于,equals()方法区分字母大小写,equalsIgnoreCase()不区分,例如:
String str1="hello";
String str2="Hello"
System.out.println(str1.equals(str2));//显示true
System.out.println(str1.equalsIgnoreCase(str2));//显示false
(6)int indexOf(int ch):返回指定字符ch在此字符串中第一次出现的位置。
int indexOf(String str):返回指定字符串str在此字符串中第一次出现的位置。
int lastIndexOf(int ch):返回指定字符ch在此字符串中最后一次出现的位置。
int lastIndexOf(String str):返回指定子字符串str在此字符串中最后一次出现的位置。
String str1="hello world";
System.out.pritnln(s1.indexOf("l"));//显示2
System.out.println(s1.indexOf("world"));//显示6
System.out.println(s1.lastIndexOf("l"));//显示9
System.out.println(s1.lastIndexOf("world"));//显示6
(7)String toUpperCase():将字符串转换为大写。
String toLowCase():将字符串转换成小写。例如:
String s1="Welcome to Java world";
System.out.println(s1.toUpperCase());//显示WELCOME TO JAVA WORLD
System.out.println(s1.toLowerCase());//显示welcome to java world
(8)String substring(int beginIndex):返回一个新字符串,改子字符串从指定索引处的字符开始,直到此字符串末尾。
String substring(int beginIndex,int endIndex):返回一个新字符串,改子字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符。例如:
String s1="Welcome to Java world";
System.out.println(s4.substring(11));//显示Java world
System.out.println(s1.substring(11,15));//显示Java
(9)static String valueOf():把基本数据类型转换为String类型,例如:
int i=123;
String s1=String.valueOf(i);
System.out.println(s1);//显示字符串123
(10)String[] split(String regex):将一个字符串按照指定的分隔符分隔,返回分隔符后的字符串数组。
String类常用的方法的更多相关文章
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- File类常用的方法与字节流类方法简介
File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...
- Java之String类常用API
目录 Java之String类常用API char chatAt(int index) int length() char[] toCharArray() String(char value[]) S ...
- String类常用的API
String类常用的API 字符串内容的比较: 注意: 不能使用 == 去比较两个字符串的内容.原理:比较的是字符串的地址. (如果两个字符串都是使用""进行赋值,那么他们都是放在 ...
- 关于JAVA的String类的一些方法
一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- Java中String类的format方法使用总结
可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...
- String类的split方法以及StringTokenizer
split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...
随机推荐
- bzoj1680[Usaco2005 Mar]Yogurt factory*&&bzoj1740[Usaco2005 mar]Yogurt factory 奶酪工厂*
bzoj1680[Usaco2005 Mar]Yogurt factory bzoj1740[Usaco2005 mar]Yogurt factory 奶酪工厂 题意: n个月,每月有一个酸奶需求量( ...
- 想用@Autowired注入static静态成员?官方不推荐你却还偏要这么做
生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...
- 计算思维(Computational Thinking)在少儿编程中的体现
本文主要针对少儿编程从业人员及正在学习编程的学生家长 大家好,我是C大叔,国内早期的少儿编程从业人员.一直以来都是在做scratch,JavaScript,python以及信息学奥赛C++的讲师,教研 ...
- pyhton 3.6 pip 出现 Fatal error in launcher: Unable to create process using 解决方法
ERROR:Fatal error in launcher: Unable to create process using '"' 出现这个 打开 终端 输入 python36 -m ...
- stm32-HAL库串口收发
串口发送 重写fputc函数 /* 优点 直接使用printf函数,发送数据长度无限制,不需要额外的数组空间 缺点 只能对应一个串口,暂时没想到解决方案 */ //头文件中要包含 stdio.h 然后 ...
- xctf-web fakebook
点join注册账号 进入view.php发现参数no存在sql注入,但是过滤了select关键字,用内联注释绕过 在users表的data字段发现了用序列化存储的用户信息 然后就卡在这里了....看了 ...
- Csrf+Xss组合拳
本文首发于“合天智汇”公众号,作者: 影子 各位大师傅,第一次在合天发文章,请多多关照 今年年初的疫情确实有点突然,打乱了上半年的所有计划(本来是校内大佬带我拿奖的时刻,没了 ,学长毕业了,就剩下我这 ...
- C语言中的 “>>”与“<<”
1. ">>" int x = 16; printf("%d\n", x >> 1); 先将x转成二进制 10000, 不读最后一位, ...
- Button基本用语
1.self.btn2 = Button(root,image = photo,command = self.login) 使用 image 图片作为按钮,command 作为响应 2.self.bt ...
- go项目dockerfile最佳实践
1. 前言 2. 不需要cgo情况下的最佳实践 3. 依赖cgo情况下的最佳实践 1. 前言 这几天在构建golang编写的web项目中,关于dockerfile编写的一些总结 可能是单纯我比较菜(大 ...