Powershell缩短字符串


我们先演示下常用的缩短字符串方法:

$text = "Some text"
$fromRight = 3

$text.Substring(0, $text.Length - $fromRight)

更强大的方法是使用 “-replace” 正则表达式

$text = "Some text"
$fromRight = 3

$text -replace ".{$fromRight}$"

这里的”.”代表任意字符串,后面的“fromRight$”表示裁剪到结尾的长度。

正则表示式是非常强大的,你可以只砍掉尾部的数字部分,假设指定砍掉最多4位数字。

$text1 = "Some text with digits267686783"
$text2 = "Some text with digits3"

$text1 -replace "\d{0,5}$"
$text2 -replace "\d{0,5}$"

原文地址: Shortening Text

本文链接: https://www.pstips.net/shortening-text.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注