String 类的常用字符串方法
public class Page106 {
/**
* 字符串练习第五章
* @param args
*/
public static void main(String[] args) {
String str=new String(" WOrld ");
String str1=new String("w,o,r;l;d");
System.out.println(str.length());//获取长度
System.out.println(str.toUpperCase());//转为大写
System.out.println(str.toLowerCase());//转为小写
System.out.println(str.trim());//去掉首尾的空格
System.out.println(str.trim().length());
System.out.println(str.equals(str1));//比较是否相等,区分大小写
System.out.println((str.trim()).equalsIgnoreCase(str1));//不区分大小写
//查找
System.out.println(str.indexOf("or"));//返回第一次出现这个字符的位置,如果没有找到返回-1
System.out.println(str.lastIndexOf('l'));//返回最后一次出现这个字符的位置
System.out.println(str.lastIndexOf("lp",4));//从字符串第四个索引位置开始找lp最后一次出现的位置
System.out.println(str1.charAt(3));//返回索引位置为3的字符
//截取
System.out.println(str.substring(2, 4));//截取索引位置为2-4之间的字符
//分割
String[] array1=str1.split(",");
String[] array2=str1.split(";",3);//分割;标志的字符串,指定分割字符数为3,返回的字符存放在字符串数组中
for(int i=0;i<array1.length;i++){
System.out.print(array1[i]+"\t");
}
System.out.println();
for(int i=0;i<array2.length;i++){
System.out.print(array2[i]+"\t");
}
//替换
String s="hello world,hello world";
System.out.println(s.replace("world", "java"));//用java替换所有的world
System.out.println(s.replaceFirst("world", "java"));//用java只替换第一次出现的world
System.out.println(s.replaceAll("world", "java"));//用将咪表字符串匹配的正则表达式的所有子字符串替换成新的
//链接
System.out.println(s.concat(str1));//在s字符串后链接str1字符串
}
}
结果显示:
7
WORLD
world
WOrld
5
false
false
-1
4
-1
,
Or
w o r;l;d
w,o,r l d hello java,hello java
hello java,hello world
hello java,hello java
hello world,hello worldw,o,r;l;d
String 类的常用字符串方法的更多相关文章
- String类中常用的方法
@Test public void demo(){ // 以下为String中的常用的方法及注释, 最常用的注释前有**标注 String s = "abcdefg123456"; ...
- String类的常用判断方法使用练习
选取了一些常用的判断方法进行了使用练习,后续跟新其他方法 package StringDemo; // String类的判断方法解析 // 1:boolean equals(); // 判断字符串是否 ...
- String类中常用的方法(重要)
1.字符串与字节 public String(byte[] byte); 将全部字节变成字符串 public String (byte[] byte,int offset,int length) 将部 ...
- Java中String类中常用的方法
1.字符串与字符数组的转换 用toCharArray()方法将字符串变为字符数组 String str = "abcdef"; char c[] = str.tocharArray ...
- 27 string类中常用的方法列表
1. 获取方法 int length() 获取字符串的长度 char charAt(int index) 获取特定位置的字符 (角标越界) int indexOf(String str) 获取 ...
- 2017.12.11 String 类中常用的方法
1.编写程序将 "jdk" 全部变为大写,并输出到屏幕,截取子串"DK" 并输出到屏幕 package demo; import java.util.Scann ...
- Java String类中的intern()方法
今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- js中关于string的一些常用的方法
最近总结了一些关于string中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. ...
随机推荐
- iOS 上拉刷新和下拉加在更多(第三方框架EGOTableViewPullRefresh)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- websocket 待更新
https://mp.weixin.qq.com/s?__biz=MjM5OTM0MzIwMQ==&mid=2652545551&idx=1&sn=403b75d95cf191 ...
- mysql 启动服务
http://blog.chinaunix.net/uid-13642598-id-3153537.html mysql的四种启动方式: 1.mysqld 启动mysql服务器:./mysqld -- ...
- 连接sql server数据库的两种方式
class DB { private static SqlConnection conn; public static SqlConnection getConn() { //conn = n ...
- 一个新人对于JavaScript简单应用的理解
JavaScript 1.输出:document.write("hello,world"); document.write的意思就是给我再此页面中显示出什么什么小括号里面的内 ...
- html 鼠标移入标签 显示小手指
<div style="width: 200px; height: 200px; background: red; cursor: pointer;"></div ...
- C++之路进阶——codevs4416(FFF的后宫)
4416 FFF 团卧底的后宫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 你在某日收到了 FFF 团卧底的求 ...
- JAVA面试题之实现字符串的倒序输出
package shb.java.demo; public class MyTest { public static void main(String[] args) { String string ...
- paper 53 :深度学习(转载)
转载来源:http://blog.csdn.net/fengbingchun/article/details/50087005 这篇文章主要是为了对深度学习(DeepLearning)有个初步了解,算 ...
- andriod之应用内置浏览器 webview
参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...