0

Been scratching my head on this one...

I'd like to remove .com and capitalize S and T from: "sometext.com"

So output would be Some Text

Thank you in advance

2 Answers2

1

For most of this you can use the replace() member of the String object. The syntax is:

$string = $string.replace('what you want replaced', 'what you will replace it with')

Replace can be used to erase things by using blank quotes '' for the second argument. That's how you can get rid of .com

$string = $string.replace('.com','')

It can also be used to insert things. You can insert a space between some and text like this:

$string = $string.replace('et', 'e t')

Note that using replace does NOT change the original variable. The command below will print "that" to your screen, but the value of $string will still be "this"

$string = 'this'
$string.replace('this', 'that')

You have to set the variable to the new value with =

$string = "this"
$string = $string.replace("this", "that")

This command will change the value of $string to that.

The tricky part here comes in changing the first t to capital T without changing the last t. With strings, replace() replaces every instance of the text.

$string = "text"
$string = $string.replace('t', 'T')

This will set $string to TexT. To get around this, you can use Regex. Regex is a complex topic. Here just know that Regex objects look like strings, but their replace method works a little differently. You can add a number as a third argument to specify how many items to replace

$string = "aaaaaa"
[Regex]$reggie = 'a'
$string = $reggie.replace($string,'a',3)

This code sets $string to AAAaaa.

So here's the final code to change sometext.com to Some Text.

$string = 'sometext.com'
#Use replace() to remove text.
$string = $string.Replace('.com','')
#Use replace() to change text
$string = $string.Replace('s','S')
#Use replace() to insert text.
$string = $string.Replace('et', 'e t')
#Use a Regex object to replace the first instance of a string.
[regex]$pattern = 't'
$string = $pattern.Replace($string, 'T', 1)
Bagheera
  • 1,358
  • 4
  • 22
  • 35
1

What you're trying to achieve isn't well-defined, but here's a concise PowerShell Core solution:

PsCore> 'sometext.com' -replace '\.com$' -replace '^s|t(?!$)', { $_.Value.ToUpper() }

SomeText
  • -replace '\.com$' removes a literal trailing .com from your input string.

  • -replace '^s|t(?!$), { ... } matches an s char. at the start (^), and a t that is not (!) at the end ($); (?!...) is a so-called negative look-ahead assertion that looks ahead in the input string without including what it finds in the overall match.

  • Script block { $_.Value.ToUpper() } is called for each match, and converts the match to uppercase.

  • -replace (a.k.a -ireplace) is case-INsensitive by default; use -creplace for case-SENSITIVE replacements.

For more information about PowerShell's -replace operator see this answer.


Passing a script block ({ ... }) to dynamically determine the replacement string isn't supported in Windows PowerShell, so a Windows PowerShell solution requires direct use of the .NET [regex] class:

WinPs> [regex]::Replace('sometext.com' -replace '\.com$', '^s|t(?!$)', { param($m) $m.Value.ToUpper() })

SomeText
mklement0
  • 382,024
  • 64
  • 607
  • 775