String manipulation with PowerShell

The small examples cited in this article come from the following links:
 
This is a very good digest article that discusses some string manipulation using the expr command and Bash extensions.
Here I will make extensive use of the various methods of the String class in the System namespace of the .Net Framework 2.0 class library.
 
1. Let's see an example of finding the length of a string in Shell:
 

%x="abcd"
#方法一
%expr length $x
4
# 方法二
%echo ${#x}
4
# 方法三
%expr "$x" : ".*"
4

Do we need so many ways?? I think one is enough. In PowerShell, the string length can be returned by calling the string length property, as follows:

PS C:\> "abcd"
abcd
PS C:\> "abcd".length
4

Does PowerShell look more clear?

2. We next look at substring search in the shell:

%expr index $x "a"
1
%expr index $x "bc"
2
%expr index $x "cd"
3

The index of the array returned by expr is counted from 1. Let's see how PowerShell copes with it?

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

OK, PowerShell's array subscripts are consistent with most programming languages, and array subscripts are calculated from 0.

3. Get the substring in the shell:

# 方法一
# expr 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

Let 's take a look at PowerShell's Substring method

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:\>

PowerShell doesn't allow out-of-bounds situations during operations on strings. So one of the examples gives an error message. But then I use the trap keyword, which will catch the exception. So, this operation is nothing. output.

4. Matching regular expressions in the shell:

# 打印匹配长度
%expr match $x "."
1
%expr match $x "abc"
3
%expr match $x "bc"
0

As you can see here, expr supports regular expressions, and the length of the match is given after matching. Let's take a look at PowerShell's built-in regular expression operators:

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

这里三个语句都发出了自己的执行结果, -match操作符返回bool值, 提示是否匹配成功. 如果匹配成功可以通过访问$matches这个hashtable获得返回情况. 默认整个正则表达式匹配的信息被放在key = 0的位置. 如果匹配不成功, $matches的信息是不会被更新的. 这些行为和Perl几乎一样. 熟悉Perl的朋友是不是觉得这个地方很自然??相比使用expr, 虽然这里复杂了一些, 但是功能确强大了很多.

5. 在Shell中对字符串的掐头去尾

%x=aabbaarealwwvvww
%echo "${x%w*w}"
aabbaarealwwvv
%echo "${x%%w*w}"
aabbaareal
%echo "${x##a*a}"
lwwvvww
%echo "${x#a*a}"
bbaarealwwvvww

哎呀, 这个奇怪的表达式, 看起来让人费解呢...>_<看看-replace运算符是如何工作的吧^^

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:\>

你会用正则表达式嘛??如果你会, 那么上面的操作, 用正则表达式表示不是很简单嘛?

6. 在Shell中字符串的替换

这里不允许使用regex呢..不过可以用*,?...

%x=abcdabcd
%echo ${x/a/b} # 只替换一个
bbcdabcd
%echo ${x//a/b} # 替换所有
bbcdbbcd

第一个例子用PowerShell好像不容易呢...因为-replace是整行匹配的. 这点上不如Perl的s:::功能强大. 我先给出第二个的办法...^^也许以后会想到好办法呢

PS C:\> $x='abcdabcd'
PS C:\> $x -replace 'a','b'
bbcdbbcd

写到这里, 我想大家也能感受到PowerShell的一些特性, 正如我的理解, 所有.Net程序员, 天生对PowerShell中大部分功能是非常熟悉的. 可以直接调用.Net上的方法, 为PowerShell扩展了强大的功能.

我再给几个比较有意思的功能:

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

Trim是个常见的功能, 还有TrimStart, TrimEnd. ToUpper, ToLower更是很方便的功能.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570428&siteId=291194637