第4章字符串.ppt
文本预览下载声明
模式匹配 Pattern.matches(regex, input); 等价于 Ppile(regex).matcher(input).matches() 模式匹配 下列几个方法也是Matcher对象m常用的方法: public boolean find(int start) 判断input从参数start指定位置开始是否有和patter匹配的子序列,参数start取值0时,该方法和lookingAt()的功能相同。 public String replaceAll(String replacement) Matcher对象m调用该方法可以返回一个字符串对象,该字符串是通过把input中与模式patter匹配的子字符串全部替换为参数replacement指定的字符串得到的(注意,input本身没有发生变化)。 public String replaceFirst(String replacement) Matcher对象m调用该方法可以返回一个字符串对象,该字符串是通过把input中第1个与模式patter匹配的子字符串替换为参数replacement指定的字符串得到的(注意,input本身没有发生变化)。 模式匹配 模式匹配 正则表达式与字符串分解 public String[] split(String regex) 字符串调用该方法时,使用参数指定的正则表达式regex做为分隔标记分解出其中的单词,并将分解出的单词存放在字符串数组中 public String[] split(String) public String[] split(String regex,int limit) 以特定的字符串作为间隔,拆分当前字符串的内容,会获得一个字符串数组 字符|,*,+.作为分隔符时都需要加上转义字符,即在前面加上\\ 字符串与基本数据的相互转化 字符串转化为整型 可能抛出NumberFormatException public static int parseInt(String s) public static byte parseByte(String s) public static short parseShort(String s) public static long parseLong(String s) 5.字符串与基本数据的相互转化 字符串转化为浮点型 public static float parseFloat(String s) public static double parseDouble(String s) 字符串与基本数据的相互转化 数值转化为字符串 public static String toBinaryString(long i) public static String toOctalString(long i) public static String toHexString(long i) public static String toString(long i, int p) 返回i 的p进制表示 字符串与基本数据的相互转化 数值转化为字符串 public static String valueOf(byte n) public static String valueOf(int n) public static String valueOf(float n) public static String valueOf(double n) public static String valueOf(boolean b) public static String valueOf(Object obj) public static String valueOf(char data[]) 例子:【例6-2】 对象的字符串表示 所有的类都默认是java.lang包中Object类的子类或间接子类。 Object类有一个public方法toString(),一个对象通过调用该方法可以获得该对象的字符串表示。 如果没有重写该方法,该类所创建的对象调用toString()方法得到的字符串格式为: 类名@对象的引用 例子:【例6-3】 字符串与字符、字节数组 字符串与字符数组 String(char[]) String(char[],int offset,int length) public void getChars (int start, int end, char c[], int offset )将start到end-1位置上的字符复制到数组c中,并从
显示全部