美文网首页
2017-12-26字符串

2017-12-26字符串

作者: 王凯_wkk | 来源:发表于2018-04-04 13:16 被阅读0次

字符串的长度:

String s6 = new String("abcde");
System.out.println("s6.length():" + s1.length());
结果:5

字符串的特点:一旦被赋值,就不能改变,但是引用可以改变。

public class StringDemo {
    public static void main(String[] args) {
        String s = "hello";
        s += "world";
        System.out.println("s:" + s);
结果:helloworld

String s = new String(“hello”)和 String s = “hello”;的区别?String字面值对象和构造方法创建对象的区别
有。前者会创建2个对象,后者创建1个对象。

  • ==:比较引用类型比较的是地址值是否相同
  • equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
public class StringDemo2 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = "hello";

        System.out.println(s1 == s2);// false
        System.out.println(s1.equals(s2));// true
    }
}
  • String类的判断功能:
  • boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean contains(String str):判断大字符串中是否包含小字符串
  • boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  • boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  • boolean isEmpty():判断字符串是否为空。

注意:
String s = "";//对象存在,所以可以调方法
String s = null;//对象不存在,不能调方法

String类的获取功能

  • int length():获取字符串的长度。
  • char charAt(int index):获取指定索引位置的字符
  • int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
    // 为什么这里是int类型,而不是char类型?
    // 原因是:'a'和97其实都可以代表'a'。如果里面写char,就不能写数字97了
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  • int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • String substring(int start):从指定位置开始截取字符串,默认到末尾。
  • String substring(int start,int end):从指定位置开始到指定位置结束截取字符串

/*

  • 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
  • 举例:
  •     "Hello123World"
    
  • 结果:
  •     大写字符:2个
    
  •     小写字符:8个
    
  •     数字字符:3个
    
  • 分析:
  •     前提:字符串要存在
    
  •     A:定义三个统计变量
    
  •         bigCount=0
    
  •         smallCount=0
    
  •         numberCount=0
    
  •     B:遍历字符串,得到每一个字符。
    
  •         length()和charAt()结合
    
  •     C:判断该字符到底是属于那种类型的
    
  •         大:bigCount++
    
  •         小:smallCount++
    
  •         数字:numberCount++
    
  •         这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
    
  •         ASCII码表:
    
  •             0    48
    
  •             A    65
    
  •             a    97
    
  •         虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
    
  •             char ch = s.charAt(x);
    
  •             if(ch>='0' && ch<='9') numberCount++
    
  •             if(ch>='a' && ch<='z') smallCount++
    
  •             if(ch>='A' && ch<='Z') bigCount++
    
  •    D:输出结果。
    
  • 练习:把给定字符串的方式,改进为键盘录入字符串的方式。
    */
    public class StringTest2 {
    public static void main(String[] args) {
    //定义一个字符串
    String s = "Hello123World";

//定义三个统计变量
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;

//遍历字符串,得到每一个字符。
for(int x=0; x<s.length(); x++){
char ch = s.charAt(x);

//判断该字符到底是属于那种类型的,char类型会转成int类型
if(ch>='a' && ch<='z'){
smallCount++;
}else if(ch>='A' && ch<='Z'){
bigCount++;
}else if(ch>='0' && ch<='9'){
numberCount++;
}
}

//输出结果。
System.out.println("大写字母"+bigCount+"个");
System.out.println("小写字母"+smallCount+"个");
System.out.println("数字"+numberCount+"个");
}
}

String的转换功能:

  • byte[] getBytes():把字符串转换为字节数组。
  • char[] toCharArray():把字符串转换为字符数组。
  • static String valueOf(char[] chs):把字符数组转成字符串。
  • static String valueOf(int i):把int类型的数据转成字符串。
    //注意:String类的valueOf方法可以把任意类型的数据转成字符串。
  • String toLowerCase():把字符串转成小写。
  • String toUpperCase():把字符串转成大写。
  • String concat(String str):把字符串拼接。

http://www.cnblogs.com/kamibaba/p/8116281.html

相关文章

网友评论

      本文标题:2017-12-26字符串

      本文链接:https://www.haomeiwen.com/subject/wfajgxtx.html