黑马程序员——常用API
2014-10-16 19:57
274 查看
-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
今天重点看了String类,不断的通过查找API文档来实现一些功能。以前一直把String当成基本数据类型用,现在比较全面的了解了String类。
String类:
String是一个特殊的对象,一旦被初始化,就不会被改变。()指的是abc 不是变量 s1.
String s1="abc"; s1是一个类类型变量,“abc”是一个对象。
String s2=new String("abc");
s1和s2的区别:
s1在字符串常量池中创建了一个abc字符串
s2在堆中创建了两个对象一个是默认对象一个是字符串对象。
==比较的是地址,equals比较的是内容。
下面是我自己查找并且使用的String类。
package com.itcast.test1_1;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "afdaAdasfda";
/*
* sop(s.length()); sop(s.charAt(3));
*/
/*
* sop(s.indexOf('d')); sop(s.indexOf("das")); sop(s.indexOf('d',4));
* sop(s.indexOf("das", 3)); sop(s.substring(4)); sop(s.substring(2,
* 9));
*/
/*
* String s1="["; sop(s1); for (int i = 0; i < s.length(); i++) {
* if(i!=(s.length()-1)){ sop(s.charAt(i)+","); }else{
* sop(s.charAt(i)+"]");
*
* } }
*/
char[] arr={'a','b','c','d','e','f'};
String s1=new String(arr,1,3);
sop(s1);
String s2="dfasdgasaswe";
char[] chs=s2.toCharArray();
sop(chs[3]);
for (int i = 0; i < chs.length; i++) {
sop(chs[i]);
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
下面关于一些重要的API文档
1.1 获取字符串长度
int length();
1.2 根据位置获取字符
char charAt(int index);
1.3 根据字符获取在字符中的位置
int indexof(int ch) 返回的是ch在字符串中第一个出现的位置
int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int indexof(String str); 返回的是str在字符串中第一个出现的位置
int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
反响索引一个字符出现的位置。
int lastindexof(int ch) 返回的是ch在字符串中第一个出现的位置
int lastindexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int lastindexof(String str); 返回的是str在字符串中第一个出现的位置
int lastindexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
1.4获取字符串中的一部分字符串,也叫子串。
String subString(int beginindex,intendindex) ;
String subString(int beginindex)
2.判断
2.1两个字符串是否相同
equals(Object obj)
equalsIgnoreCase(String str)
2.2字符串中是否包含某个字符串
contains(String str)
2.3两个字符串是否以指定字符串开头或结尾
boolean Startswith(String);
boolean endswith(String);
2.4字符串是否为空
boolean isEmpty();
3.字符串转换
3.1将字符串变成字符串数组
String[] split(String regex);
3.2将字符串变成字符数组
char[] toCharArray();
3.1将字符串变成字节数组
byte[] getBytes();
3.4将字符串数组变成字符串
构造函数 String(char[])
String(char[],offset,count)将字符数组中的一部分转成字符串。
静态函数 static String copyValueof(char[])
static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。
3.5将字符串的字母大小写转换
String toUppercase();大写
String toUppercase();小写
3.6将字符串的内容替换
String repalce(char oldch,char newch);
String repalce(String s1,String s2);
3.7将字符串两端空格去掉
String trim();
3.8将字符串进行连接
String concat(String);。。
这些基本的类需要会用,API就像学英语用的汉语词典一样,以后需要经常使用。
-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
今天重点看了String类,不断的通过查找API文档来实现一些功能。以前一直把String当成基本数据类型用,现在比较全面的了解了String类。
String类:
String是一个特殊的对象,一旦被初始化,就不会被改变。()指的是abc 不是变量 s1.
String s1="abc"; s1是一个类类型变量,“abc”是一个对象。
String s2=new String("abc");
s1和s2的区别:
s1在字符串常量池中创建了一个abc字符串
s2在堆中创建了两个对象一个是默认对象一个是字符串对象。
==比较的是地址,equals比较的是内容。
下面是我自己查找并且使用的String类。
package com.itcast.test1_1;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "afdaAdasfda";
/*
* sop(s.length()); sop(s.charAt(3));
*/
/*
* sop(s.indexOf('d')); sop(s.indexOf("das")); sop(s.indexOf('d',4));
* sop(s.indexOf("das", 3)); sop(s.substring(4)); sop(s.substring(2,
* 9));
*/
/*
* String s1="["; sop(s1); for (int i = 0; i < s.length(); i++) {
* if(i!=(s.length()-1)){ sop(s.charAt(i)+","); }else{
* sop(s.charAt(i)+"]");
*
* } }
*/
char[] arr={'a','b','c','d','e','f'};
String s1=new String(arr,1,3);
sop(s1);
String s2="dfasdgasaswe";
char[] chs=s2.toCharArray();
sop(chs[3]);
for (int i = 0; i < chs.length; i++) {
sop(chs[i]);
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
下面关于一些重要的API文档
1.1 获取字符串长度
int length();
1.2 根据位置获取字符
char charAt(int index);
1.3 根据字符获取在字符中的位置
int indexof(int ch) 返回的是ch在字符串中第一个出现的位置
int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int indexof(String str); 返回的是str在字符串中第一个出现的位置
int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
反响索引一个字符出现的位置。
int lastindexof(int ch) 返回的是ch在字符串中第一个出现的位置
int lastindexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
int lastindexof(String str); 返回的是str在字符串中第一个出现的位置
int lastindexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
1.4获取字符串中的一部分字符串,也叫子串。
String subString(int beginindex,intendindex) ;
String subString(int beginindex)
2.判断
2.1两个字符串是否相同
equals(Object obj)
equalsIgnoreCase(String str)
2.2字符串中是否包含某个字符串
contains(String str)
2.3两个字符串是否以指定字符串开头或结尾
boolean Startswith(String);
boolean endswith(String);
2.4字符串是否为空
boolean isEmpty();
3.字符串转换
3.1将字符串变成字符串数组
String[] split(String regex);
3.2将字符串变成字符数组
char[] toCharArray();
3.1将字符串变成字节数组
byte[] getBytes();
3.4将字符串数组变成字符串
构造函数 String(char[])
String(char[],offset,count)将字符数组中的一部分转成字符串。
静态函数 static String copyValueof(char[])
static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。
3.5将字符串的字母大小写转换
String toUppercase();大写
String toUppercase();小写
3.6将字符串的内容替换
String repalce(char oldch,char newch);
String repalce(String s1,String s2);
3.7将字符串两端空格去掉
String trim();
3.8将字符串进行连接
String concat(String);。。
这些基本的类需要会用,API就像学英语用的汉语词典一样,以后需要经常使用。
-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
相关文章推荐
- 黑马程序员--API中其他常用的类
- 黑马程序员 常用对象API--集合框架笔记总结
- 黑马程序员 JAVA基础 ---API常用类(二)
- 黑马程序员之 ---- API中的常用类
- 黑马程序员--常用API(String、基本数据类型的包装类、System、Runtime、Date、Calendar、Math)
- 黑马程序员->常用API (1
- 黑马程序员——常用API——Math,System,Calendar,正则表达式
- 黑马程序员——常用API(StringBuilder和StringBuffer以及基本数据类型包装类)
- 黑马程序员----------常用API
- 黑马程序员——常用API——Object,Scanner,String
- 黑马程序员_常用API(1) String&StringBuffer&StringBuilder
- 黑马程序员_Java基础常用API
- 黑马程序员-常用API应用(持续跟新)
- 黑马程序员——包机制、常用的API 笔记第七篇
- 黑马程序员---面向对象及工具类常用API知识点总结
- 黑马程序员 常用对象API-- String类、Stringbuffer、StringBuilder总结
- 黑马程序员_常用API
- 黑马程序员----Java基础之StringBuffer 与 String的区别(常用API)
- 黑马程序员——常用API
- 黑马程序员——常用API——数组高级,Arrays,Integer