这篇文章带你学会字符串的日常操作

String类

字符串在日常生活中无处不在,所以掌握字符串的使用至关重要。

使用 String 对象存储字符串,String 类位于 java.lang 包中,java.lang 不需要我们手动导入可以直接使用。

敲一敲:String对象存储字符串

String s="Hello world";
String s=new String();
String s=new String("Hello world");

下面列出一些常用的方法

方法 介绍
length() 获取字符串中字符的个数
equals() 比较两个字符串对象的内容是否一致
equalsIgnoreCase() 忽略大小写比较
toLowerCase() 转小写
toUpperCase() 转大写
concat() 向字符串后面拼接字符串并返回一个新字符串
indexOf() 搜索第一个出现的字符或字符串,从左往右
lastIndexOf() 与indexOf搜索方向相反
substring() 提取指定位置开始的字符串或指定开始和结束之间的字符串
trim() 返回去除字符串前后的空格后的副本
split() 按照指定字符串分隔,返回字符串数组

敲一敲:length 方法的使用

import java.util.Scanner;

public class DemoLength {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入用户名:");
String name=input.next();
if (name.length()<4) {
System.out.println("用户名不能小于4位");
}else {
System.out.println("用户名可以使用,长度为:"+name.length());
}
}
}

==equals 不能混用,== 判断两个字符串在内存中的地址,在任何地方使用 new 都会产生新的内存地址。

敲一敲:体会区别和字符串的不可变性

public class DemoString {
public static void main(String[] args) {
String a="张三";
String b="张三";//a与b是同一个对象,未变
System.out.println(a==b);//true
System.out.println(a.equals(b));//true String c=new String("张三");//新的字符串对象
System.out.println(a==c);//false
System.out.println(a.equals(c));//true
}
}

比较两个字符串的值不能使用 == ,应该使用 equals 方法比较

敲一敲:不考虑大小写比较1

import java.util.Scanner;

//忽略大小比较方法1
public class DemoEquals1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String str="";
do {
System.out.println("继续输入 Yes");
str=input.next();
} while (str.toLowerCase().equals("yes")||str.toUpperCase().equals("YES"));
}
}

敲一敲:不考虑大小写比较2

import java.util.Scanner;

public class DemoEquals2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String str="";
do {
System.out.println("继续输入 Yes");
str=input.next();
} while (str.equalsIgnoreCase("yeS"));
}
}

敲一敲:对比 + 拼接和 concat() 拼接

public class DemoConcat {
public static void main(String[] args) {
String a="hello";
String b=a+" world";
System.out.println(b);
String c=b.concat(" test");//b不会变
System.out.println(b);
System.out.println(c);
}
}

敲一敲:indexOf 和 lastIndexOf 的使用

import java.util.Scanner;

public class TestIndexOf {
public static void main(String[] args) {
System.out.println("请输入邮箱:");
Scanner input=new Scanner(System.in);
String email=input.next();
if(email.indexOf("@")==-1) {
System.out.println("邮箱格式有误!");
}
String text="a@bb@cc";
System.out.println("indexOf:"+text.indexOf("@"));
System.out.println("lastIndexOf:"+text.lastIndexOf("@"));
}
}

敲一敲:使用substring

import java.util.Scanner;
public class DemoSubstring {
public static void main(String[] args) {
//获取邮箱用户名
Scanner input=new Scanner(System.in);
System.out.println("输入完整邮箱:");
String email=input.next();
int index=email.indexOf("@");
String username=email.substring(0,index);
System.out.println("用户名:"+username);
}
}

敲一敲:使用 trim

public class DemoTrim {
public static void main(String[] args) {
String text=" he llo ";
System.out.println(text.trim());
}
}

敲一敲:使用 split

public class DemoSplit {
public static void main(String[] args) {
String text="hello,world,java";
String[] result=text.split(",");
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}

StringBuffer类与常用方法

对字符串频繁修改(如字符串连接)时,使用 StringBuffer 类可以大大提高程序执行效率。

敲一敲:使用 StringBuffer 拼接字符

public class DemoStringBuffer {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("hello");
sb.append("word");
System.out.println(sb.toString()); StringBuffer sb2=new StringBuffer();
sb2.append("hello");
sb2.append("word");
System.out.println(sb2);
}
}

敲一敲:使用 insert 方法

import java.util.Scanner;

public class TestInsert {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一串数字:");
String nums=input.next();
StringBuffer str=new StringBuffer(nums);
for (int i = str.length()-3; i >0; i-=3) {
str.insert(i, ",");
}
System.out.println(str);
}
}

搜索关注公众号「享智同行」,第一时间获取技术干货

Java入门系列-13-String 和 StringBuffer的更多相关文章

  1. java‘小秘密’系列(一)---String、StringBuffer、StringBuilder

    java'小秘密'系列(一)---String.StringBuffer.StringBuilder 前言:本系列的主题是平时容易疏忽的知识点,只有基础扎实,在编码的时候才能更注重规范和性能,在出现b ...

  2. Java入门系列之StringBuilder、StringBuffer(三)

    前言 上一节我们讲解了字符串的特性,除了字符串类外,还有两个我们也会经常用到的类,那就是StringBuffer和StringBuilder.因为字符串不可变,所以我们每次对字符串的修改比如通过连接c ...

  3. java基础解析系列(一)---String、StringBuffer、StringBuilder

    java基础解析系列(一)---String.StringBuffer.StringBuilder 前言:本系列的主题是平时容易疏忽的知识点,只有基础扎实,在编码的时候才能更注重规范和性能,在出现bu ...

  4. Java 集合系列 13 WeakHashMap

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  5. java io系列13之 BufferedOutputStream(缓冲输出流)的认知、源码和示例

    本章内容包括3个部分:BufferedOutputStream介绍,BufferedOutputStream源码,以及BufferedOutputStream使用示例. 转载请注明出处:http:// ...

  6. 夯实Java基础系列13:深入理解Java中的泛型

    目录 泛型概述 一个栗子 特性 泛型的使用方式 泛型类 泛型接口 泛型通配符 泛型方法 泛型方法的基本用法 类中的泛型方法 泛型方法与可变参数 静态方法与泛型 泛型方法总结 泛型上下边界 泛型常见面试 ...

  7. Java笔记(二十一)……String与StringBuffer

    String类 String类是一个特殊的类,叫做只读类,一旦创建了对象,便不可被改变,同样"abc"既为一个常量,也为一个对象,也是不可以改变的 String s1 = &quo ...

  8. Java容器深入浅出之String、StringBuffer、StringBuilder

    对字符串的花式处理一直是现代应用系统的主要操作之一,也是对Java基础知识考察的重要方面.事实上,Java字符串类的底层是通过数组来实现的.具体来说,String类是固定长度的数组,StringBuf ...

  9. Java入门系列-26-JDBC

    认识 JDBC JDBC (Java DataBase Connectivity) 是 Java 数据库连接技术的简称,用于连接常用数据库. Sun 公司提供了 JDBC API ,供程序员调用接口和 ...

随机推荐

  1. django drf Filter

    1.定义get_queryset()方法 from django.shortcuts import render from rest_framework.views import APIView fr ...

  2. zookeeper 开机启动

    第一种:直接修改/etc/rc.d/rc.local文件 在/etc/rc.d/rc.local文件中需要输入两行,其中export JAVA_HOME=/usr/java/jdk1.8.0_112是 ...

  3. 【OCP-12c】CUUG最新考试原题整理及答案(071-9)

    9.(5-5) choose the best answerView the Exhibit and examine the structure of the SALES and STORES tab ...

  4. luoguP3702 [SDOI2017]序列计数

    https://www.luogu.org/problemnew/show/P3702 题目让我们在 $ [1, m] $ 从中选出 $ n $ 个数,当中要有 > $ 0 $ 个质数,和是 $ ...

  5. WordPress翻译更新失败解决方法

    编辑php的配置文件:php.ini,搜索并找到disable_functions: 删除disable_functions后面的scandir字符串,保存php.ini: 重载或重启php-fpm服 ...

  6. ios处理键盘的大小

    iOS的键盘有几个通知 UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotificat ...

  7. linux开机、重启和用户登陆注销

    关机&重启命令 基本介绍: shutdown –h now    立该进行关机 shudown -h 1 "hello, 1 分钟后会关机了" shutdown –r no ...

  8. SQL chema的新增和修改

    1.先要创建你自己的schema create schema myschema 2. alter schema myschema transfer  ado.User    --执行完后,User表就 ...

  9. python3.6 for pygame安装

    首先下载好文件: pygame下载网址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 找到pygame-1.9.2b8-cp36-cp36m-wi ...

  10. 简说LINUX 下chmod|chown|chgrp和用法和区别

    1.chgrp(改变文件所属用户组) chgrp 用户组    文件名    ###就是这个格了.如果整个目录下的都改,则加-R参数用于递归. 如:chgrp  -R    user  smb.con ...