首页 > Java > java教程 > 正文

Java 字符串运算符

PHPz
发布: 2024-08-30 15:20:11
原创
491 人浏览过

下面的文章《Java 字符串运算符》概述了 Java 字符串中使用的运算符和方法。字符串通常是字符序列,可以是文字常量,也可以是某种变量。在Java中,字符串被视为对象,Java平台提供了String类来创建和操作此类字符串。 Java String 是使用最广泛的类之一,定义在 java.lang 包中。运算符通常是要求编译器执行特定数学或逻辑操作的符号。它们将输入作为操作数并返回一些值作为输出。 java中的运算符也类似于用于执行运算的符号。例如:+、-、*、/等

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java 字符串操作

该字符串与双引号文本形式的字符串文字相关联,例如“Hello, world!”。这样,我们就可以直接将字符串赋值给 String 变量,而不需要调用构造函数来创建 String 实例。

在Java中,String是唯一允许运算符重载的类。例如,我们可以使用 + 运算符连接两个字符串。

例如:

"a"+"b"=" ab"
登录后复制

创建Java字符串对象的两种方法:-

1。使用字符串文字: Java 字符串文字可以使用双引号创建。例如:

String s="Hello";
登录后复制

2。使用 new 关键字: Java 字符串也可以使用关键字“new”创建。例如:

String s=new String("Hello");
登录后复制

Java String 类实现了三个接口,即 – Serialized、Comparable 和 CharSequence 由于 Java 字符串是不可变且固定的,因此每当我们需要进行字符串操作时,我们都需要创建一个新字符串。由于字符串操作会消耗资源,因此java提供了两个实用程序类:StringBuffer和StringBuilder。使用这两个实用程序类,操作java字符串变得更加容易。让我们看一些例子。

String 类中的方法

让我们看看 String 类中的方法。

1.字符串长度

Java String 基本上是一个对象,它曾经具有对字符串执行某些操作的方法。举个例子,借助“length()”方法,我们可以找到字符串的长度。 示例

public class MyClass
public static void main(String[] args) {
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int len = txt.length();
System.out.println("The length of the txt string is: " + len);
}
}
登录后复制

输出:

Java 字符串运算符

2.大写和小写

要使字符串大写和小写,字符串方法是:toUpperCase() 和 toLowerCase()

示例:

public class MyClass {
public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}
登录后复制

输出:

Java 字符串运算符

3.在给定字符串中查找索引

我们可以使用“indexOf()”方法在Java中找到给定字符串的索引。它返回特定文本第 1st 次出现的索引位置,其中也包括空格。

代码:

public class MyClass {
public static void main(String[] args) {
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate"));
}
}
登录后复制

输出:

Java 字符串运算符

注意:在 Java 中,计数位置被认为是从零 (0) 开始。这意味着“0”是给定字符串中的第一个或第一个位置,“1”被认为是第二个位置,“2”被认为是第 3rd 位置,而且很快就会消失。

4. Java 字符串连接

如果我们考虑运算符“+”,它用于数字加法(在java字符串连接中),意思是“在一起”,那么在Java中对于字符串,我们可以使用相同的字符串加法来创建一个新字符串。该操作称为字符串连接。

示例#1

代码:

public class MyClass {
public static void main(String[] args) {
String firstName = "Raju";
String lastName = "Raj";
System.out.println(firstName + " " + lastName);
}
}
登录后复制

输出:

Java 字符串运算符

注意我们添加了一个空文本(“”),以便在打印时在名字和姓氏之间创建一个空格。

我们还可以使用 concat() 方法来连接两个字符串:

示例 #2

代码:

public class MyClass {
public static void main(String[] args) {
String firstName = "Raju ";
String lastName = "Raj";
System.out.println(firstName.concat(lastName));
}
}
登录后复制

输出:

Java 字符串运算符

5.修剪字符串

如果我们有一个字符串的开头和结尾都有空格,这个方法将帮助我们删除它们。

Example:

Code:

class StringTrim{
public static void main(String args[]){
String s1 = new String(" AbodeQA ");
s1 = s1.trim();
System.out.println(s1);
}
}
登录后复制

Output:

Java 字符串运算符

Java String Class Methods

The java.lang.string class provides many useful methods to perform operations.

Below are some of the methods which are supported by the String class:

Method

Description

char charAt(int index) Returns char value for the index
String concat(String str) It concatenates the string to the end
boolean equals(Object another) Checks the equality of string with the given
int compareTo(Object o) Compares the string to other objects
static String format(String format, Object… args) It returns a string that is formatted
boolean endsWith(String suffix) For testing the string if it ends with suffix or not
byte getBytes() Encodes a string to a sequence of bytes
int indexOf(int ch) Returns the char value index
boolean isEmpty() It checks if the string is empty or not
int lastIndexOf(String regex) Returns index of the rightmost occurrence
String intern() It returns interned string
int length() Returns length of the sting
int hashCode() It returns the hash code
boolean matches(String regex) Checks if a string matches the regular expression
String trim() It removes the starting & ending spaces of the string
String[] split(String regex) It returns a split string to matching a regex
String toLowerCase() Returns string to lowercase
String substring(int beginIndex) It returns the substring for the starting index
String toUpperCase() Returns string to uppercase
String replace(char old, char new) It replaces the occurrences of the char value

以上是Java 字符串运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!