gpt4 book ai didi

batch-file - 检查字符串是否被引用并向字符串添加引号的问题

转载 作者:行者123 更新时间:2023-12-04 23:19:34 25 4
gpt4 key购买 nike

我试图通过检查字符串的第一个和最后一个字符来检查字符串是否被引用。但是我的脚本在检查引用时失败了,请查看输出:此时是意外的。以下。

代码

@echo off

set mystring=Non quoted string

set myquotedstring=^"My quoted string^"

echo mystring: %mystring%

echo myquotedstring: %myquotedstring%

set result=%mystring:~0,1%

echo first character of non quoted string is: %result%

set result=%mystring:~-1%

echo last character of non quoted string is: %result%

if %mystring:~0,1%u==^" AND %mystring:~-1%==^" (
echo this string is NOT quoted
set newstring=^"Non quoted string^"
echo newstring: %newstring%
)

set result=%myquotedstring:~0,1%

echo first character of quoted string is: %result%

set result=%myquotedstring:~-1%

echo last character of quoted string is: %result%

if %myquotedstring:~0,1%u==^" AND %myquotedstring:~-1%==^" (
echo this string is quoted
)

这是我得到的输出
mystring: Non quoted string
myquotedstring: "My quoted string"
first character of non quoted string is: N
last character of non quoted string is: g
this string is NOT quoted
newstring: "Non quoted string"
first character of quoted string is: "
last character of quoted string is: "
AND was unexpected at this time.

更新

我现在意识到我不能使用 AND。但即使我删除我也有问题。

例如
if %mystring:~0,1%u==^" if %myquotedstring:~-1%==^" (
echo this string is NOT quoted
set newstring=^"Non quoted string^"
echo newstring: %newstring%
)

我得到
The syntax of the command is incorrect.

最佳答案

@echo off
setlocal EnableDelayedExpansion

set mystring=Non quoted string
echo %mystring%
if !mystring:~0^,1!!mystring:~-1! equ "" (
echo -^> String is quoted
) else (
echo -^> String not quoted
set newstring="%mystring%"
echo New string: !newstring!
)
echo/

set mystring="My quoted string"
echo %mystring%
if !mystring:~0^,1!!mystring:~-1! equ "" (
echo -^> String is quoted
) else (
echo -^> String not quoted
set newstring="%mystring%"
echo New string: !newstring!
)

关于batch-file - 检查字符串是否被引用并向字符串添加引号的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31358869/

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