Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate text in string after/before separator in PowerShell

Tags:

I want to make a PowerShell script that takes each file of my music library and then does a hash sum of it and writes that into a file like so:

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

When I start the script, I need it to first compare my music library with the already existing values, but for this I just want to cut off everything after the ; so that it can compare filename against filename (or filepath)... but I'm stumped at how to do that.

I tried replacing the value via $name = $name -replace ";*","", but that didn't work. I also tried to filter... but I don't know how.

like image 507
DemonWareXT Avatar asked Mar 05 '11 12:03

DemonWareXT


People also ask

How do I trim text in PowerShell?

One of the most common ways to trim strings in PowerShell is by using the trim() method. Like all of the other trimming methods in PowerShell, the trim() method is a member of the System. String . NET class.

How do I split a string using delimiter in PowerShell?

The .Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks.

What is delimiter in PowerShell?

Introduction to PowerShell Split String. PowerShell uses the Split () function to split a string into multiple substrings. The function uses the specified delimiters to split the string into sub strings. The default character used to split the string is the whitespace.

How do I remove the last character of a string in PowerShell?

Result is “DCCOMP01″. This works especially well when the last character is a special PowerShell reserved one like “$”.


1 Answers

$pos = $name.IndexOf(";") $leftPart = $name.Substring(0, $pos) $rightPart = $name.Substring($pos+1) 

Internally, PowerShell uses the String class.

like image 72
VVS Avatar answered Sep 18 '22 12:09

VVS