1

我一直在编写一个需要在文件中扩展环境字符串的应用程序。

为此,我可以使用标准的 Windows API 函数 ExpandEnvironmentStrings:http: //msdn.microsoft.com/en-us/library/ms724265 (VS.85).aspx

不过,我确实对该功能有一些问题。第一的: The size of the lpSrc and lpDst buffers is limited to 32K.

下一个:Note that this function does not support all the features that Cmd.exe supports. For example, it does not support %variableName:str1=str2% or %variableName:~offset,length%.

我想实现 cmd.exe 允许的这些额外功能,但我不确定它们到底是什么。:~offset,length 有点明显......子字符串。但不确定第一个是什么。

有任何想法吗?

比利3

4

1 回答 1

5

这是字符串替换。

基本上,如果 variableName 设置为"I am three",则"%variableName:three=four%"生成"I am four"(放入双引号以获得更好的格式,它们不构成字符串的一部分)。

C:\Documents and Settings\Administrator>set x=I am three

C:\Documents and Settings\Administrator>echo %x%
I am three

C:\Documents and Settings\Administrator>echo %x:three=four%
I am four

您还可以用空字符串替换(很明显)并从字符串的开头替换(不太明显):

C:\Documents and Settings\Administrator>echo %x:three=%
I am 

C:\Documents and Settings\Administrator>echo %x:*am=I am not%
I am not three

此外,子字符串变体是 Pythonesque,因为负数从字符串末尾开始工作:

C:\Documents and Settings\Administrator>echo %x:~,4%
I am

C:\Documents and Settings\Administrator>echo %x:~-5%
three
于 2009-04-28T01:36:14.493 回答