String类
2016-04-30 21:26
225 查看
第十二章 常用类
String类
- 1 String类常用的重要方法
-
----------
1.public int leng()//当前字符串的长度
2.public char charAt(int index)//返回在指定index位置的字符,index(位置)从0开始
3.public char[] toCharArray()//将当前字符串转换成数组
4.public int indexOf(int ch)//返回指定字符ch在当前字符串第一次出现的索引(位置)
eg:String st = "ssdghh"; int n = st.("d");
5.public int lastIndexof(int ch)//返回指定字符在当前字符串最后一次出现的索引
6.public String trim()//返回当前字符串的副本,去掉当前字符串前后的空格
7.public boolean startWith(String s)//判断当前字符串是否以s字符开始
8.public boolean endsWith(String s)//判断当前字符串是否以s字符结束
9.public boolean contains(String s)//判断当前字符串是否包含字符串s
10.public String concat(String str)//将当前字符串与str字符串拼接
11.public String substring(int beginIndex, int endIndex)//返回截取索引范围内的字符作为新的字符串,前闭后开,即不会截取到endIndex的字符
12.public int compareTo(String1 String2)//按字典顺序比较两个字符串,将String1中的字符与String2的字符一个个的比较,当字符不相同时,相减,得到的值就是返回值,可能为负
13.public int compareToIgnoreCase(String str)//忽略大小写比较两数组,相等返回0,
其他返回正数和负数
** 14.public String[] split(String regex)//根据正则表达式将字符串拆分成数组
** 15.public boolean matches(String regex)//判断字符串是否符合正则表达式,返回
true /false
----------
- 2 正则表达式(regex)
-
1.任何一个字符串都是正则表达式
2.模糊符号:[],{},()
1. [] 代表一个字符,在内部书写有效的选择 eg:[0-9,a-z],表示内部的选择是数字0到9,字母是a到z
2. {} 表示前面表达式出现的次数,{m,n}表示表达式至少出现m次,最多n次,{m,}表达式至少出现m次,{m}只出现m次
3.()与|配合,(|)表示可选择的表达式,要么|前的表达式,要么|后面的表达式
eg:if input matches(0000|1[0-9]{3}|[1-9][0-9]{0-2}00){
inputMoney = Integer.parseInt(input);
---------- }
----------
3 String StringBuffer StringBuilder的区别
String是不可变的字符串 , StringBuffer StringBuilder是可变的字符串,
StringBuffer的方法:
public StringBuffer append(char c)在字符串后面添加新的字符串
eg:StringBuffer sb = new StringBuffer("hello");
sb.append("xx")
1. 三者的性能 :StringBuilder〉 StringBuffer〉String
2. Builder 适用于单线程,不考虑线程安全的话,执行效率高
3. Buffer 适用于多线程,设计为线程安全,但执行效率低
4. 不考虑线程安全情况下,如果要创建一个内容可变的字符串对象,优先考虑使用Builder
-
-
- 4 包装类 -- 在基本数据类型和引用数据类型间作为桥梁
- 1.基本与包装类的转换
- 基本转包装类对象
- eg: int a = 10;
- Integer i = new Integer(a);//调用包装类的带参构造
- Integer i = a;//自动封箱
- 包装类对象转基本
- eg:Integer i = 100;
- int a = i;//自动拆箱
- int a = i.intvalue();//调用包装类对象的intvalue()方法
- 2.包装类与String的转换
- String转包装类对象
- eg:String str = "300";
- Integer i = new Intger(str);//调用包装类的String参数构造
- 包装类转String
- eg:Integer i = 200;
- String str = i.toString();//调用包装类的toString()方法
- **** 3.基本与String的转换
- 基本转String
- eg:int i = 400;
- String str = Integer.toString(i);//用包装类的带参toString()方法
- String转基本
- eg:String str = "250";
- int i = Integer.parseInt(str);//用包装类的parseInt()方法
- ####### parse后面可以跟double 或者int
-
-
- 5 Date类
取当前日期,通常使用Date;
eg:Date date = new Date();
使用SimpleDateFormat类进行格式化输出SimpleDateFormat
eg:SimpleDateFormat sdf = new SimpleDateFormat("y-m-d h:f:m")
String dateMsg = sdf.format(date);
Calendar类: 当涉及到具体某个时间设置或获取时使用
eg:Calender cal = Carlendar.getInstance();
cal.set(2016,4,25)
cal.get(DAY-OF-YEAR)//得到某年的第几天
- 6 Properties(属性)-
特点:1.集合类
集合类的特点:1.数据在内部按键值对的方式出现
2.可存放Object类型,但操作文件时都被当作String
3.可变大变小
4.存放不连续
2 . 可操作文件-配置文件(属性文件和XML文件)
-
- 3. Properties对数据的操作
- eg:Properties props = new Properties();
- Properties setProperty("userName","zhang3")//存数据
- String value = props.getProperty("password");//读数据
- props.setProperty("password","6524");//改数据
- props.remove("address")//根据键值移除
- int size = props.size();//得到容器中数据的数量
4. Properties对文件的操作
把数据写入文件
eg: props.store(new FileOutputStream("data. properties","这是用户信息"));//把数据写入文件
把数据存入对象
eg:Properties newProps = new Properties();
newProps.load(new FileInputStream("data.Properties"));
-
-
-
String类
- 1 String类常用的重要方法
-
----------
1.public int leng()//当前字符串的长度
2.public char charAt(int index)//返回在指定index位置的字符,index(位置)从0开始
3.public char[] toCharArray()//将当前字符串转换成数组
4.public int indexOf(int ch)//返回指定字符ch在当前字符串第一次出现的索引(位置)
eg:String st = "ssdghh"; int n = st.("d");
5.public int lastIndexof(int ch)//返回指定字符在当前字符串最后一次出现的索引
6.public String trim()//返回当前字符串的副本,去掉当前字符串前后的空格
7.public boolean startWith(String s)//判断当前字符串是否以s字符开始
8.public boolean endsWith(String s)//判断当前字符串是否以s字符结束
9.public boolean contains(String s)//判断当前字符串是否包含字符串s
10.public String concat(String str)//将当前字符串与str字符串拼接
11.public String substring(int beginIndex, int endIndex)//返回截取索引范围内的字符作为新的字符串,前闭后开,即不会截取到endIndex的字符
12.public int compareTo(String1 String2)//按字典顺序比较两个字符串,将String1中的字符与String2的字符一个个的比较,当字符不相同时,相减,得到的值就是返回值,可能为负
13.public int compareToIgnoreCase(String str)//忽略大小写比较两数组,相等返回0,
其他返回正数和负数
** 14.public String[] split(String regex)//根据正则表达式将字符串拆分成数组
** 15.public boolean matches(String regex)//判断字符串是否符合正则表达式,返回
true /false
----------
- 2 正则表达式(regex)
-
1.任何一个字符串都是正则表达式
2.模糊符号:[],{},()
1. [] 代表一个字符,在内部书写有效的选择 eg:[0-9,a-z],表示内部的选择是数字0到9,字母是a到z
2. {} 表示前面表达式出现的次数,{m,n}表示表达式至少出现m次,最多n次,{m,}表达式至少出现m次,{m}只出现m次
3.()与|配合,(|)表示可选择的表达式,要么|前的表达式,要么|后面的表达式
eg:if input matches(0000|1[0-9]{3}|[1-9][0-9]{0-2}00){
inputMoney = Integer.parseInt(input);
---------- }
----------
3 String StringBuffer StringBuilder的区别
String是不可变的字符串 , StringBuffer StringBuilder是可变的字符串,
StringBuffer的方法:
public StringBuffer append(char c)在字符串后面添加新的字符串
eg:StringBuffer sb = new StringBuffer("hello");
sb.append("xx")
1. 三者的性能 :StringBuilder〉 StringBuffer〉String
2. Builder 适用于单线程,不考虑线程安全的话,执行效率高
3. Buffer 适用于多线程,设计为线程安全,但执行效率低
4. 不考虑线程安全情况下,如果要创建一个内容可变的字符串对象,优先考虑使用Builder
-
-
- 4 包装类 -- 在基本数据类型和引用数据类型间作为桥梁
- 1.基本与包装类的转换
- 基本转包装类对象
- eg: int a = 10;
- Integer i = new Integer(a);//调用包装类的带参构造
- Integer i = a;//自动封箱
- 包装类对象转基本
- eg:Integer i = 100;
- int a = i;//自动拆箱
- int a = i.intvalue();//调用包装类对象的intvalue()方法
- 2.包装类与String的转换
- String转包装类对象
- eg:String str = "300";
- Integer i = new Intger(str);//调用包装类的String参数构造
- 包装类转String
- eg:Integer i = 200;
- String str = i.toString();//调用包装类的toString()方法
- **** 3.基本与String的转换
- 基本转String
- eg:int i = 400;
- String str = Integer.toString(i);//用包装类的带参toString()方法
- String转基本
- eg:String str = "250";
- int i = Integer.parseInt(str);//用包装类的parseInt()方法
- ####### parse后面可以跟double 或者int
-
-
- 5 Date类
取当前日期,通常使用Date;
eg:Date date = new Date();
使用SimpleDateFormat类进行格式化输出SimpleDateFormat
eg:SimpleDateFormat sdf = new SimpleDateFormat("y-m-d h:f:m")
String dateMsg = sdf.format(date);
Calendar类: 当涉及到具体某个时间设置或获取时使用
eg:Calender cal = Carlendar.getInstance();
cal.set(2016,4,25)
cal.get(DAY-OF-YEAR)//得到某年的第几天
- 6 Properties(属性)-
特点:1.集合类
集合类的特点:1.数据在内部按键值对的方式出现
2.可存放Object类型,但操作文件时都被当作String
3.可变大变小
4.存放不连续
2 . 可操作文件-配置文件(属性文件和XML文件)
-
- 3. Properties对数据的操作
- eg:Properties props = new Properties();
- Properties setProperty("userName","zhang3")//存数据
- String value = props.getProperty("password");//读数据
- props.setProperty("password","6524");//改数据
- props.remove("address")//根据键值移除
- int size = props.size();//得到容器中数据的数量
4. Properties对文件的操作
把数据写入文件
eg: props.store(new FileOutputStream("data. properties","这是用户信息"));//把数据写入文件
把数据存入对象
eg:Properties newProps = new Properties();
newProps.load(new FileInputStream("data.Properties"));
-
-
-
相关文章推荐
- org.json.JsonObject的put和append方法比较
- lightOj--1261--K-SAT Problem(STL)
- 1054. 求平均值 (20)
- Qt之信号与槽机制
- 单列模式
- SDUT 2408:Pick apples
- 把web项目改造为maven项目
- Linux 查看 系统正在监听的端口的进程
- 个人工作总结08
- Linux内核总结
- 【GOF】代理模式之动态代理
- 回炉重铸-设计模式的领悟
- leetcode 101. Symmetric Tree 判断对称树,递归和迭代
- NYOJ 791 Color the fence
- iOS笔记----Hybrid应用基本核心
- 个人工作总结07
- SDUT 2416:Fruit Ninja II
- HDU-4496 D-City (并查集)
- Android NFC
- Qt多线程程序设计