gpt4 book ai didi

windows - cmd 相当于 std::string::find_first:of

转载 作者:可可西里 更新时间:2023-11-01 09:23:10 25 4
gpt4 key购买 nike

C++、Java、JavaScript 和其他可能的编程语言都有一个字符串函数,可以在字符串中搜索指定字符串模式中的任何 字符。例如,C++ 的 std::string::find_first_of像这样工作:

std::cout << "Vowel found in : " << "Search me for vowels".find_first_of("aeiou") << std::endl;
// should print "Vowel found in : 1".

CMD 中是否有任何等效项?我尝试搜索“dos 字符串函数”,但似乎找不到任何内容。

最佳答案

没有直接的方法,但您可以很容易地编写自己的方法。

搜索一个字符

@echo off
call :charposition "Search me for vowels" a pos
echo Found a at position %pos%

goto :eof
:charposition
set "string_search=%~1"
set /a char_pos=0
:charcheck
IF %string_search:~0,1%==%2 (
endlocal & set "%3=%char_pos%"
goto :eof
)
set "string_search=%string_search:~1%"
IF "%string_search%"=="" (
set "%3=Not Found"
goto :eof
)
set /a char_pos+=1
goto :charcheck

对于多个字符:

@echo off
call :charposition "Search me for vowels" aeiou pos
echo Found vowel at position %pos%

goto :eof
:charposition
set "string_search=%~1"
set /a char_pos=0
:charcheck
echo %2|find "%string_search:~0,1%">nul
IF %errorlevel%==0 (
endlocal & set "%3=%char_pos%"
goto :eof
)
set "string_search=%string_search:~1%"
IF "%string_search%"=="" (
set "%3=Not Found"
goto :eof
)
set /a char_pos+=1
goto :charcheck

参见 http://ss64.com/nt/syntax-substring.html有关提取子字符串的语法的说明。如前所述,这些区分大小写。

关于windows - cmd 相当于 std::string::find_first:of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41521853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com