简体   繁体   English

无法修剪 PowerShell 中的字符串

[英]Unable to trim string in PowerShell

I am tryinf to trim a string in powershell to get a desirer value but unable to do that.我正在尝试修剪 powershell 中的字符串以获得期望值但无法做到这一点。

ex string1= abc_xyz-mnq_R81 string2= abc_xyz-mnq_R82 ex string1= abc_xyz-mnq_R81 string2= abc_xyz-mnq_R82

I tried like- string1.trim("abc_xyz-mnq_R") Output: 81我试过了 - string1.trim("abc_xyz-mnq_R") Output: 81

string2.trim("abc_xyz-mnq_R") Output:8 string2.trim("abc_xyz-mnq_R") Output:8

I got output as 81 but when I tried for string2 I got only 8.我得到 output 为 81,但是当我尝试使用 string2 时,我只得到了 8。

I need to have a generic way so whatever may be before anystring_R it only trim out that number.我需要有一个通用的方法,所以无论在 anystring_R 之前可能是什么,它只会删除那个数字。

try -replace to delete substrings by regex patterns尝试-replace通过正则表达式模式删除子字符串

PS> $string1="abc_xyz-mnq_R81"
PS> $string2="abc_xyz-mnq_R82"
PS> $string1 -replace "^.*_R"
81
PS> $string2 -replace "^.*_R"
82

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM