有什么方法可以在 Mac OS X 下的 Bash 中找到字符串中第一个字符的位置?
类似于:
stringZ=abcABC123ABCabc # 6
echo `expr index "$stringZ" C12` # C position.
如 Advanced Bash-Scripting Guide 中所述
几个问题:
- 官方的 index 函数
expr index $string $substring
不是 存在于 OS X (BSD) 匹配 - 安装 gnu match (gmatch) 似乎不是一个 BSD 系统领域的可移植解决方案
有什么想法吗?
最佳答案
这是一个可怕的 hack,可能不适用于所有情况。
tmp=${stringZ%%C12*} # Remove the search string and everything after it
echo $(( ${#tmp} + 1 )) # Add one to the length of the remaining prefix
关于macos - Bash:在 OS X 下的字符串中查找字符的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17615750/