java学习笔记【二】之字符串常见处理方法

java学习笔记【二】

书写是对思维最好的缓存

越努力,越幸运!!!

特喜欢村上春树一句话:不必太纠结于当下,也不必太忧虑未来,人生没有无用的经历,当你经历过一些事情后,眼前的风景已经和从前不一样了。

java学习笔记【二】之字符串常见处理方法_第1张图片

  1. java中的数据类型,除String外,其他的数据类型必须是小写开头;
  2. 计算表达式时,按照下面的顺序执行:
    1. 最先执行递增递减运算符
    2. 接下来是乘,除,求模;
    3. 然后加减
    4. 接下来是 比较
    5. 最后根据等号给变量赋值;
  3. 输出语句 :System.out.println;System必须为大写;输出换行
    System.out.print; 输出不换行
    代码验证:
public class test {
    public static void main(String[] args)
    {

        System.out.println("I ");
        System.out.println("Love ");
        System.out.println("China ");
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53568 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I 
Love 
China 

Process finished with exit code 0

public class test {
    public static void main(String[] args)
    {

        System.out.print("I ");
        System.out.print("Love ");
        System.out.print("China ");
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53589 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Df ile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love China 
Process finished with exit code 0

4 . 为了显示双引号,java创建一个特殊编码",可放到字符串中,字符串中遇到改编码,将其替换成双引号。

public class test {
    public static void main(String[] args)
    {

        System.out.println("I Love \"china\"");
    }
}

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53709 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love "china"

Process finished with exit code 0

  1. 字符串的拼接
    可以使用加号“+”
public class test {
    public static void main(String[] args)
    {

        String a="I Love ";
        String b="China";
        System.out.println(a+b);
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54246 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love China

Process finished with exit code 0

“+”还可以将字符串与变量连接起来
代码:

public class test {
    public static void main(String[] args)
    {

        int c=2020;
        String a=" I Love ";
        String b="China";
        System.out.println(c+a+b);
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54369 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
2020 I Love China

Process finished with exit code 0

字符串的高级处理

1.比较两个字符串是否相等,可以使用equals()语句; 其值返回ture或者false
代码:

public class test {
    public static void main(String[] args)
    {

        int c=2020;
        String a=" I Love ";
        String b="China";
        System.out.println(a.equals(b));
    }
}

输出结果

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54769 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
false

Process finished with exit code 0

  1. 确定字符串长度
    可以使用length()但是其只涉及一个变量
    代码:
public class test {
    public static void main(String[] args)
    {

        int len;
        String a="I Love China";
        len=a.length();
        System.out.println(len);
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54858 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
12

Process finished with exit code 0

3.改变字符串的大小写
java提供了将字符串全部转换成大写的方法:
(toUpperCase())
和全部转换成小写的方法:
(toLowerCase());
代码如下:

public class test {
    public static void main(String[] args)
    {

        String a="I LOVE CHINA";
        String b=a.toLowerCase();
        System.out.println(b);
        String d="fanqiuqiu i miss you";
        String f=d.toUpperCase();
        System.out.println(f);
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=55060 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
i love china
FANQIUQIU I MISS YOU

Process finished with exit code 0

4.查找字符串
要在字符串中查找,可以使用indexof() ,并将要查找到的内容放到括号之中,如果未找到字符串,返回-1,如果找到,indexof()返回一个整数值,指出该字符串的起始位置。
代码示例:

public class test {
    public static void main(String[] args)
    {

        String a="I LOVE CHINA ,fanqiuqiu i miss you";
        String b="miss";
        String c="you";
        String d="LOVE";
        int bnum=a.indexOf("miss");
        System.out.println(bnum);
        System.out.println(a.indexOf(c));
        System.out.println(a.indexOf(d));
    }
}

输出结果:

D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=55492 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
26
31
2

Process finished with exit code 0

好了今天小飞就写到这里了,

喜欢的话,就点个赞吧!!!

喜欢的话,就点个赞吧!!!

喜欢的话,就点个赞吧!!!

java学习笔记【二】之字符串常见处理方法_第2张图片

你可能感兴趣的:(java学习)