Java基础知识(六) 字符串

devtools/2024/10/21 9:58:58/

六 字符串

6.1 String字符串

1、String类对象创建

定义String类对象格式:**
1)String 字符串变量名=“字符串常量”;
2)String 字符串变量名=new String(字符串常量);
3)String 字符串变量名;
字符串变量名=“字符串常量”;

java">public class StringDemo {public static void main(String[] args) {String s1="java is beautiful";//字符串变量String s2=new String("hello java!");String s3;s3="JAVA";System.out.println(s1);System.out.println(s2);System.out.println(s3);}}

2、String类对象的常用基本方法

字符串变量通过点“.”方式调用,即字符串变量.方法名()

java">1、获取字符串长度方法
length()
2、字符串拼接
concat()
3、字符串比较
equals()
4、字符串代替
replace()
5、去掉字符串首尾空格,返回字符串
trim()
6、返回字符串对象
toString()
7、返回字符串中所有字符转变为大写的新字符串
toUpperCase()
8、获取给定的index处字符串
charAt()public class StringDemo {public static void main(String[] args) {String s1="java is beautiful";//字符串变量String s2=new String("hello java!");String s3;s3="JAVA";String s4 =" java and python ";System.out.println(s1);System.out.println(s2);System.out.println(s3);System.out.println("========================");//获取字符串的长度System.out.println("s1的长度为:"+s1.length());//字符串拼接System.out.println(s1.concat(s2));//s2拼接到s1中//字符串比较System.out.println("JAVA".equals(s3));//两个字符串比较,忽略大小写System.out.println("java".equalsIgnoreCase(s3));//字符串代替 replaceSystem.out.println(s1.replace("java", "JAVA"));//去掉字符串首尾空格,返回字符串System.out.println(s4.trim());//返回字符串对象System.out.println(s4.toString());//返回字符串中所有字符转变为大写的新字符串System.out.println(s4.toUpperCase());//获取给定的index处字符串System.out.println(s4.charAt(1));}}运行结果:
java is beautiful
hello java!
JAVA
========================
s1的长度为:17
java is beautifulhello java!
true
true
JAVA is beautiful
java and pythonjava and python JAVA AND PYTHON 
j

3、遍历字符串中的所有字符,并打印

java">public class BianliDemo {public static void main(String[] args) {String s1="java is beautiful";for(int i=0;i<s1.length();i++) {System.out.print(s1.charAt(i));}}}运行结果:
java is beautiful

4、单词计数案例

java">方法一:import java.util.Scanner;// 统计单词的个数。
class CountWords {public static void main(String[] args) {// 输入Scanner sc = new Scanner(System.in);// 输入一行(可以接收一行的数据)String str = sc.nextLine();// 默认有0个单词int wordcount=0;// 从第一个到最后一个字符for(int i=0;i<str.length();i++){// 如果遇到了空格表示一个单词的结束。if(str.charAt(i)==' '){// 累加wordcount++;}}// 输出字符串System.out.println(str);// 输出单词的个数。System.out.println(wordcount+1);}
}方法二:
import java.util.Scanner;public class WordCount {public static void main(String[] args) {//计数器int count = 0 ;System.out.println("输入一系列以空格隔开的字符串");Scanner sc = new Scanner(System.in);//忽略字符串头尾的空白String str = sc.nextLine().trim();String[]  words = str.split(" ");for(int i = 0 ; i < words.length; i++) {if(!words[i].equals("")) {//不为空格时才计数(即只计算单词数)count++;}}System.out.println("一共有"+count+"个单词");sc.close();}}

6.2 StringBuffer类

当String类字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题线程安全的可变字符序列。

1、StringBuffer类对象创建

定义StringBuffer类对象格式:
1)StringBuffer 字符串变量名 =new StringBuffer();//默认长度16字符
2)StringBuffer 字符串变量名 =new StringBuffer(缓冲区长度);
3)StringBuffer 字符串变量名 =new StringBuffer(“字符串常量”);

java">public class StringBufferDemo {public static void main(String[] args) {StringBuffer s2 =new StringBuffer();//默认长度16字符StringBuffer s3 =new StringBuffer(50);//长度为50StringBuffer s4 =new StringBuffer("i like java");System.out.println("s2="+s2);System.out.println("s3="+s3);System.out.println("s4="+s4);}}public class StringBufferDemo {public static void main(String[] args) {StringBuffer s1 =new StringBuffer();//默认长度16字符System.out.println("s1的缓冲区长度:"+s1.capacity());//查看对象缓冲区长度System.out.println("s1的长度:"+s1.length());StringBuffer s2 =new StringBuffer(50);//缓冲区长度为50System.out.println("s2的缓冲区长度:"+s2.capacity());//查看对象缓冲区长度System.out.println("s2的长度:"+s2.length());StringBuffer s3 =new StringBuffer("i like java python");System.out.println("s3的缓冲区长度:"+s3.capacity());//查看对象缓冲区长度  16+11=27System.out.println("s3的长度:"+s3.length());}}运行结果:
s1的缓冲区长度:16
s1的长度:0
s2的缓冲区长度:50
s2的长度:0
s3的缓冲区长度:34//16+18
s3的长度:18

2、StringBuffer类成员方法

java">1append():添加、增加public class ChengYuanWay {public static void main(String[] args) {//append:添加StringBuffer S1 =new StringBuffer("i like java");System.out.println("原来的S1:"+S1);//append()  :S1.append()S1.append(",html,web");System.out.println("添加后的S1:"+S1);}}注意:该方法多次重写public class ChengYuanWay {public static void main(String[] args) {//append:添加StringBuffer S1 =new StringBuffer("i like java");System.out.println("原来的S1:"+S1);//append()  :S1.append()S1.append(",html,web").append(",python").append(",php");System.out.println("添加后的S1:"+S1);}}
public class ChengYuanWay {public static void main(String[] args) {//append:添加StringBuffer S1 =new StringBuffer("i like java");System.out.println("原来的S1:"+S1);//append()  :S1.append()S1.append(",html,web").append(",python").append(",php语言").append(12.67);System.out.println("添加后的S1:"+S1);}}2insert():在指定的索引上,插入字符串public class InsertDemo {public static void main(String[] args) {StringBuffer S=new StringBuffer("abcd  祖国  ");System.out.println("原来的S:"+S);//insert添加元素  1)找到位置?  找到位置之后在这个位置添加我们要添加的元素S.insert(0, "F").insert(3, "G");//a索引0:0就是它的位置System.out.println("添加元素后的S:"+S);}
}append()insert()区别:a、append()只能在字符串的结尾添加,而insert()在指定的索引上,插入字符串b、insert()两个参数,第一个参数索引(位置),第二参数你要添加什么值  通过索引值找到位置才能添加3deleteCharAt(int index):删除指定索引的值4delete(int start,int end)删除此序列的子字符串中的字符,子串开始于指定start并延伸到字符索引end - 15)replace:替换   6)reverse:反转7)substring:截取
public class StringBufferDemo {public static void main(String[] args) {// replace:替换StringBuffer s =new StringBuffer();s.append("hello java");System.out.println(s);System.out.println(s.length());s.replace(6, 10, "html");System.out.println("替换之后的s:"+s);//反转:reverse()StringBuffer s1=new StringBuffer();s1.append("我爱你");System.out.println(s1);s1.reverse();System.out.println("反转之后的s1:"+s1);//截取:substringStringBuffer s2=new StringBuffer();s2.append("java").append(" html").append(" php").append(" web").append("python");System.out.println(s2);String s3= s2.substring(4, 10);System.out.println(s3);}}

3、统计大、小写以及数字出现次数案例

java">案例:键盘输入字符串(absDGHS345;;;),统计该字符串中的大写字母、小写字母、数字字符出现的次数import java.util.Scanner;public class CountStringDemo {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc =new Scanner(System.in);System.out.println("请输入一串字符串:");String str=sc.next();int bigc=0;int smallc=0;int numc=0;for(int i=0;i<str.length();i++) {char c=str.charAt(i);if(c>='a' &&c<='z') {smallc++;}else if (c>='A' &&c<='Z') {bigc++;}else if (c>='0' &&c<='9') {numc++;}}System.out.println("大写字母有"+bigc+"个");System.out.println("小写字母有"+smallc+"个");System.out.println("数字有"+numc+"个");}}

http://www.ppmy.cn/devtools/40900.html

相关文章

【C#】与cpp异同总结

1.类外部调用类内的静态变量和静态成员 C#中类内定义的静态变量和静态方法&#xff0c;在类外访问是用类名.变量 或者 类名.方法 public class MyClass {public static int MyStaticVariable 10;public static void MyStaticMethod(){Console.WriteLine("This is a sta…

kali搭建Vulhub靶场

简单概述 Vulhub是一个面向大众的开源漏洞靶场&#xff0c;借助Docker简单执行两条命令即可编译、运行一个完整的漏洞靶场镜像。旨在让漏洞复现变得更加简单&#xff0c;让安全研究者更加专注于漏洞原理本身。 Docker是一个开源的容器引擎&#xff0c;它有助于更快地交付应用…

从零手写实现 tomcat-06-servlet bio/thread/nio/netty 池化处理

创作缘由 平时使用 tomcat 等 web 服务器不可谓不多&#xff0c;但是一直一知半解。 于是想着自己实现一个简单版本&#xff0c;学习一下 tomcat 的精髓。 系列教程 从零手写实现 apache Tomcat-01-入门介绍 从零手写实现 apache Tomcat-02-web.xml 入门详细介绍 从零手写…

基于springboot+vue+Mysql的音乐翻唱与分享平台

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

Minio(官方docker版)容器部署时区问题研究记录

文章目录 感慨&概述补充&#xff1a;MINIO_REGION和容器时间的关系 问题一&#xff1a;minio容器和本地容器时间不一致问题说明原因探究解决方法结果验证 问题二&#xff1a;minio修改时间和本地查询结果不一致具体问题原因探究解决办法时间转化工具类调用测试和验证上传文…

【已解决】chrome/其他浏览器:你的连接不是私密连接

一、问题原因 这个问题一般是https的网址会有&#xff0c;它是安全的访问连接&#xff0c;在原来的基础上SSL/TLS协议进行加密和身份验证。 这里可能是证书过期了&#xff0c;其实 二、解决方法【亲测有效】 方法1&#xff1a;点击高级&#xff0c;如果有继续访问对应的http…

IPFoxy Tips:什么是静态住宅IP?静态ISP代理指南

静态住宅代理&#xff08;也称为静态ISP代理&#xff09;是最流行的代理类型之一。它们也是隐藏您的身份并保持在线匿名的最佳方法之一。您为什么要使用住宅代理而不是仅使用常规代理服务&#xff1f;下面我具体分享。 一、什么是静态住宅代理&#xff1f; 首先&#xff0c;我…

想让别人帮你,就要把对方变成“利益共同体”

目录 1.麻烦别人是一种拉近关系的方式 2.大众认为所谓的正确不会让你活得很好