String Powershell TrimEnd()中的字符串操作问题

String Powershell TrimEnd()中的字符串操作问题,string,powershell,String,Powershell,我有一个用于重命名文件的PowerShell脚本。以下是字符串操作代码的一部分(不是我的实际代码,只是为了说明问题): 结果是: String1.txt String1 String1.txt20131104 String1_20131104.bak String.bak 为什么String1末尾的1也被删除了?我希望结果是String1.baktrimend()方法接受一个字符数组(而不是字符串)参数,并将修剪数组中出现在字符串末尾的所有字符 我通常使用-replace操作符修剪字符串值:

我有一个用于重命名文件的PowerShell脚本。以下是字符串操作代码的一部分(不是我的实际代码,只是为了说明问题):

结果是:

String1.txt
String1
String1.txt20131104
String1_20131104.bak
String.bak
为什么
String1
末尾的
1
也被删除了?我希望结果是
String1.bak

trimend()方法接受一个字符数组(而不是字符串)参数,并将修剪数组中出现在字符串末尾的所有字符

我通常使用-replace操作符修剪字符串值:

$text="String1.txt"
$text 
$text = $text -replace '\.txt$',''
$text

String1.txt
String1

根据上的答案,我正在使用这些正则表达式来删除未知类型的扩展,在文件名的其他地方可能有

$imagefile="hi.there.jpg"
$imagefile
hi.there.jpg
$imagefile -replace '\.([^.]*)$', ''
hi.there
$imagefile -replace '([^.]*)$', ''
hi.there.
$imagefile="hi.there.jpg"
$imagefile
hi.there.jpg
$imagefile -replace '\.([^.]*)$', ''
hi.there
$imagefile -replace '([^.]*)$', ''
hi.there.