zoukankan      html  css  js  c++  java
  • javase学习第13天(String字符串)

    String相关知识

    引用类型的强制类型转换需要注意:转换只能在具有继承关系的两个类型之间进行;如果试图将一个父类实例变量转换成子类类型时,这个对象必须是子类的实例才行(编译时是父类类型,运行时是子类类型),否则发生ClassCastException异常。

    String、StringBuffer

    String类代表的是一个字符,它的值是固定的,永恒的,一个字符一旦被创建,包含在这个对象中的字符序列就是不可更改的,直到对象被销毁;

    StringBuffer代表一个字符序列可变的字符串,当创建一个StringBuffer对象后,可通过方法改变其字符序列;最后通过toString()方法将其换成一个String对象;

    常用方法

    截取子串: str.substring(int a,int b) a/b代表下标,前包后不包,即a可取得,b取不到;截取长度为(b-a);

    int compareTo(String anotherString):比较两个字符串的大小,如果两个字符串的字符序列完全相等,则返回0;不相等时,从两个字符串的第0个字符开始比较,返回第一个不相等的字符差(即两个字符的编码的差),当一个较长的字符串的前面的部分恰巧是一个短的字符串,则返回它们的长度差。

    static String copyValueOf(char[] data) :将字符数组连成字符串,与String(char[] content)构造方法作用相同

    boolean endsWith(String suffix) :判断是否以一个字符串结尾 int indexOf(int ch) 返回字符ch在字符串第一次出现的位置,

    Character

    常用方法:

    1、public static boolean isUpperCase(char ch):判断指定字符是否是大写字符

    2、public static boolean isLowerCase(char ch):判断指定字符是否是小写字符

    3、public static boolean isDigit(char ch):判断指定字符是否是数字

    4、public static char toUpperCase(char ch):将指定字符转换成大写

    5、public static char toLowerCase(char ch):将指定字符转换成小写

    ###案例

     1 package at.it18zhang;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 从键盘输入一个字符串,统计其中大写字母字符,小写字母字符,数字字符和其他字符出现的次数
     6  * @author feigu
     7  *
     8  */
     9 
    10 public class Count {
    11 
    12     public static void main(String[] args) {
    13         // TODO Auto-generated method stub
    14         System.out.println("请输入字符串:");
    15         Scanner s=new Scanner(System.in);
    16         String str=s.next();
    17         char[]ch=str.toCharArray();
    18         System.out.println("总的字符个数为:"+ch.length);
    19         int Ucount=0;
    20         int Lcount=0;
    21         int Dcount=0;
    22         int Ocount=0;
    23         for(char c:ch){
    24             if(Character.isUpperCase(c)){
    25                 Ucount++;
    26             }
    27             else if(Character.isLowerCase(c)){
    28                 Lcount++;
    29             }
    30             else if(Character.isDigit(c)){
    31                 Dcount++;
    32             }
    33             else{
    34                 Ocount++;
    35             }
    36         }
    37         System.out.println("大写字母的个数是:"+Ucount);
    38         System.out.println("小写字母的个数是:"+Lcount);
    39         System.out.println("数字的个数是:"+Dcount);
    40         System.out.println("其他字符出现的次数"+Ocount);
    41     }
    42 
    43 }
     1 package at.it18zhang;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 从键盘输入qq号码,编写方法对其进行判断,返回值为布尔值,要求:
     6 1.必须是5-15位数字        
     7 2.不能0开头            
     8 
     9  * @author feigu
    10  *
    11  */
    12 
    13 public class QqTest {
    14 
    15     public static void main(String[] args) {
    16         // TODO Auto-generated method stub
    17         Scanner s = new Scanner(System.in);
    18         System.out.println("请输入一个5-15位号码");
    19         String str=s.next();
    20         boolean b=trueOrFalse(str);
    21         System.out.println(b);
    22         
    23     }
    24     public static boolean trueOrFalse(String str){
    25         boolean b=true;
    26         if(str.length()>15||str.length()<5){
    27             b=false;
    28         }
    29         else if(str.startsWith("0")){
    30             b=false;
    31         }
    32         else{
    33             char[] arr=str.toCharArray();
    34             for(char c:arr){
    35                 if(!Character.isDigit(c)){
    36                     b=false;
    37                 }
    38             }
    39         }
    40         return b;
    41     }
    42 }

    正则表达式

    是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。

    组成规则

    1、字符

        x  :字符 x。举例:'a'表示字符a

         :反斜线字符

        :新行(换行)符 ('u000A')

        :回车符 ('u000D')

    2、字符类

    [abc]        :a 或 b 或 c(取其中之一)

    [^abc]     : 除了 a、b 或 c之外的任何字符(否定)

    [a-zA-Z]    :a到 z 或 A到 Z,两头的字母包括在内(范围)

    [0-9]        : 0到9的字符都包括,取其一

    3、预定义字符类

      :任何字符。若想要匹配.本身,则使用.

    :数字:[0-9]

    w :单词字符:[a-zA-Z_0-9]   ;在正则表达式里面组成单词的东西必须由这些东西组成

    4、边界匹配器

    ^  :行的开头

    $  :行的结尾

      :单词边界 就是不是单词字符的地方。 举例:hello world?haha;xixi

    5、Greedy 数量词 ,用来匹配前面的规则出现的次数

    X?          :X,一次或一次也没有

    X*          :X,零次或多次,可以没有 X+ X,一次或多次,至少一次

    X{n}      :X,恰好 n 次

    X{n,}     :X,至少 n 次

    X{n,m}   :X,至少 n 次,至多 m 次

     1 package at.it18zhang;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 邮箱验证
     6  * @author feigu
     7  *
     8  */
     9 public class Com {
    10     public static void main(String[] args) {
    11         Scanner s=new Scanner(System.in);
    12         System.out.println("请输入邮箱");
    13         String str=s.next();
    14         String regex="\w+@\w{2,6}(\.\w{2,4})+";
    15         boolean b=cheak(str, regex);
    16         System.out.println(b);
    17     }
    18     public static boolean cheak(String mailbox,String regex){
    19         return mailbox.matches(regex);
    20     }
    21 }

    注意:

    用来匹配的正则表达式中出现的一般都是\,例如:\b,\w,\d,\.

    String类分割功能

       public String[] split(String regex)

    把给定的字符串用指定的字符串切分成小字符串数组

     1 package at.it18zhang;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 有一个字符串用来描述年龄:比如:“20-30”,要求从此字符串中取出年龄上下限,
     6  * 然后从键盘输入一个年龄值,并对其进行判断,在年龄范围值之内打印出OK,否则打印NO
     7  * @author feigu
     8  *
     9  */
    10 public class Cut {
    11     public static void main(String[] args) {
    12         String st="40-30";
    13         String []arr=st.split("-");
    14         int a=Integer.parseInt(arr[0]);
    15         int b=Integer.parseInt(arr[1]);
    16         Scanner s=new Scanner(System.in);
    17         System.out.println("input num");
    18         int x=s.nextInt();
    19         
    20         if(a>b){
    21             if(x>b&&x<a){
    22                 System.out.println("OK");
    23             }
    24             else{
    25                 System.out.println("no");
    26             }
    27         }
    28         else {
    29             if(x>a&&x<b){
    30                 System.out.println("OK");
    31             }
    32             else{
    33                 System.out.println("no");
    34             }
    35         }
    36     }
    37 }

     

    涉及到正则表达式转义字符匹配时:

    如果被匹配的字符串中使用的是一个,则用来匹配的表达式中使用两个\

    如果被匹配的字符串中使用的是两个\,则用来匹配的表达式中使用的是四个\\

    要对特殊字符进行匹配的话,通常是用两个\

    特殊字符有:

    $,*,^,+,?,\,|  (),[],{} .  

    String类替换功能

    public String replaceAll(String regex,String replacement)

    把字符串中符合正则表达式的地方,换成第二个参数

     1 /*
     2 *      使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 
     3  */
     4 public class RegexDemo {
     5     public static void main(String[] args) {
     6         // 定义一个字符串
     7         String s = "helloqq12345worldkh622112345678java";
     8         // 我要去除所有的数字,用*给替换掉
     9             String ss = "*";
    10         // 直接把数字干掉
    11         String regex = "\d+";     //至少一个数字
    12         String result = s.replaceAll(regex, ss);//把数字用*替
    14         System.out.println(result);
    15     }
    16 }
    17     

     

    System类概述及其成员方法

    1 public static void gc()                //通知jvm进行垃圾收集
    2 public static void exit(int status)    //退出当前程序,0正常,非0表示非正常
    3 public static long currentTimeMillis()//从1970-1-1午夜0点到目前的毫秒值
    4 public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

     

     1 public class Person {
     2     private String name;
     3     private int age;
     4     public Person() {}
     5     public Person(String name, int age) {
     6         this.name = name;
     7         this.age = age;
     8     }
     9     public String getName() {
    10         return name;
    11     }
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15     public int getAge() {
    16         return age;
    17     }
    18     public void setAge(int age) {
    19         this.age = age;
    20     }
    21     @Override
    22     public String toString() {
    23         return "Person [name=" + name + ", age=" + age + "]";
    24     }
    25     @Override
    26     protected void finalize() throws Throwable {
    27         System.out.println("当前的对象被回收了" + this);
    28         super.finalize();
    29     }
    30 }
    31 
    32 package cn.test_01;
    33 public class SystemDemo {
    34     public static void main(String[] args) {
    35         Person p = new Person("zhangsan", 60);
    36         System.out.println(p);
    37         p = null; // 让p不再指定堆内存
    38         System.gc();//验证对象是否会被回收,即finalize方法是否会被调用
    39     }
    40 }

    exit(int i):i=0表示正常退出,i=其他值表示非正常退出

    BigInteger:大整数;BigDecimal:大小数。

    System类概述及其成员方法

    Date类

    类 Date 表示特定的瞬间,精确到毫秒。

    构造方法  public Date() ;public Date(long date)

    成员方法

    1、public long getTime():获取从1970, 00:00:00 GMT到当前时间对象所过去的毫秒值,和系统的时区设置相关

    2、public void setTime(long time):用毫秒值设置一个时间点,这个时间点是从1970, 00:00:00 GMT过去的指定的毫秒值

    Calendar类

    Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAYOFMONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

    成员方法

    1、public static Calendar getInstance():获取一个实例对象

    2、public int get(int field):返回指定日历字段的值

    3、public void add(int field,int amount):根据日历规则,为指定字段添加或者减去指定的时间量

    4、public final void set(int year,int month,int date):注意月份的起始值是0,所以要设置8月时,设置为7即可

     

    public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。

    1 // 5年后的10天前
    2         c.add(Calendar.YEAR, 5);
    3         c.add(Calendar.DATE, -10);

    public final void set(int year,int month,int date):设置当前日历的年月日

     

     

     

  • 相关阅读:
    Bit Manipulation
    218. The Skyline Problem
    Template : Two Pointers & Hash -> String process
    239. Sliding Window Maximum
    159. Longest Substring with At Most Two Distinct Characters
    3. Longest Substring Without Repeating Characters
    137. Single Number II
    142. Linked List Cycle II
    41. First Missing Positive
    260. Single Number III
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6659301.html
Copyright © 2011-2022 走看看