Day10 API
String类
String是不可变类:值一旦确定了,就不会更改。
public static void main(String[] args) {
String s1 = "hello";//到常量池里找对象
String s2 = new String("hello");
s1 = "Tom";
s1 = "hello"+"Tom";
String s3 = "hello";
String s4 = new String("hello");
// ==判断是否为同一对象,判断内存地址
System.out.println(s1==s2);//false
System.out.println(s2==s4);//false
System.out.println(s1==s3);//false
s1 = "hello";
System.out.println(s1==s3);//true
}
String常用的方法
concat
字符串连接,返回连接后的串
String s1 = "hello";
//concat 字符串连接,返回连接后的串
s1 = s1.concat("tom");
System.out.println(s1);//hellotom
length
字符串长度
System.out.println(s1.length());
equals
比较字符序列是否相同,区分大小写
String s2 = "hellotom";
System.out.println(s1.equals(s2));
equalsIgnoreCase
比较字符序列是否相同,不区分大小写
String s3 = "HEllotom";
System.out.println(s1.equalsIgnoreCase(s3));
toUpperCase
大写兼容,全部转为大写
System.out.println(s1.toUpperCase());
toLowerCase
小写兼容,全部转为小写
System.out.println(s1.toLowerCase());
indexOf
首次出现的位置索引,没有返回-1
s1 = "hellohellotom";
System.out.println(s1.indexOf("hello"));
System.out.println(s1.indexOf("abc"));
lastIndexOf
最后一次出现的位置索引
System.out.println(s1.lastIndexOf("hello"));
charAt
取出某一个位置的字符
System.out.println(s1.charAt());//h
substring
取字符串
//取子串(起始位置)取到最后
System.out.println(s1.substring());//tom
//[起始位置,终止位置) 不包括终止位置
System.out.println(s1.substring(, ));//tom
trim
去除字符串的首尾空格
s1 = " h e l l o t o m ";
System.out.println(s1);// h e l l o t o m
System.out.println(s1.trim());//h e l l o t o m
replace
字符串替换(旧串,新串)用新串替换旧串
s1 = "hellotom";
System.out.println(s1.replace(s1, "你好"));//你好
//去掉是s1串中的所有空格
s1 = " h e l l o t om ";
System.out.println(s1.replace(" ", ""));//hellotom
endsWith
判断是否以某个字符串结尾,是true,否false
s1 = "hello.java";
System.out.println(s1.endsWith("java"));//true
startsWith
判断是否以某个字符串开头,是true,否false
System.out.println(s1.startsWith("he"));//true
compareTo
字符串比较大小
s1 = "abc";
//unicode s1在参数之前,返回负数,s1在参数之后,返回正数,相对返回0
System.out.println(s1.compareTo("cc"));//-2
System.out.println(s1.compareTo("aa"));//
System.out.println(s1.compareTo("abc"));//
contains
是否包含指定参数的字符串,包含true,不包含false
System.out.println(s1.contains("bc"));//true
toCharArray()
把字符串转换成字符数组
char[] c = s1.toCharArray();//'a','b','c'
for(char cc:c) {
System.out.print("字符:"+cc);//字符:a字符:b字符:c
}
split
用某个字符串把原始字符串分割为一个字符串数组
s1 = "aa bb cc dd ee";
String[] strs = s1.split(" ");//"aa","bb","cc","dd","ee"
for(String ss : strs) {
System.out.print("元素:"+ss);//元素:aa元素:bb元素:cc元素:dd元素:ee
}
public static void main(String[] args) {
// String 常用的方法
String s1 = "hello";
/* System.out.println(s1);//hello
s1 = s1 + "tom";// "hellotom"
*/
//连接字符串,返回连接后的串
s1 = s1.concat("tom");//"hellotom"
System.out.println(s1);//"hellotom"
//字符串长(字符序列的个数)
System.out.println(s1.length());//8
//equals比较字符序列是否相同 相同 true 区分大小写
String s2 = "hellotoM";
System.out.println(s1.equals(s2));//fasle
//忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));
//大小写兼容
//大写
System.out.println(s1.toUpperCase());
//小写
System.out.println(s1.toLowerCase());
// zhangsan@163.com
// 0123------------------------------------
s1 = "hellohellotom";
//位置索引
//首次出现的位置索引 ,没有返回 -1
System.out.println(s1.indexOf("hello"));//
System.out.println(s1.indexOf("@"));//-1
//最后一次出现的位置索引
System.out.println(s1.lastIndexOf("hello"));//5
//取出 某一个 位置 的字符
System.out.println(s1.charAt());//'h'
//取 子串 (起始位置) 取到最后
System.out.println(s1.substring());//"tom"
//[起始位置,终止位置) 不包括终止位置
System.out.println(s1.substring(, ));
//----------------------------------------
s1 = " h e l l o tom ";
//去除字符串 的 首尾 空格
System.out.println(s1.trim());
//
s1 = "hellotom";
//字符串 替换 (旧串,新串) 用新串 替换 旧串
System.out.println(s1.replace("hello","你好"));//"hello"->“你好”
s1 = " h e l l o t om ";
//问题:去掉 s1串 中的 所有空格
System.out.println(s1.replace(" ", ""));
//
s1 = "hello.java";
//判断 是否 以 某个 字符串 结尾 是 true
System.out.println(s1.endsWith("java"));//true
//判断 是否 以 某个 字符串 开头 是 true
System.out.println(s1.startsWith("he"));//true
//字符串 比较大小
s1 = "abc";
//unicode s1 在 参数之前 ,返回 负数 ,s1 在参数后 ,返回正数,相等 返回0
System.out.println(s1.compareTo("cc"));//-2
System.out.println(s1.compareTo("aa"));//
System.out.println(s1.compareTo("abc"));//0
//-----------------------------------------
//是否 包含 指定参数 的字符串 ,包含 true
System.out.println(s1.contains("ab"));
//把 字符串 转换成字符数组
char [] c = s1.toCharArray();// 'a','b','c'
for(char cc :c) {
System.out.println("字符:"+cc);
}
//
s1 = "aa bb cc dd ee";
//用某个 字符串 把原始字符串 分割为 一个 字符串数组
String [] strs = s1.split(" ");//"aa","bb","cc","dd","ee"
for(String ss:strs) {
System.out.println("元素:"+ss);
} }
StringBuffer类
如果频繁更改字符串的值,不要用String类,效率低,用变长字符串类StringBuffer和StringBuilder类
StringBuffer类常用方法
capacity
返回容量
StringBuffer sf1 = new StringBuffer();
//具备16个字符的缓冲区
System.out.println(sf1.capacity());//
StringBuffer sf2 = new StringBuffer("hello");
System.out.println(sf2.capacity());//21=16+5
//直接规定缓冲区大小
StringBuffer sf3 = new StringBuffer();
System.out.println(sf3.capacity());//
apend
追加字符串
StringBuffer sf = new StringBuffer();
sf.append("hello");
System.out.println(sf);//hello
char[] c = {'a','b','c'};
sf.append(c,,);//bc
System.out.println(sf);//hellobc
trimToSize
缩小容量为我存储的字符的个数大小
System.out.println(sf.capacity());//
sf.trimToSize();
System.out.println(sf.capacity());//
insert
向索引位置插入一个字符串
System.out.println(sf);//hellobc
sf.insert(, "你好");
System.out.println(sf);//hello你好bc
setCharAt
修改指定位置的一个字符
sf.setCharAt(, '您');
System.out.println(sf);//hello您好bc
deleteCharAt
删除指定位置索引的一个字符
sf.deleteCharAt();
System.out.println(sf);//hello好bc
delete
删除指定范围的字符串[起始位置,终止位置),不包括终止位置
sf.delete(, );
System.out.println(sf);//hello
chatAt
获取指定索引处的字符
System.out.println(sf.charAt());//e
indexOf
首次出现的位置索引,没有返回-1
System.out.println(sf.indexOf("l"));//
lastIndexOf
最后一次出现的位置索引
System.out.println(sf.lastIndexOf("l"));//
reverse
反转
System.out.println(sf);//hello
sf.reverse();
System.out.println(sf);//olleh
String类与StringBuffer类之间的转换
String str = sf.toString();
StringBuffer sfr = new StringBuffer(str);
public static void main(String[] args) {
// StringBuffer StringBuilder
//String 不可变类
//如果频繁 更改字符串的值,不要用String效率低,用 变 长字符串类 StringBuffer StringBuilder
//StringBuffer
StringBuffer sf1 = new StringBuffer();
//具备 16个字符的缓冲区
System.out.println(sf1.capacity());//
StringBuffer sf2 = new StringBuffer("hello");
//容量
System.out.println(sf2.capacity());//
sf2.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
System.out.println(sf2.capacity());// StringBuffer sf3 = new StringBuffer();
System.out.println(sf3.capacity()); //----------------StringBuffer--------------------------------
StringBuffer sf = new StringBuffer();
//容量
System.out.println(sf.capacity());
//追加字符串的
sf.append("hello");
char[] c = {'a','b','c'};
sf.append(c, , );//bc
System.out.println(sf);//"hellobc" System.out.println(sf.capacity());
//缩小容量为 我 存储的字符的个数大小
sf.trimToSize();
System.out.println(sf.capacity());
System.out.println(sf);//"hellobc"//
//向 索引 位置 插入一个字符串
sf.insert(, "你好");//"hello你好bc"
System.out.println(sf);
//修改 指定位置的一个 字符
sf.setCharAt(, '您');//"hello您好bc"
System.out.println(sf);
//删除 指定 位置索引的 一个 字符
sf.deleteCharAt();//"hello好bc"
System.out.println(sf);
//删除 指定 范围 的字符串 【起始位置,终止位置)不包括 终止位置
sf.delete(, );
System.out.println(sf);//"hello" System.out.println(sf.charAt());//e
System.out.println(sf.indexOf("l"));//
System.out.println(sf.lastIndexOf("l"));//3 //------------其它----------------------
sf.reverse();
System.out.println(sf);
String str = sf.toString();//StringBuffer -> String
//String - > StingBuffer
StringBuffer sfr = new StringBuffer(str);
//-----------------------------------------------------
/*
* String:定长字符串类,不可变类,表示一个简单的字符串 用 String
* 但是,如果 字符串的值大量更改频繁更改 ,选择StringBuffer,StringBuilder
* StringBuffer :线程安全的,数据准确,但速度慢
* StringBuilder:线程非安全的,多线程环境下数据不准确,但是速度快。
*/
//--------------------------------------------------
}
String、StringBuffer和StringBuilder之间的区别
String:定长字符串类,不可变类,表示一个简单的字符串用String,但是,如果字符串的值大量更改频繁,选择StringBuffer或StringBuilder
StringBuffer:线程安全的,数据准确,但速度慢
StringBuilder:线程非安全的,多线程环境下数据不准确,但是速度快
正则表达式
用某种模式去匹配指定字符串的一种表达方式。
语法
定义正则表达式:Pattern.compile(regString,)
表达式的模式:Matcher matcher = p.matcher();
验证:matcher.matches()
public static void main(String[] args) {
//正则验证:邮政编码必须6位
//1.指定正则表达式[0-9]{6}或\\d{6}
Pattern p =Pattern.compile("[0-9]{6}");
//2.指定要验证的数据
Matcher m = p.matcher("");
//3.验证
System.out.println(m.matches());//格式验证true
}
拆箱、装箱
JDK提供了对所有的基本数据类型的包装类
作用
1.把基本类型当做对象使用
2.提供了更加丰富的功能。
Day10 API的更多相关文章
- ##DAY10 UITableView基础
##DAY10 UITableView基础 UITableView继承于UIScrollView,可以滚动. UITableView的每⼀条数据对应的单元格叫做Cell,是UITableViewCel ...
- day10(java web之request&respone&访问路径&编码问题)
day10 请求响应流程图 response response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletR ...
- Elasticsearch--集群管理_别名&插件&更新API
目录 使用索引别名 别名 创建别名 修改别名 合并命令 获取所有别名 移除别名 别名中过滤 别名和路由 Elasticsearch插件 基础知识 安装插件 移除插件 更新设置API 使用索引别名 通过 ...
- 学习日常笔记<day10>servlet编程
1 如何开发一个Servlet 1.1 步骤: 1)编写java类,继承HttpServlet类 2)重新doGet和doPost方法 3)Servlet程序交给tomcat服务器运行!! 3.1 s ...
- 383 day10缓冲流、转换流、序列化流
day10[缓冲流.转换流.序列化流] 主要内容 缓冲流 转换流 序列化流 打印流 教学目标 [ ] 能够使用字节缓冲流读取数据到程序 [ ] 能够使用字节缓冲流写出数据到文件 [ ] 能够明确字符缓 ...
- 干货来袭-整套完整安全的API接口解决方案
在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...
- 12306官方火车票Api接口
2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...
- 几个有趣的WEB设备API(二)
浏览器和设备之间还有很多有趣的接口, 1.屏幕朝向接口 浏览器有两种方法来监听屏幕朝向,看是横屏还是竖屏. (1)使用css媒体查询的方法 /* 竖屏 */ @media screen and (or ...
- html5 canvas常用api总结(三)--图像变换API
canvas的图像变换api,可以帮助我们更加方便的绘画出一些酷炫的效果,也可以用来制作动画.接下来将总结一下canvas的变换方法,文末有一个例子来更加深刻的了解和利用这几个api. 1.画布旋转a ...
随机推荐
- An internal error occurred during: "Updating status for Tomcat v7.0 Server at localhost..."
tomcat启动maven工程的时候提示如下错误信息: An internal error occurred during: "Updating status for Tomcat v7.0 ...
- 悟空模式-java-单例模式
[那座山,正当顶上,有一块仙石.其石有三丈六尺五寸高,有二丈四尺围圆.三丈六尺五寸高,按周天三百六十五度:二丈四尺围圆,按政历二十四气.上有九窍八孔,按九宫八卦.四面更无树木遮阴,左右倒有芝兰相衬.盖 ...
- JSP学习笔记(4)-Javabean
按照sun公司的定义,Javabean是一个可重复使用的软件组件,实际上Javabean是一种Java类,通过封装属性和方法成为具有某种功能或处理某个业务的对象,简称Bean,Javabean基于ja ...
- Android开发之旅5:应用程序基础及组件
引言 上篇Android开发之旅:应用程序基础及组件介绍了应用程序的基础知识及Android的四个组件,本篇将介绍如何激活组关闭组件等.本文的主题如下: 1.激活组件:意图(Intents) 1.1. ...
- mysql 删除某一个数据库下面的所有表,但不删除数据库
删除某一个数据库下面的所有表,但不删除数据库.该语句经过从concat拼接,最后查询出来的是删除表的语句,然后执行那些查询出来的语句就ok了select concat(‘drop table ‘,ta ...
- display:inline-block居中方式
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- python学习笔记之——文件I/O
1.print打印到屏幕 print "打印到屏幕" 2.读取读取键盘输入 (1)raw_input函数 raw_input([prompt]) 函数从标准输入读取一个行,并返回一 ...
- iphone精简教程
那么教程开始 首先讲一下到底什么是精简 精简,就是把iphone4里面没用的自带软件和一些没用的东西删除 比如说股票,facetime,itunes store这些从来不用的东西,把这些东西删除从而使 ...
- maven 远程仓库、私服及镜像配置
maven仓库分类 本地仓库.远程仓库.远程仓库又有私服.中央仓库.其它公共库.中央仓库是maven自带的核心仓库. 仓库配置远程仓库可以配置多个,超级pom中定义的中央仓库 <reposito ...
- 如何在微软Windows平台上打造出你的Linux开发环境(转载)
如何在微软Windows平台上打造出你的Linux开发环境 投递人 itwriter 发布于 2013-12-10 11:18 评论(1) 有348人阅读 原文链接 [收藏] « » 英文原文: ...