• JAVA语言基础——字符串


    1.字符串的比较代码 String.equals

      1 System.out.println("str1和str2是否相同:"+(str1.equals(str2)));

    equals在英语中有数量平等相同的意思

    字符串判断相等与否不能使用"==" ,否则会出现意外错误影响程序处理。

    字符串的判断相等是用.equals,返回值为布尔型——true与false。

    2.字符串的信息 String.length

    1 str = "字符串常量字符串常量";

    2 System.out.println("字符串长度 = " + str.length());

    length在英语中为长度的意思,在此代码中,求str字符串的数量。

    在此代码中返回值为:字符串长度 = 10。

    3.查找字符串。String.indexOf

    1 System.out.println("常字的位置 = " + str.indexOf("常"));

    2 System.out.println("常量的位置 = " + str.indexOf("常量")); 

    从前往后查找字符串位置中子字符串的索引值。
    返回值为找到之后的首字的索引值。

    4.查如果无法查找到关键字,返回值为负数。

    System.out.println("我的位置 = " + str.indexOf("问"));

    我的位置:-1.

    5。获取字符串位置。string.charAt

    str = "字符串常量字符串常量";

    char c1 = str.charAt(0);

    System.out.println("c1 = "+c1);

    按照西方惯例,一切数字以0为开始,在JAVA中也是如此,第一个字符是从0开始数,第二个字符才是1.

    所以这段字符串索引中0为首个字符——“字”。

  • 相关阅读:
    Android 程序员必须知道的 53 个知识点
    2017.8.27 考试
    hdu 3118 Arbiter
    UVA 1575 Factors
    [HNOI2008]Cards
    JSOI2008 小店购物
    hdu 2121 Ice_cream’s world II
    poj 3164 Command Network(最小树形图模板)
    [USACO14MAR] Counting Friends
    UVA 10479 The Hendrie Sequence
  • 原文地址:https://www.cnblogs.com/TENOKAWA/p/5228807.html
Copyright © 2020-2023  润新知