KSH检查字符串是否以子字符串开头

编程入门 行业动态 更新时间:2024-10-14 14:17:53
本文介绍了KSH检查字符串是否以子字符串开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要检查变量是否具有以指定子字符串开头的字符串值.

I need to check if the variable has value of string which starts with specified substring.

在Python中,将是这样的:

In Python it would be something like this:

foo = 'abcdef' if foo.startswith('abc'): print 'Success'

在Ksh中检查strig $foo是否以子字符串bar开头的最明确的方法是什么?

What is the most explicit way to check in Ksh whether strig $foo starts with substring bar?

推荐答案

这很简单,但看起来有点奇怪:

It's very simple but looks a bit odd:

if [[ "$foo" == abc* ]]; then ...

人们会假设ksh会使用当前目录中的文件扩展模式,但是相反,它会进行模式匹配.但是,您需要[[.单个[将不起作用.如果foo中没有空格,则引号不是必须的.

One would assume that ksh would expand the pattern with the files in the current directory but instead, it does pattern matching. You need the [[, though. Single [ won't work. The quotes are not strictly necessary if there are no blanks in foo.

更多推荐

KSH检查字符串是否以子字符串开头

本文发布于:2023-11-29 16:06:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   开头   KSH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!