Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 从可变字符串长度中删除(减除)最后n个字符_String_Powershell_Split - Fatal编程技术网

String 从可变字符串长度中删除(减除)最后n个字符

String 从可变字符串长度中删除(减除)最后n个字符,string,powershell,split,String,Powershell,Split,我需要删除(或替换为零)powershell代码中字符串的最后n个字符。该变量可能与减法字符串一起构成字符串(未找到我的答案)。 我有这样的东西(字符串): 某物/某物//最后某物/名称 其中NAME是一个变量文本,我可以预先提取并操作($NAME或$NAME.length)。整个字符串都可以计数-$string.length。 如何从字符串($string)中减去此名称?我搜索了很多方法,包括trim、replace、substring,但所有这些方法都主要用于静态单词或正则表达式,或者字符串

我需要删除(或替换为零)powershell代码中字符串的最后n个字符。该变量可能与减法字符串一起构成字符串(未找到我的答案)。 我有这样的东西(字符串):

某物/某物//最后某物/名称

其中NAME是一个变量文本,我可以预先提取并操作($NAME或$NAME.length)。整个字符串都可以计数-$string.length。 如何从字符串($string)中减去此名称?我搜索了很多方法,包括trim、replace、substring,但所有这些方法都主要用于静态单词或正则表达式,或者字符串的开头

我需要得到这个:

某物/某物/某物

我甚至尝试过这样的构造:

$string.split('($NAME)')[0]


还有一些使用get AD*函数和join绕过字符串,但没有任何效果。

您可以将其替换为
'
(无…空字符串),并且因为
-replace
与正则表达式一起工作,所以您可以确保只在字符串末尾获得“匹配”,如下所示:

$var = '/NAME'
'something/Name/something/../lastsomething/NAME' -replace "$var$",''

您可以将其替换为
'
(无…空字符串),并且由于
-replace
与正则表达式一起工作,因此您可以确保仅在字符串末尾获得“匹配”,如下所示:

$var = '/NAME'
'something/Name/something/../lastsomething/NAME' -replace "$var$",''

一个简单的解决方案是将子字符串从开头(0)取到最后出现的
/

$t = 'something/something/../lastsomething/NAME'
$t.Substring(0, $t.LastIndexOf('/'))

一个简单的解决方案是将子字符串从开头(0)取到最后出现的
/

$t = 'something/something/../lastsomething/NAME'
$t.Substring(0, $t.LastIndexOf('/'))

编辑您的评论真正的问题是如何获得

 -replace '($_.Name)',' ' 
工作。单引号不展开变量-因此使用双引号。 要强制对
$进行求值,必须使用
$()


具有未知的最后一个元素
/Name

> $String = 'something/something/../lastsomething/NAME'

> $String.Split('/')[-1]
NAME

> $string = $string -replace "/$($String.Split('/')[-1])"

> $string
something/something/../lastsomething
更简单的解决方案是:

> Split-Path $string
something\something\..\lastsomething

> Split-Path $string -Leaf
NAME

但它将斜杠改为反斜杠

编辑您的评论真正的问题是如何获得

 -replace '($_.Name)',' ' 
工作。单引号不展开变量-因此使用双引号。 要强制对
$进行求值,必须使用
$()


具有未知的最后一个元素
/Name

> $String = 'something/something/../lastsomething/NAME'

> $String.Split('/')[-1]
NAME

> $string = $string -replace "/$($String.Split('/')[-1])"

> $string
something/something/../lastsomething
更简单的解决方案是:

> Split-Path $string
something\something\..\lastsomething

> Split-Path $string -Leaf
NAME

但是它将斜杠改为反斜杠

,所以为什么不将其替换为
?”。。。像这样:
'something/something/./lastsomething/NAME'-replace'NAME','
因为我有一个作为变量的名称-它实际上是Get AD*函数中的“$\.NAME”,它不能正确地替换为“-replace'($\.NAME)”,”。对于这种情况,我必须添加另一个字符串转换。请尝试
-replace”/$($.Name)
单引号不展开变量,要计算
$.Name
必须将其括在
$()
中。是的,这很有效!非常感谢。但是您需要准确地使用
(双引号)而不是
(单引号)。最后应该是
-replace”/$($.Name)”,“”
,因为第一部分中的单引号不起任何作用,只有双引号起作用。使用
-replace
操作符,您不需要指定空的替换表达式。(与需要它的
.replace()
方法不同)那么为什么不将其替换为
'
。。。像这样:
'something/something/./lastsomething/NAME'-replace'NAME','
因为我有一个作为变量的名称-它实际上是Get AD*函数中的“$\.NAME”,它不能正确地替换为“-replace'($\.NAME)”,”。对于这种情况,我必须添加另一个字符串转换。请尝试
-replace”/$($.Name)
单引号不展开变量,要计算
$.Name
必须将其括在
$()
中。是的,这很有效!非常感谢。但是您需要准确地使用
(双引号)而不是
(单引号)。最后应该是
-replace”/$($.Name)”,“”
,因为第一部分中的单引号不起任何作用,只有双引号起作用。使用
-replace
操作符,您不需要指定空的替换表达式。(与需要它的
.replace()
方法不同)此方法也适用。非常感谢。但它的代码比Remko的建议要长一点。这个也行。非常感谢。但它的代码比Remko的建议要长一点。