美文网首页
第六章字符串

第六章字符串

作者: Dream_wdl | 来源:发表于2018-05-29 17:07 被阅读0次
字符串的初始化方法

字符串:由多个字符组成的一串数据,也可看成字符数组

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

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

字符串数组写在堆内存中
new(新建)的都在堆内存中
字符串都在字符串常量池中
int声明变量在栈内存中
String声明变量在栈内存中

带new字符串 栈 到 堆 到 常量池
带new数组 栈 到 堆
字符串 栈 到 常量池
数字在栈内存中

String的判断功能

 String s1 = "helloworld";
 String s2 = "helloworld";
 String s3 = "HelloWorld";

boolean .equals() 判断字符串是否相等

System.out.println("equals:" + s1.equals(s2));

boolean .equalsIgnoreCase()判断字符串是否相等,忽略大小写

System.out.println("equals:" + s1.equalsIgnoreCase(s2));

boolean .contains()判断大字符串中是否有小字符串

System.out.println("contains:" + s1.contains("hello"));

boolean .startswith()判断字符串是否以某个指定的字符串开头

System.out.println("startsWith:" + s1.startsWith("h"));

boolean .endsWith()判断字符串是否以某个指定的字符串结尾

System.out.println("endsWith:" + s1.startsWith("h"));

boolean .isEmpty() 判断字符串是否为空

System.out.println("isEmpty:" + s1.isEmpty());

String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());

字符串内容为空和字符串对象为空
String s=" "; //对象存在,可以调用方法
String s=null; //对象不存在,不可以调用方法

// s5对象都不存在,所以不能调用方法,空指针异
 System.out.println("isEmpty:" + s5.isEmpty());

String获取功能

String s = "helloworld";

.length() 获取字符串长度

System.out.println("s.length:" + s.length());

.charAt() 获取指定位置上的字符

System.out.println("charAt:" + s.charAt(7));

.indexof() 返回指定字符在此字符串第一次出现的位置
包含则输出位置 数值
不包含则输出 -1 第一位从0开始
可在后面加数值n ,表示从第n位开始查找

System.out.println("indexOf:" + s.indexOf("owo"));
System.out.println("indexOf:" + s.indexOf('l', 4));
System.out.println("indexOf:" + s.indexOf('k', 4)); // -1

.substring() 从指定位置开始截取字符串,默认到结尾

System.out.println("substring:" + s.substring(5));
System.out.println("substring:" + s.substring(3, 8));//8表示从头开始第8位

字符串遍历

public class StringTest {
    public static void main(String[] args) {
        // 定义字符串
        String s = "helloworld";
        for (int x = 0; x < s.length(); x++) {
            System.out.println(s.charAt(x));
        }
    }
}

String的转换功能

String s = "JavaSE";

byte[] .getBytes() 把字符串转换成字节数组

 byte[] bys = s.getBytes();
        for (int x = 0; x < bys.length; x++) {
            System.out.println(bys[x]);
        }

.tocharArray() 把字符串转换成字符数组

char[] chs = s.toCharArray();
        for (int x = 0; x < chs.length; x++) {
            System.out.println(chs[x]);
        }

String.valueof(char[]) 把字符数组转换成字符串

String ss = String.valueOf(chs);

String.valueof(int ) 把int类型的数据转换成字符串

int i = 100;
String sss = String.valueOf(i);

注意:string类的valueof方法可以吧任意类型的数据转换成字符串

.tolowerCase() 把字符串转化成小写

System.out.println("toLowerCase:" + s.toLowerCase());

.toupperCase() 把字符串转换成大写

System.out.println("toUpperCase:" + s.toUpperCase());

.concat() 把字符串拼接

String s1 = "hello";
        String s2 = "world";
        String s3 = s1 + s2;
        String s4 = s1.concat(s2);

String类的其他功能

替换功能 replace

String str2 ="hello world hello kitty";
        String strNew = str2.replace("hello","hehe");
        System.out.println(strNew);
        String strNewName = name.replace(" ","");
        System.out.println(strNewName);

去除字符串两端空格 trim

String name = "     ni hao ";
        System.out.println(name.trim());
        System.out.println(name.trim().length());
        System.out.println(name);
        System.out.println(name.length());

按字典顺序比较两个字符串 comparTo()

String s1 = "Lisi";
          String s2 = "lisi";

          System.out.println(s1.compareTo(s2));
          if(s1.compareToIgnoreCase(s2) > 0)
          {
              System.out.println("da");
          }
          else
          {
              System.out.println("小");
          }

字符串反转

import java.util.Scanner;

/*
 * 字符串反转
 * 举例:键盘录入”abc”        
 * 输出结果:”cba”
 * 
 * 分析:
 *         A:键盘录入一个字符串
 *         B:定义一个新字符串
 *         C:倒着遍历字符串,得到每一个字符
 *             a:length()和charAt()结合
 *             b:把字符串转成字符数组
 *         D:用新字符串把每一个字符拼接起来
 *         E:输出新串
 */
public class StringTest3 {
    public static void main(String[] args) {
        // 键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        String s = myReverse(line);
        System.out.println("实现功能后的结果是:" + s);
    }

    /*
     * 两个明确: 返回值类型:String 参数列表:String
     */
    public static String myReverse(String s) {
        // 定义一个新字符串
        String result = "";

        // 把字符串转成字符数组
        char[] chs = s.toCharArray();

        // 倒着遍历字符串,得到每一个字符
        for (int x = chs.length - 1; x >= 0; x--) {
            // 用新字符串把每一个字符拼接起来
            result += chs[x];
        }
        return result;
    }
}

统计大写字母,小写字母,数字在字符串中的个数

需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
 * 举例:
 *         "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+"个");
    }
}

把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

/*
 * 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
 * 举例:
 *         helloWORLD
 * 结果:
 *         Helloworld
 * 
 * 分析:
 *         A:先获取第一个字符
 *         B:获取除了第一个字符以外的字符
 *         C:把A转成大写
 *         D:把B转成小写
 *         E:C拼接D
 */
public class StringTest {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "helloWORLD";

        // 先获取第一个字符
        String s1 = s.substring(0, 1);
        // 获取除了第一个字符以外的字符
        String s2 = s.substring(1);
        // 把A转成大写
        String s3 = s1.toUpperCase();
        // 把B转成小写
        String s4 = s2.toLowerCase();
        // C拼接D
        String s5 = s3.concat(s4);
        System.out.println(s5);

        // 优化后的代码
        // 链式编程
        String result = s.substring(0, 1).toUpperCase()
                .concat(s.substring(1).toLowerCase());
        System.out.println(result);
    }
}

字符串拆分

split()

String类提供了split()方法,将一个字符串分割为子字符串,结果作为字符串数组返回

public class Lyric {
        public static void main(String[] args) {
            String words="长亭外 古道边 芳草碧连天 晚风扶 柳笛声残 夕阳山外山";
            String[ ] printword=new String[100];        
            System.out.println("***原歌词格式***\n"+words);
            System.out.println("\n***拆分后歌词格式***");
            printword=words.split(" "); 
            for(int i=0;i<printword.length;i++){
                System.out.println( printword[i] );
            }
        }
}

StringBuffer和StringBuilder

StringBuffer sb = new StringBuffer();
StringBuffer sb = new StringBuffer("aaa");
StringBuffer的使用
sb.toString(); //转化为String类型
sb.append("aaa"); //追加字符串

相关文章

  • python基础7

    第六章 字符串的秘密 6.1 字符串的创建 字符串可以按以下方法创建 或 或 6.2 字符串的索引 字符串: 索引...

  • logback官方文档中文翻译第六章:Layouts

    第六章:Layouts layout 是 logback 的组件,负责将日志事件转换为字符串。Layout 接口中...

  • <>

    第六章重点: go语言数据类型:基础类型(数字,字符串,布尔类型),复合类型(数组,结构体),引用类型(指针,切片...

  • 课程学习-第五章/第六章

    字符串连接运算符 接收用户键盘输入 String ss = sc.next(); 第六章:控制语句 day-08课...

  • MICK-SQL基础教程(第二版) 第六章 函数、谓词、CASE

    第六章 函数、谓词、CASE表达式 函数 函数大致可以分为以下几种: 算术函数(用来进行数值计算的函数) 字符串函...

  • 《岳响河》目录 第六章

    第六章1 第六章2 第六章3 第六章4 第六章5 第六章6 第六章7 第六章8 第六章9 第六章10 第六章11 ...

  • 第六章字符串

    字符串的初始化方法 字符串:由多个字符组成的一串数据,也可看成字符数组 字符串特点:一旦被赋值,就不能改变但是引用...

  • 第六章 字符串

    创建 简单操作 \ 转义符 +拼接 不可以用 字符串和 一个非字符串类型的对象相加 * 复制 字符串 和 0 或者...

  • 第六章 字符串

    创建 简单操作 \ 转义符 +拼接 不可以用 字符串和 一个非字符串类型的对象相加 * 复制 字符串 和 0 或者...

  • 数据结构与算法365天特训营 百度网盘分享

    第一章:算法基础第二章: 线性表第三章: 栈与队列第四章: 数组第五章: 字符串第六章: 二叉...

网友评论

      本文标题:第六章字符串

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