zoukankan      html  css  js  c++  java
  • String类常用的方法

      String类是我们最常使用的类。字符串类的方法我们必须非常熟悉!我们列出常用的方法,请大家熟悉。

               表5-2 String类的常用方法列表

    图2.png

    【示例】String类常用方法一

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class StringTest1 {
        public static void main(String[] args) {
            String s1 = "core Java";
            String s2 = "Core Java";
            System.out.println(s1.charAt(3));//提取下标为3的字符
            System.out.println(s2.length());//字符串的长度
            System.out.println(s1.equals(s2));//比较两个字符串是否相等
            System.out.println(s1.equalsIgnoreCase(s2));//比较两个字符串(忽略大小写)
            System.out.println(s1.indexOf("Java"));//字符串s1中是否包含Java
            System.out.println(s1.indexOf("apple"));//字符串s1中是否包含apple
            String s = s1.replace(' ''&');//将s1中的空格替换成&
            System.out.println("result is :" + s);
        }
    }

    图5-31 示例5-29运行效果图.png

    【示例】String类常用方法二

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class StringTest2 {
        public static void main(String[] args) {
            String s = "";
            String s1 = "How are you?";
            System.out.println(s1.startsWith("How"));//是否以How开头
            System.out.println(s1.endsWith("you"));//是否以you结尾
            s = s1.substring(4);//提取子字符串:从下标为4的开始到字符串结尾为止
            System.out.println(s);
            s = s1.substring(47);//提取子字符串:下标[4, 7) 不包括7
            System.out.println(s);
            s = s1.toLowerCase();//转小写
            System.out.println(s);
            s = s1.toUpperCase();//转大写
            System.out.println(s);
            String s2 = "  How old are you!! ";
            s = s2.trim();//去除字符串首尾的空格。注意:中间的空格不能去除
            System.out.println(s);
            System.out.println(s2);//因为String是不可变字符串,所以s2不变
        }
    }

    图5-32 示例5-30运行效果图.png

  • 相关阅读:
    [文档].Altera 可选择的Nios II的Boot方法
    [原创].关于使用QII 10.0编译器无法编辑和查看中文的问题一个变通解决方案
    判断某程序是64位还是32位
    在调用RestoreSPSite时指定ContentDatabase
    CAML join
    ADODB.Connection Invalid connection error
    Sharepoint 如何修改Web.Config文件
    ActivateOnDefault & AutoActivateInCentralAdmin feature 属性
    Sharepoint 2010 解决DFWP Unable to display this Web Part 的问题
    Powershell点滴
  • 原文地址:https://www.cnblogs.com/huaxiansheng/p/15312848.html
Copyright © 2011-2022 走看看