Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)

Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)

In Java

String.equals & String.equalsIgnoreCase

boolean     equals(Object anObject)
          比较此字符串与指定的对象。

 

boolean     equalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 进行比较,不考虑大小写。

 

 

if (s1.equals(s2)) {

}

注意:一定要保证s1 != null,否则会抛出异常。

 

StringUtils.equals & StringUtils.equalsIgnoreCase

在Apache Commons Lang中的StringUtils类提供了equals和equalsIgnoreCase静态方法,它的好处是两个字符串都可以是null。

org.apache.commons.lang.StringUtils equals方法 写道
public static boolean equals(String str1, String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils.equals(null, null) = true
StringUtils.equals(null, "abc") = false
StringUtils.equals("abc", null) = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false

Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case sensitive, or both null
 
org.apache.commons.lang.StringUtils equalsIgnoreCase方法 写道
public static boolean equalsIgnoreCase(String str1, String str2)

Compares two Strings, returning true if they are equal ignoring the case.

nulls are handled without exceptions. Two null references are considered equal. Comparison is case insensitive.

StringUtils.equalsIgnoreCase(null, null) = true
StringUtils.equalsIgnoreCase(null, "abc") = false
StringUtils.equalsIgnoreCase("abc", null) = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true


Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case insensitive, or both null
 

In Bash

判断字符串相等

格式1:test "$S1" = "$S2"

格式2:[ "$S1" = "$S2" ]

格式3:test "$S1" == "$S2"

格式4:[ "$S1" == "$S2" ]

格式5:[[ $S1 = $S2 ]]

格式6:[[ $S1 == $S2 ]]

 

在Bash中关于=和==的说法

man bash 写道
string1 == string2
True if the strings are equal. = may be used in place of == for strict POSIX compliance.

 

[root@web ~]# S1=Hello
[root@web ~]# S2=Hello
[root@web ~]# test "$S1" = "$S2"  && echo "equals"
equals
[root@web ~]# [ "$S1" = "$S2" ]  && echo "equals"   
equals
[root@web ~]# test "$S1" == "$S2"  && echo "equals"
equals
[root@web ~]# [ "$S1" == "$S2" ]  && echo "equals" 
equals
[root@web ~]# [[ "$S1" = "$S2" ]]  && echo "equals"
equals
[root@web ~]# [[ "$S1" == "$S2" ]]  && echo "equals"
equals
[root@web ~]#

[root@web ~]# S1=Hello
[root@web ~]# S2=hello
[root@web ~]# test "$S1" = "$S2"  && echo "equals"
[root@web ~]# [ "$S1" = "$S2" ]  && echo "equals"   
[root@web ~]# test "$S1" == "$S2"  && echo "equals"
[root@web ~]# [ "$S1" == "$S2" ]  && echo "equals" 
[root@web ~]# [[ "$S1" = "$S2" ]]  && echo "equals"
[root@web ~]# [[ "$S1" == "$S2" ]]  && echo "equals"
[root@web ~]#

 

 

判断字符串不相等

格式1:test "$S1" != "$S2"

格式2:[ "$S1" != "$S2" ]

格式3:[[ $S1 != $S2 ]]

注意在[[ ]]中,变量的引用可以不加双引号,这是与[ ]的不同之处。

也可以在判断字符串相等的基础上加上逻辑非(!)可以得到更多方法,比如:[ ! "$S1" == "$S2" ]

 

 

[root@web ~]# S1=Hello
[root@web ~]# S2=Hello
[root@web ~]# test "$S1" != "$S2" && echo "not equals"
[root@web ~]# [ "$S1" != "$S2" ] && echo "not equals"     
[root@web ~]# [[ $S1 != $S2 ]] && echo "not equals"   
[root@web ~]# S1=Hello
[root@web ~]# S2=hello
[root@web ~]# test "$S1" != "$S2" && echo "not equals"
not equals
[root@web ~]# [ "$S1" != "$S2" ] && echo "not equals"
not equals
[root@web ~]# [[ $S1 != $S2 ]] && echo "not equals"
not equals
[root@web ~]#

 

比较字符串是否相等,不区分大小写

采用shopt命令启用不区分大小写匹配模式,只在case语句和[[ ]]中有效。

man bash: shopt 写道
shopt
    Toggle the values of variables controlling optional shell behavior.

-s Enable (set) each optname.
-u Disable (unset) each optname.

nocasematch
    If set, bash matches patterns in a case-insensitive fashion when performing matching while exe-
cuting case or [[ conditional commands.
 

[root@web ~]# shopt -s nocasematch
[root@web ~]# [ "$S1" = "$S2" ] && echo "equals ignore case"
[root@web ~]# [[ "$S1" = "$S2" ]] && echo "equals ignore case"
equals ignore case
[root@web ~]# case "$S1" in "$S2") echo "equals ignore case"; esac
equals ignore case
[root@web ~]# shopt -u nocasematch
[root@web ~]# [[ "$S1" = "$S2" ]] && echo "equals ignore case"   
[root@web ~]# case "$S1" in "$S2") echo "equals ignore case"; esac
[root@web ~]#

 

包装成一个函数

equals_ignore_case(){
    shopt -s nocasematch
    [[ $1 == $2 ]]
    local rc=$?
    shopt -u nocasematch
    return $rc
}
 

[root@web ~]# equals_ignore_case "Hello" "hello" && echo "equals ignore case"
equals ignore case
[root@web ~]# equals_ignore_case "Hello" "Qello" && echo "equals ignore case"
[root@web ~]# equals_ignore_case "Hello" "hello" && echo "equals ignore case"
equals ignore case
[root@web ~]# equals_ignore_case "Hello" "helLo" && echo "equals ignore case"  
equals ignore case
[root@web ~]#

 

如果其中一个字符串是常量,可以采用 [[ ]] 中 == 的模式匹配来进行。(不用shopt开启不区分大小写)

比如:[[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]]

麻烦的地方就是后面那个匹配模式要每个字符提供大小写的形式。

 

[root@web ~]# S1=Hello
[root@web ~]# [[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]] && echo "equals ignore case"    
equals ignore case
[root@web ~]# S1=HeLlo
[root@web ~]# [[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]] && echo "equals ignore case"
equals ignore case
[root@web ~]#

 

还可以采用case语法结构。

比如:case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac

 

[root@web ~]# S1=hEllo
[root@web ~]# case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac
equals ignore case
[root@web ~]# S1=hellO
[root@web ~]# case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac
equals ignore case
[root@web ~]#

 

如果比较的双方都是变量,上面的技巧就无法做到了。

下面的方法是将两个字符串都变成大写的形式(使用tr命令),然后进行判断。

[ "$(echo "$S1" | tr [a-z] [A-Z])" == "$(echo "$S2" | tr [a-z] [A-Z])" ]

 

[root@web ~]# S1=Hello
[root@web ~]# S2=helLo
[root@web ~]# [ "$(echo "$S1" | tr [a-z] [A-Z])" == "$(echo "$S2" | tr [a-z] [A-Z])" ] && echo "equals ignore case"
equals ignore case
[root@web ~]#

 

 

 

本文链接:http://codingstandards.iteye.com/blog/1175608   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符

下节内容:Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)

 

 

第14.11章 高翔博士最新开源框架Lightning-LM源码解读(十一):离线定位模块全解析,从激光匹配到位姿图优化的极致优化 本文介绍了Lightning-LM离线定位模块的技术架构与实现细节。该模块基于3D激光雷达与IMU融合,采用改进版NDT匹配算法实现高精度定位,具备动态图层更新、多源信息融合等特性。核心流程包括数据预处理、激光里程计处理、IMU预测与点云去畸变、位姿图优化等环节,通过轻量级miao库实现高效增量优化。文章详细解析了模块的代码结构、关键函数功能及数据流,为SLAM系统的开发与优化提供了实践参考。 阅读详情

相关推荐

海康威视GigE工业相机的python调用demo

默认路径安装完成后,在路径C:\Program Files (x86)\MVS\Development\Samples\Python\BasicDemo下的BasicDemo.py中,海康威视官方提供了基本的调用方法,本文也是基于这个修改的。BasicDemo.py提供了通过枚举选择相机的方法,在官方的另一个DEMO:ConnectSpecCamera.py中提供了另一种不通过枚举,直接访问特定IP相机的方法,也可以参考。另外,如果每台相机都定义了唯一的用户定义名,也可以通过用户定义名来区分相机。

xulibo5828的博客 2107

[Bash基础] 判断字符串相等

判断字符串相等 六种方式: test "$S1" = "$S2" [ "$S1" = "$S2" ] test "$S1" == "$S2" [ "$S1" == "$S2" ] [[ $S1 = $S2 ]] [[ $S1 == $S2 ]] Note: 在[[ ]]中,变量的引用可以不加双引号;这是与[ ]的不同之处。 判断字符串不相等 test "$S1" != "$S2" [ "$S1" != "$S2" ] [[ $S1 != $S2 ]] 也可以在判断字符串相等的基

mathemagics的博客 6057

DCDC--控制方式(Control Mode)

DCDC反馈控制方式

网始如芯个人博客 1万+

bash脚本中比较字符串的方法

您是否为 Bash shell 中大量的测试和比较选项而困惑呢?这个技巧可以帮助您解密不同类型的文件、算术和字符串测试,这样您就能够知道什么时候使用 test、 [ ]、 [[ ]]、 (( )) 或 if-then-else 了。 Bash shell 在当今的许多 Linux? 和 UNIX? 系统上都可使用,是 Linux 上常见的默认 shell。Bash 包含强大的编程功能,

wlia的专栏 1万+

lstm如何输入多维序列_深度学习也可以做时间序列异常诊断

最近在看的一篇论文里,用到了GRU、VAE等技术来做多维时间序列异常诊断。那么今天首先来聊一聊深度学习的基础知识,然后简单说一下论文。以下基础知识部分的内容,主要来源于李宏毅老师的课程。有兴趣的可以去看李老师的课程哦https://www.bilibili.com/video/BV1JE411g7XF?p=1 1、GRU(Gate Recurrent Unit):GRU是循环神经网络(Recurr...

weixin_30899789的博客 2833

瑞芯微RK3506开发板Ethercat主站性能优化实战:从内核配置到微秒级延迟控制

本文详细介绍了在瑞芯微RK3506开发板上实现EtherCAT主站微秒级延迟控制的实战优化方案。核心在于部署Preempt-RT实时内核,并进行深度系统调优,包括CPU隔离、中断亲和性设置、内存锁定及电源管理调整。通过绕过Linux网络栈的专用驱动加载与配置,最终实现了稳定的高性能实时控制,满足工业自动化需求。

tcp8optimizer的博客 315

bash中执行比较的几种方法

bash 脚本中的 test 命令用于检查表达式的有效性,检查命令或表达式为 true 或者 false。上面的例子我们是使用的一行代码做的演示,除此以外,我们还可以在 bash 脚本中使用 test 命令以及 if else 分支条件。如果这两个字符串不相等,则返回0。如果两个字符串相等,则返回0;除了整数以外,也可以在 bash 中使用 test 命令比较字符串,下面我们来看一些例子。-le 运算符检查整数1是否小于或等于整数2。如果给定的表达式有效,则执行第一个命令,否则执行第二个命令。

TRY 1369

GStreamer —— 2.1、Windows搭建GStreamer环境,Qt加载GStreamer库后运行 - “教程1:Hello world!“(附:完整源码)

GStreamer —— 2、Windows搭建GStreamer环境,Qt加载GStreamer库后运行"教程1:Hello world!"

796

BASH shell脚本篇3——字符串处理

这篇博客主要是记录bash shell的字符串处理方法,便于日后复习。

W1Z1Q的专栏 1785

【学前须知】推荐阅读顺序

AUTOSAR学习笔记专栏文章推荐阅读顺序

weixin_47571898的博客 743

大数据领域数据服务的数据分析工具推荐

你有没有过这样的经历?——领导说“帮我分析一下上个月的销量”,你打开Excel却发现数据有100万行,电脑直接卡死;或者想实时知道“现在直播间的观众在买什么”,却不知道用什么工具处理每秒10万条的点击数据。本文的目的:帮你解决“选什么工具做什么事”的问题,覆盖大数据服务的全流程(采集→存储→处理→分析→可视化),推荐高频、实用、易上手的工具,并讲清楚它们的“适用场景”和“优缺点”。范围。

大数据洞察的博客 1931

Bash字符串判断

原文出处:http://codingstandards.iteye.com/blog/1175608 判断字符串相等 格式1:test "$S1" = "$S2" 格式2:[ "$S1" = "$S2" ] 格式3:test "$S1" == "$S2" 格式4:[ "$S1" == "$S2" ] 格式5:[[ $S1 = $S2 ]] 格式6:[[ $S1 == $S2 ]

韩王-信 6861

6万字嵌入式最全八股文面试题大全及参考答案(持续更新)

进程上下文定义:进程上下文包含了进程执行时所需的所有信息,包括 CPU 寄存器的值、内核栈、任务控制块(TCB)等。作用:当进程被切换时,需要保存当前进程的上下文,并恢复下一个将要运行进程的上下文。中断上下文定义:中断上下文是指处理中断时 CPU 所处的状态,包括中断发生时的寄存器值等。作用:当中断发生时,CPU 会自动保存当前的上下文信息,并跳转到相应的中断处理程序。示例表格上下文描述进程上下文包含进程执行时的所有相关信息中断上下文处理中断时 CPU 所处的状态线程同步定义。

大模型大数据攻城狮的专栏 7923

linux 判断相等,linux if 判断字符串是否相等

str1==str2和str1.Equals(str2)。两种方法都可以。#!/bin/shecho -n "login:"read nameecho -n "password:"read passwdif [ "$name" = "aa" -a "$passwd" = "aaa" ];thenecho "right!"else echo "error"fi一。运行过程中出现过[: mi...

weixin_29284885的博客 4708

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)...

Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较) In Java String.compareTo int     compareTo(String anotherString)           按字典顺序比较两个字符串。如果返回0,表示相等;<0表示小于;>0 表示大于。   int     compareToIgnor...

巨人站在肩膀上 806

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

BASH字符串处理:字符串分割为数组

http://codingstandards.iteye.com/blog/1164910 Bash字符串处理(与Java对照) - 25.字符串分割(成数组) In Java String.split  String[]     split(String regex)           根据给定的正则表达式的匹配来拆分此字符串。  String[]

zouyongjin的专栏 7763

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

java 判断包含6,Bash字符串处置(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)...

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)In JavaString.contains & String.indexOfString.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串)boolean ...

weixin_36255898的博客 162

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空) In Java 判断是否为null 是null if (s == null) {     // do something }   不是null if (s != null) {     // do something }   判断是否null或空串 是null或者空串:方法一 if...

巨人站在肩膀上 590

java判断字符串是否包含某个字符串_Bash技巧:使用[[命令的 =~ 操作符判断字符串的包含关系...

在 Linux bash shell 中,可以使用 [[ 命令来进行判断。其中,可以使用 [[ 命令的 =~ 操作符来判断某个字符串是否包含特定模式。查看 man bash 对 [[ 命令的 =~ 操作符说明如下:An additional binary operator, =~, is available, with the same precedence as == and !=.When i...

weixin_39606575的博客 487

Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾

From: http://codingstandards.iteye.com/blog/1187353 In Java String.endsWith oolean     endsWith(String suffix)           测试此字符串是否以指定的后缀结束。   StringUtils.endsWith & StringUtils.

JoeBlackZQQ的专栏 3089

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取 In Java 字符串替换  String     replace(char oldChar, char newChar)           返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。  String     replace(CharSe...

巨人站在肩膀上 633

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头 In Java String.startsWith  boolean     startsWith(String prefix)           测试此字符串是否以指定的前缀开始。  boolean     startsWith(String prefix, int toffset) ...

巨人站在肩膀上 334

Bash字符串处理(与Java对照) - 22.判断字符串是否数字串

Bash字符串处理(与Java对照) - 22.判断字符串是否数字串 In Java 用正则表达式匹配方式判断字符串是否数字串 String str = "1234"; if (str.matches("\\d+")) {     // matched, it's digit string }   In Bash 使用模式匹配(Pattern Matching)判...

巨人站在肩膀上 307

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)...

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法) In Java String.contains & String.indexOf String.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串) boolean     contains(CharSequence s)  ...

巨人站在肩膀上 398

java 字符串截取替换_Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取...

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取In Java字符串替换?String ??? replace(char oldChar, char newChar)????????? 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。?String ??? replace(CharSequence target, C...

weixin_39601641的博客 168
上一篇: Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符...
下一篇: Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)...
codingstandards
博客等级 码龄16年 12粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值