Java入门基础学习笔记44——String

news2025/7/15 10:52:01

为什么要学习String的处理呢?

开发中,对字符串的处理是非常常见的。

String是什么?可以做什么?

java.lang.String 代表字符串。可以用来创建对象封装字符串数据,并对其进行处理。

1、创建对象

2、封装字符串数据

3、调用String的方法

String创建对象封装字符串数据的方法:

方式一:

Java程序中的所有字符串文字(例如:“abc”)都为此类对象。

String name = "小黑";
String SchoolName = "黑马程序员";

方式二:

调用String类的构造器初始化字符串对象。

new String()创建字符串对象,并调用构造器初始化字符串。

package cn.ensource.string;

public class StringDemo {
    public static void main(String[] args) {
        String name = "itheima";
        System.out.println(name);

        String rs1 = new String();
        System.out.println(rs1);

        String rs2 = new String("itheima");
        System.out.println(rs2);

        char[] chars = {'a', 'b', 'c'};
        String rs3 = new String(chars);
        System.out.println(rs3);

        byte[] bytes = {100, 101, 102};
        String rs4 = new String(bytes);
        System.out.println(rs4);
    }
}

运行结果:

通过构造函数创建

通过new创建的字符串对象,每一次new都会申请一个空间,虽然内容相同,但是地址值不同。

直接赋值方式创建:

以“”双引号给出的字符串,只要字符串序列相同顺序和大小相同,无论程序代码中出现几次,JVM都只会建立一个String对象,并在字符串池中维护。

String类的常用方法:

String提供的操作字符串数据的常用方法:

为什么是快速熟悉这些方法呢?

API是解决需求的,快速地认识他们,实实在在地解决业务需求。

package cn.ensource.string;

public class StringDemo2 {
    public static void main(String[] args) {
        // 目标:快速熟悉String提供的处理字符串的方法
        String s = "黑马Java";

        // 获取字符串的长度
        System.out.println(s.length());

        // 提取字符串中某个索引位置处的字符
        char c = s.charAt(1);
        System.out.println(c);

        // 字符串的遍历
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            System.out.print(ch);
        }

        System.out.println("--------");

        // 把字符串转成字符数组,然后再进行遍历
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
        }

        System.out.println("--------");

        // 判断字符串内容,内容一样,就返回true
        String s1 = new String("黑马");
        String s2 = new String("黑马");

        boolean rs = s.equals(s2);
        System.out.println(rs);

        System.out.println("--------");

        // 忽略大小写比较字符串
        String c1 = "34Aefg";
        String c2 = "24aefg";
        System.out.println(c1.equals(c2));
        System.out.println(c1.equalsIgnoreCase(c2));


        System.out.println("--------");

        // 截取字符串内容
        String s3 = "Java是最好的编程语言之一";
        System.out.println(s3.substring(0, 8));

        System.out.println("--------");

        // 截取字符串内容,从当前位置到字符串末尾
        System.out.println(s3.substring(8));

        System.out.println("--------");

        // 把字符串的某个内容,替换成新内容
        String s3c = s3.replace("Java", "C++");
        System.out.println(s3c);

        System.out.println("--------");

        // 判断字符串中是否包含某个关键字
        String info = "Java是最好的编程语言之一";
        boolean rs5 = info.contains("Java");
        System.out.println(rs5);

        // startwith
        System.out.println("--------");

        String info2 = "Java是最好的编程语言之一";
        boolean rs6 = info2.startsWith("Java");
        System.out.println(rs6);

        System.out.println("--------");

        // 分割字符串
        String str5 = "张无忌,周芷若,殷素素,赵敏";
        String[] names = str5.split(",");
        for(int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }
    }
}

split这个成员方法,之前在python中也遇到。

如果方法不再记得了,都是可以到API文档中查询的。

另外:

==:

比较基本数据类型:比较具体的值。

比较引用数据类型:比较的是对象地址值。

package com.company;


public class Main {
    public static void main(String[] args) {
        char[] chs = {'a', 'b', 'c'};
        String s1 = new String(chs);
        String s2 = new String(chs);


        String s3 = "abc";
        String s4 = "abc";


        System.out.println(s1 == s2);
        System.out.println(s1 == s4);
        System.out.println(s3 == s4);


        System.out.println("-------------");


        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s3.equals(s4));


    }
}

运行结果:

false
false
true
-------------
true
true
true

用户登录案例:

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        String username = "changchunhua";
        String password = "chang@123";


        for (int i=0; i<3; i++) {
            Scanner sc =  new Scanner(System.in);
            System.out.println("Please input username: ");
            String name = sc.nextLine();
            System.out.println("Please input password: ");
            String pwd = sc.nextLine();


            if (name.equals(username) && pwd.equals(password)) {
                System.out.println("Sign in susccessfully!");
                break;
            } else {
                if (2 - i == 0) {
                    System.out.println("Your account is locked!");
                } else {
                    System.out.println("Your has 2 - i times to sign in.");
                }
            }
        }
    }
}

运行结果: 

Please input username:
chang
Please input password:
chang@123
Your has 2 - i times to sign in.
Please input username:
changchun
Please input password:
chang@123
Your has 2 - i times to sign in.
Please input username:
changchunhua
Please input password:
chang@123
Sign in susccessfully!

字符串反转:

package com.company;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input a string: ");
        String line = sc.nextLine();
        String s = reverse(line);
        System.out.println("s: " + s);
    }


    public static String reverse(String s) {
        String ss = "";


        for(int i=s.length()-1; i>=0; i--) {
            ss += s.charAt(i);
        }
        return ss;
    }
}

运行结果:

Please input a string:
changchunhua
s: auhnuhcgnahc

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1695129.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

超过GPT4.0?Claude3官网及国内镜像站,国内使用克劳德3的方法

近期又有一个大模型横空出世&#xff0c;这就是由Anthropic公司推出的Claude 3&#xff08;克劳德3&#xff09;&#xff0c;在多项基准测试中得分超越了GPT-4&#xff0c;那么他到底是什么情况呐&#xff1f;其实大家在国内也是可以使用上的&#xff01; 克劳德Claude3 关于…

Python 闭包的高级用法详解

所谓闭包&#xff0c;就是指内函数使用了外函数的局部变量&#xff0c;并且外函数把内函数返回出来的过程&#xff0c;这个内函数称之为闭包函数。可以理解为是函数式编程中的封装。 内部函数可以使用外部函数定义的属性&#xff1a;外部函数调用后&#xff0c;返回内部函数的地…

Java入门基础学习笔记36——面向对象基础

面向对象编程快速入门&#xff1a; 计算机是用来处理数据的。 单个变量 数组变量 对象数据 Student类&#xff1a; package cn.ensource.object;public class Student {String name;double chinese_score;double math_score;public void printTotalScore() {System.out.pr…

AUTOMATIC1111/stable-diffusion-webui/stable-diffusion-webui-v1.9.3

配置环境介绍 目前平台集成了 Stable Diffusion WebUI 的官方镜像&#xff0c;该镜像中整合如下资源&#xff1a; GpuMall智算云 | 省钱、好用、弹性。租GPU就上GpuMall,面向AI开发者的GPU云平台 Stable Diffusion WebUI版本&#xff1a;v1.9.3 Python版本&#xff1a;3.10.…

HCIE是什么证书?为什么要考?

每当我发一些关于HCIE的话题时&#xff0c;总有小伙伴过来问“啥是HCIE啊&#xff1f;”今天就一起来了解下&#xff0c;到底什么是HCIE&#xff1f;为什么这么多人都要考HCIE? HCIE是华为认证ICT专家的缩写&#xff0c;它是华为认证体系中最高级别的ICT技术认证。HCIE全称为H…

windows 设置系统字体 (win11 win10)

由于微软的字体是有版权的&#xff0c;所以我打算替换掉 1.下载替换工具 github的项目&#xff0c;看起来很多人对微软默认字体带版权深恶痛绝。 项目地址&#xff1a;nomeiryoUi地址 这里选取最新的版本即可 2.打开软件 这里显示标题栏不能改&#xff0c;确认&#xff0c;其…

使用Systemd 设置Python程序开机启动

在 Linux 系统中设置Python 脚本开机启动&#xff0c;通常可以通过以下几种方式实现&#xff1a; 1. 使用 systemd&#xff08;推荐方式&#xff09; systemd 是大多数现代 Linux 发行版使用的初始化系统和服务管理器。你可以为Python 脚本创建一个 systemd 服务文件&#xf…

鸿蒙ArkUI-X平台差异化:【运行态差异化(@ohos.deviceInfo)】

平台差异化 简介 跨平台使用场景是一套ArkTS代码运行在多个终端设备上&#xff0c;如Android、iOS、OpenHarmony&#xff08;含基于OpenHarmony发行的商业版&#xff0c;如HarmonyOS Next&#xff09;。当不同平台业务逻辑不同&#xff0c;或使用了不支持跨平台的API&#xf…

Android开发-Android开发中的TCP与UDP通信策略的实现

Android 开发中的 TCP 与 UDP 通信策略的实现 1. 前言2. 准备工作3. Kotlin 中 TCP 通信实现客户端代码示例&#xff1a;服务器代码示例&#xff1a; 4. Kotlin 中 UDP 通信实现客户端代码示例&#xff1a;服务器代码示例&#xff1a; 5. TCP 与 UDP 应用场景分析TCP 实现可靠传…

词条唤夜兽唤夜兽的养殖与护理 幻兽帕鲁 唤夜兽怎么获取 唤夜兽去哪里抓 crossover玩Steam游戏

唤夜兽在地图上没有出现&#xff0c;是唤冬兽和雷冥鸟共同培育出来的帕鲁。 ------------------------- 介绍&#xff1a; 帕洛斯群岛之守护神&#xff0c;拥呼唤黑夜之力。 其会于灾厄席捲大地之际腾空而起&#xff0c;唤来无尽暗夜&#xff0c;试图封印灾厄。 ---------…

Video-FocalNets: Spatio-Temporal Focal Modulation for Video Action Recognition

标题&#xff1a;Video-FocalNets&#xff1a;用于视频动作识别的时空聚焦调制 源文链接&#xff1a;Wasim_Video-FocalNets_Spatio-Temporal_Focal_Modulation_for_Video_Action_Recognition_ICCV_2023_paper.pdf (thecvf.com)https://openaccess.thecvf.com/content/ICCV202…

深入解析文华量化交易策略---交易指令如何选择

随着金融投资的迅猛发展&#xff0c;自动化策略模型已逐渐成为现代投资领域的一股重要力量。量化交易模型均以数据为驱动&#xff0c;通过运用数学模型和算法&#xff0c;对期货、黄金等投资市场走势进行精准预测和高效交易。 艾云策略整理了量化策略相关资料&#xff0c;希望通…

从零开始搭建一个SpringBoot项目

目录 Spring BootSpring Boot 项目开发环境1、快速创建SpringBoot项目2、pom.xml 添加 Meavn 依赖3、配置application.yml4、验证数据库是否连接成功5、配置 Druid 数据源 Spring Boot 整合 MyBatis1、准备依赖2、application-dev.yml 配置3、启动类添加Mapper接口扫描器4、设置…

【Python搞定车载自动化测试】——Python基于Pytest框架实现UDS诊断自动化(含Python源码)

系列文章目录 【Python搞定车载自动化测试】系列文章目录汇总 文章目录 系列文章目录&#x1f4af;&#x1f4af;&#x1f4af; 前言&#x1f4af;&#x1f4af;&#x1f4af;一、环境搭建1.软件环境2.硬件环境 二、目录结构三、源码展示1.诊断基础函数方法2.诊断业务函数方法…

09.自注意力机制

文章目录 输入输出运行如何运行解决关联性attention score额外的Q K V Multi-head self-attentionPositional EncodingTruncated Self-attention影像处理vs CNNvs RNN图上的应用 输入 输出 运行 链接&#xff08;Attention Is All You Need&#xff09; 如何运行 解决关联性 a…

Iphone自动化指令每隔固定天数打开闹钟关闭闹钟

1.业务需求&#xff1a;小z每隔五天有一个夜班&#xff0c;然后下午会有三个小时的休息时间&#xff0c;如果闹钟不响就会错过交班日期&#xff0c;但是如果设置闹钟&#xff0c;iPhone的闹钟只能设定固定循环日期闹钟&#xff0c;或者一次的闹钟&#xff0c;导致要么忘记设闹钟…

【网络安全】社会工程学攻击与防范

一、社会工程学概述 1、社会工程学的定义 通过利用人们的心理弱点、本能反应、好奇心、信任、贪婪等一些心理陷阱进行的诸如欺骗、伤害、信息盗取、利益谋取等对社会及人类带来危害的行为或方法。 当网络恶意攻击者无法通过纯粹的计算机技术达到目的时&#xff0c;高超的情商…

【Text2SQL】WikiSQL 数据集与 Seq2SQL 模型

论文&#xff1a;Seq2SQL: Generating Structured Queries from Natural Language using Reinforcement Learning ⭐⭐⭐⭐⭐ ICLR 2018 Dataset: github.com/salesforce/WikiSQL Code&#xff1a;Seq2SQL 模型实现 一、论文速读 本文提出了 Text2SQL 方向的一个经典数据集 —…

Amesim应用篇-电芯等效电路模型标定

前言 为了使计算模型更加准确,在有电芯实验测试数据的情况下,依据现有的实验数据对Amesim中的电池等效电路模型进行标定。标定的目的是为了获得更加符合项目实际情况的电芯等效电路模型,标定完的电芯可以用于搭建PACK模型,也可以用于其他虚拟实验。本文以充电标定为例,进…

ideal 启动 多个 相同 工程

spring相同项目在idea多次运行 点击IDEA右上角项目的隐藏下拉框&#xff0c;出现下拉列表&#xff0c;点击Edit Configurations 弹出Run/Debug Configuration对话框&#xff0c;勾选Allow parallel run