%x="abcd" #方法一 %expr length $x 4 # 方法二 %echo ${#x} 4 # 方法三 %expr "$x" : ".*" 4 |
PS C:\> "abcd" abcd PS C:\> "abcd".length 4 |
%expr index $x "a" 1 %expr index $x "bc" 2 %expr index $x "cd" 3 |
PS C:\> $x="abcd" PS C:\> $x.IndexOf('a') 0 PS C:\> $x.IndexOf('b') 1 PS C:\> $x.IndexOf('bc') 1 PS C:\> $x.IndexOf('cd') 2 PS C:\> $x.IndexOf('ef') -1 |
# 方法一 # expr <string> startpos length %expr substr "$x" 1 3 abc %expr substr "$x" 1 5 abcd %expr substr "$x" 2 5 bcd # 方法二 # ${x:pos:lenght} %echo ${x:1} bcd %echo ${x:2} cd %echo ${x:0} abcd %echo ${x:0:2} ab %pos=1 %len=2 %echo ${x:$pos:$len} bc |
PS C:\> $x abcd PS C:\> $x.Substring(0, 2) ab PS C:\> $x.Substring(0, 4) abcd PS C:\> $x.Substring(0, 6) Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string. Parameter name: length" At line:1 char:13 + $x.Substring( <<<< 0, 6) PS C:\> trap { $x.Substring(0, 6) } PS C:\> |
# 打印匹配长度 %expr match $x "." 1 %expr match $x "abc" 3 %expr match $x "bc" 0 |
PS C:\> $x -match '.'; $matches[0]; ($matches[0]).length True a 1 PS C:\> $x -match 'abc'; $matches[0]; ($matches[0]).length True abc 3 PS C:\> $x -match 'dc'; $matches[0]; ($matches[0]).length False abc 3 |
%x=aabbaarealwwvvww %echo "${x%w*w}" aabbaarealwwvv %echo "${x%%w*w}" aabbaareal %echo "${x##a*a}" lwwvvww %echo "${x#a*a}" bbaarealwwvvww |
PS C:\> $x = 'aabbaarealwwvvww' PS C:\> $x -replace 'w*$' aabbaarealwwvv PS C:\> $x -replace 'w.*w$' aabbaareal PS C:\> $x -replace '^a.*a' lwwvvww PS C:\> $x -replace '^a*' bbaarealwwvvww PS C:\> |
%x=abcdabcd %echo ${x/a/b} # 只替换一个 bbcdabcd %echo ${x//a/b} # 替换所有 bbcdbbcd |
PS C:\> $x='abcdabcd' PS C:\> $x -replace 'a','b' bbcdbbcd |
PS C:\> $x abcdabcd PS C:\> $x.ToUpper() ABCDABCD PS C:\> $x.ToUpper().ToLower() abcdabcd PS C:\> $x='abcdabcdab' PS C:\> $x.Trim('ab') cdabcd |
欢迎光临 批处理之家 (http://bathome.net./) | Powered by Discuz! 7.2 |