东理宝二.常用方法SHANDONGUNIVERSITYOFTECHNOLOGYpublicintcompareTo(Strings):按字典序与参数s指定的字符串比较大小publicintcompareTolgnoreCase(Strings)publicbooleancontains(Strings):判断当前字符串对象是否含有参数指定的字符串spublicStringsubstring(intstartpoint):获得当前字符串的子串public String substring(int start,int end)publicStringtrim:得到一个str去掉前后空格后的字符串对象。专业专注创新
专业 专注 创新 public int compareTo(String s):按字典序与参数s 指定的字符串比较大小 public int compareToIgnoreCase(String s) public boolean contains(String s):判断当前字符 串对象是否含有参数指定的字符串s public String substring(int startpoint):获得当前 字符串的子串 public String substring(int start ,int end) public String trim() :得到一个str去掉前后空格后 的字符串对象。 二. 常用方法
东理功二.常用方法SHANDONGUNIVERSITY OFTECHNOLOGYintindexof(int ch)返回指定字符在此字符串中第一次出现处的索引。intindexof(intch,intfromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜素。intindexof(String str)返回指定子字符串在此字符串中第一次出现处的索引。intindexof(String str,int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始,intlastIndexof(intch)返回指定字符在此字符串中最后一次出现处的索引。intlastIndexof(int ch,int fromIndex)返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。intlastIndexof(Stringstr)返回指定子字符串在此字符串中最右边出现处的索引。intlastIndexof(String str, int fromIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索专业专注创新
专业 专注 创新 二. 常用方法
东理友情提示SHANDONGUNIVERSITYOFTECHNOLOGY1.字符串str中字符的索引从0开始,范围为0到str.length()-12.使用indexOf进行字符或字符串查找时,如果匹配返回位置索引:如果没有匹配结果,返回-13.使用substring(beginlndex,endlndex)进行字符串截取时,包括beginlndex位置的字符,不包括endlndex位置的字符专业专注创新
专业 专注 创新 友情提示 1. 字符串 str 中字符的索引从0开始,范围为 0 到 str.length()-1 2. 使用 indexOf 进行字符或字符串查找时,如果匹 配返回位置索引;如果没有匹配结果,返回 -1 3. 使用 substring(beginIndex , endIndex) 进行字 符串截取时,包括 beginIndex 位置的字符,不包括 endIndex 位置的字符
东理课堂练习SHANDONGUNIVERSITYOFTECHNOLOGY功能:判断Java源文件名是否正确;福合法的文件名应该以-java结尾判断邮箱格式是否正确。合法的邮箱名中至少要包含“@”,并要求“@”在“”之前。public static void main(String args)(String fileName="HelloWorld.jav";Il Java文件名Stringemail="laurenyang@imooc.com";Il邮箱7专业专注创新
专业 专注 创新 课堂练习 功能: 判断 Java 源文件名是否正确; 合法的文件名应该以 .java 结尾; 判断邮箱格式是否正确。 合法的邮箱名中至少要包含 “@” , 并要求 “@” 在 “.” 之前。 public static void main(String[] args) { String fileName = "HelloWorld.jav"; // Java文件名 String email = "laurenyang@imooc.com"; // 邮箱 . }
东理功三.字符串与基本数据类型SHANDONGUNIVERSITY OFTECHNOLOGY基本数据字符串一、将字符串基本类型类型数值类型转换:java.lang.Integer类的方法:public static int parselnt(String s)public staticintparselnt(Strings,int p)举例:intx1=Integer.parselnt("123");//x1=123//x2=83intx2=Integer.parselnt("123",8);//x3=27intx3=Integer.parselnt("123",4);其他基本数据类型的封装类:使用java.lang包中的ByteShort、Long、Float、Double、Boolean类调相应的类方法“数字字符串”parseXxx可以将由→>相应的基本数据类型数值,可能发生异常。布尔类型数据转换Boolean.parseBoolean(Stringstr)
专业 专注 创新 三. 字符串与基本数据类型 一、将字符串→基本类型: 数值类型转换: java.lang.Integer类的方法: public static int parseInt(String s) public static int parseInt(String s, int p) 举例:int x1 = Integer.parseInt("123"); //x1=123 int x2 = Integer.parseInt(“123”,8); //x2=83 int x3= Integer.parseInt(“123”,4); //x3=27 其他基本数据类型的封装类: 使用java.lang包中的Byte、 Short、Long、Float、Double、Boolean类调相应的类方法 parseXxx可以将由“数字字符串” →相应的基本数据类型数 值,可能发生异常。 布尔类型数据转换 Boolean.parseBoolean(String str) 字符串 基本数据 类型