在powershell中的seperator之后/之前切断字符串中的文本

Dem*_*eXT 13 string powershell

好的,这就是我要做的事情:我想制作一个小的PowerShell脚本,它接收我的音乐库的每个文件,然后对它做一个哈希值并将其写入一个文件,如下所示:

test.txt; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198

现在,当我开始编写脚本时,我需要先将已经存在的值与我的音乐库进行比较,但为此我只想在切断之后切断所有内容.所以它可以将文件名与文件名(或文件路径)进行比较......但是我很难过如何做到这一点,尝试用$ name = $ name -replace替换";*",""没有...

试图过滤...不知道D:

我真的很感激帮助.

如果你认为我使用了错误的编码语言,告诉我什么会更好,它只是我只使用过C和powershell

VVS*_*VVS 42

$pos = $name.IndexOf(";")
$leftPart = $name.Substring(0, $pos)
$rightPart = $name.Substring($pos+1)
Run Code Online (Sandbox Code Playgroud)

在内部,PowerShell使用String类.


hdo*_*men 9

您可以使用Split

$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"
$separator = ";" # you can put many separator like this "; : ,"

$parts = $text.split($separator)

echo $parts[0] # return test.txt
echo $parts[1] # return the part after the separator
Run Code Online (Sandbox Code Playgroud)


Sar*_*ang 9

$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"

$text.split(';')[1].split(' ')
Run Code Online (Sandbox Code Playgroud)