在字符(如果存在)之后提取子字符串,否则返回CMD中的整个字符串


0

我试图找到最简单的方法,在CMD中,在字符串中找到之后,/或者\以最后一个,如果找到任何一个,提取。

例如:

输入

apiserver_sdk

预期

apiserver_sdk

要么

输入

D:/dev/some/folder/GO/projectName

预期

projectName

我对来自实际编程语言的substr,substring非常熟悉,但我不确定它是否像CMD这样的shell语言。


字符串是/或\ 始终是文件路径吗?
迈克尔·哈维

在我的用例中,是的。
Mike Warren

Answers:


0

这是一个应该工作的方法:

  echo %string% | findstr " / \ " >nul && for %%A in (%string%) do set string=%%~nA

before: apiserver_sdk  
after:  apiserver_sdk  

before: D:/dev/some/folder/GO/projectName
after:  projectName

before: c:\one\two three\four five
after:  five

before: "c:\one\two three\four five"
after:  four five
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.