使用Java中的equals()和compareTo()方法比较字符串

Given strings and we have to compare them using equals() and compareTo() method.

给定字符串,我们必须使用equals()和compareTo()方法进行比较。

  • Java string equals() method

    Java字符串equals()方法

    Java string equals() method compares the content of two strings, If all characters are the same, it returns

    Java字符串equals()方法比较两个字符串的内容,如果所有字符都相同,则返回

    true, else it returns false.

    是true ,否则返回false

  • Java string compareTo() method

    Java字符串compareTo()方法

    Java string compareTo() method is called with a string and another string is supplied as an argument, it compares the strings based on the Unicode values of the characters in the strings. It returns a

    使用字符串调用Java字符串compareTo()方法,并提供另一个字符串作为参数,它根据字符串中字符的Unicode值比较字符串。 它返回一个

    positive number, negative number or 0. If both strings have the same content, it returns 0.

    正数负数0 。 如果两个字符串的内容相同,则返回0

Java代码使用equals(),compareTo()和==运算符比较字符串 (Java code to compare the strings using equals(), compareTo() and == operator )

// Comparing Strings with equals() and compareTo() 
// methods in Java
public class Main {
public static void main(String[] args) {
//strings
String str1 = new String("ABC");
String str2 = new String("PQR");
//comparing strings using equals() method
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str1));
//comparing strings using == operator 
System.out.println(str1 == str1);
System.out.println(str1 == str2);
//comparing strings using compareTo() method
System.out.println(str1.compareTo(str1));
System.out.println(str1.compareTo(str2));
}
}

Output

输出量

false
true
true
false
0
-15

翻译自: https://www.includehelp.com/java-programs/comparing-strings-with-equals-and-compareto-methods-in-java.aspx

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

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

相关文章

poj 2528_2

代码&#xff1a; #include<iostream> #include<fstream>using namespace std;struct e{int l,r;bool isa; };e tree[80001]; int n;struct f{int num,s,l; };f b[20001];int c[10001][2];int cmp(const void *a,const void *b){f *s(f*)a;f *t(f*)b;return s->…

linux 命令修改网络,linux网络命令学习

用命令修改的均为临时生效&#xff0c;重启后失效&#xff0c;要想永久生效需更改配置文件设置主机名vi etc/sysconfig/network查看主机名hostname设置IP地址ifconfig 网卡名 IP地址 netmask 子网掩码ifconfig etho 10.0.0.1 netmask 255.0.0.0查看某快网卡的状况ifconfig 网卡…

素数求和问题

描述 现在给你N个数(0< N<1000) ,现在要求你写出一个程序&#xff0c;找出这N个数中的所有素数&#xff0c;并求和。 输入 第一行给出整数M(0< M<10)代表多少组测试数据 每组测试数据第一行给你N&#xff0c;代表该组测试数据的数量。 接下来的N个数为要测试的…

ITIL(信息技术基础构架库)!

新找到一个网址&#xff1a;www.itsm.info&#xff0c;英文的&#xff01; 转载于:https://www.cnblogs.com/tankhorse/archive/2006/11/09/555512.html

常用汇编浮点操作指令

如果不知道ST&#xff08;0&#xff09;&#xff0c;可以看这篇文章&#xff1a;FPU数据寄存器 浮点数载入指令&#xff1a; fld src ;将浮点数src压入ST&#xff08;0&#xff09; fild src ;将整数src压入ST&#xff08;0&#xff09; ;f&#xff1a;float&#xff08;浮点…

是雏还是鹰mdash;mdash;编程规范之代码注释

注释能使代码更加容易理解&#xff0c;更加容易跟踪。出色的注释就像一幅好的设计蓝图&#xff0c;能够引导阅读者通过你的应用程序的曲折之处&#xff0c;能够说明预期的运行结果和可能出现的异常情况。注释可以为后来的维护者带来极大的方便&#xff0c;无论是类说明注释&…

Java PropertyPermission newPermissionCollection()方法与示例

PropertyPermission类newPermissionCollection()方法 (PropertyPermission Class newPermissionCollection() method) newPermissionCollection() method is available in java.util package. newPermissionCollection()方法在java.util包中可用。 newPermissionCollection() m…

C#多线程学习笔记(三)

刚刚把前两天的笔记整理完了&#xff0c;发现做笔记可以加深印象。要坚持做下去&#xff0c;可以学到一些细节的东西。a.今天学到一个非常试用的lock语法:lock(expression) statement_block expression代表你希望跟踪的对象&#xff0c;通常是对象引用。一般地&#xff0c;如果…

linux ida 图形界面,linux – IDA在屏幕内不起作用

我试图在屏幕会话中运行idal64(IDA pro),但是我收到此错误&#xff1a;TVision error: Can not load libcurses.soWithout libcurses can work only with xterm/linuxAborted (core dumped)我安装了’libncurses5-dev’,’libncursesw5-dev’,’lib32ncurses5-dev’和’libx32n…

C和汇编混合编程----printf

今天终于用c和汇编成功调试出第一个程序了&#xff0c;程序很简单&#xff0c;我太菜了&#xff0c;花了几天的时间&#xff0c;才调试好&#xff0c;来记录一下&#xff0c;以防忘记了 先上程序&#xff1a; #include "stdio.h" int main() {char *str"begin\…

(诡异事件)iframe标签后面的alert不执行

今天做项目的过程中&#xff0c;发现一个非常奇怪的事情。iframe标签后面的js 不执行&#xff1f; 把代码贴出来&#xff0c;看看有没有大牛碰到这种情况。谢谢。 <html xmlns"http://www.w3.org/1999/xhtml"><head> <title>Untitled Page</…

kotlin 判断数字_Kotlin程序检查给定数字是正数,负数还是零

kotlin 判断数字A positive number is a number which is greater than 0, a negative number is a number which is less than 0, else the number is Zero. 正数是大于0的数字&#xff0c;负数是小于0的数字&#xff0c;否则为零。 Given a number num, we have to check wh…

gz格式linux怎么打开,linux 下载解压gz文件怎么打开

一般用WINRAR下载地址&#xff1a;http://www.onlinedown.net/soft/5.htm如何下载安装winrar并打开“压缩文件”如何下载、安装winrar&#xff0c;并打开“压缩文件”网上有很多文件都是压缩格式&#xff0c;经过压缩后的文件由于其体积较小&#xff0c;因而比较适合网络传输&a…

c和汇编混合编程----main的反汇编

想看一下main函数的反汇编程序&#xff0c;分析一下&#xff1a; int main() {return 0; } 反汇编&#xff1a; 1: int main() 2: { 00401010 55 push ebp &#xff1b;将ebp压入栈 00401011 8B EC mov ebp,esp &am…

转 商业软件编程很无聊?

这周读到三篇博客帖子。把它们串在一块儿读&#xff0c;对我们的职业发展非常有教育意义。 一篇是Thoughtworks前员工Ravi Mohan写的&#xff0c;《但是马老大&#xff0c;商业编程就是无聊》。Martin Fowler在一篇帖子里说&#xff0c;编写企业软件不光是捣腾数据。并不是只有…

外中断02 - 零基础入门学习汇编语言70

第十五章&#xff1a;外中断02 让编程改变世界 Change the world by program 小甲鱼和大家谈谈心 一个帖子引发小甲鱼的反省&#xff01; 猫姐曾经说过&#xff0c;步子别迈太大&#xff0c;容易扯着蛋&#xff01; 结果还真蛋疼了…… 因此&#xff0c;小甲鱼要学会淡定…

蛇形填数

描述 在n*n方阵里填入1,2,…,n*n,要求填成蛇形。例如n4时方陈为&#xff1a; 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 输入 直接输入方陈的维数&#xff0c;即n的值。(n<100) 输出 输出结果是蛇形方陈。 样例输入 3 样例输出 7 8 1 6 9 2 5 4 3 代码如下 …

c#数组获取元素的索引_获取元素集合 从C#中的指定索引

c#数组获取元素的索引Given a Collection<T> of integer types, and an index, we have to access the element from the given index. 给定一个整数类型的Collection <T>和一个索引&#xff0c;我们必须从给定索引访问元素。 To access an element of the Collec…

linux清理缓存cache,Linux清理cache缓存

当cache缓存占用太大&#xff0c;服务起不来&#xff0c;需要查看清理缓存查看缓存&#xff1a;free -m输入运行下面一行&#xff1a;echo 3 > /proc/sys/vm/drop_caches# 释放缓存区内存的方法1)清理pagecache(页面缓存)# echo 1 > /proc/sys/vm/drop_caches 或者 # sys…

c和汇编混合编程----shellcode----弹出计算器

先用c和汇编混合编程成功弹出计算器&#xff08;在VC里运行&#xff09; #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoa…