Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read special characters 2

Status
Not open for further replies.

Tmucklow

Programmer
Jun 11, 2015
2
US
I have a string with speial characters such as '–' in it. I'm trying to get Powershell to replace them if found but not having any luck.

If ($mystring.contains'–') {} returns false so its not seeing the funky characters inside the string.

Any ideas on how I can get Powershell to see special characters like this in a string?
 
This is what I've done:
1. Create an array of all the special character you would like to identify with the double characters, such as the one you're trying to identify, at the beginning.
2. Search the specified string for any of the special characters
3. Replace the special character(s) with the appropriate substitute character(s)
- This is done with another array similar to the one in step 1​


Short sample, but hopefully it will get you going in the correct direction.

Code:
$test_string = "archâ€ology"

$sc_array = @()
$sc_array += "â€"

$sc_length = $sc_array.Length
$convert_array = New-Object 'object[,]' $sc_length,2
$convert_array[0,0] = "â€"
$convert_array[0,1] = "ae"

## Looking for special characters
$found = $FALSE
for ($sc = 0; $sc -lt $sc_length; $sc++)
	{
	 $search_char = $sc_array[$sc]
	 $found = $test_string.Contains($search_char)
	 if ($found)
		{$sc = $sc_length + 1}
	}

## Replacing special characters if they are found
if ($found)
	{
	 for ($i=0; $i -lt $sc_length; $i++)
		{
		 $search_char = $convert_array[$i,0]
		 $replace_char = $convert_array[$i,1]
		 $mod_test_string = $test_string -replace $search_char,$replace_char
		}
	}
	
$test_string
$mod_test_string


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
You could do the search and replace in one step. Mine are separate functions, because there are some times when I just want to be notified that there are special characters present and then prompt for what action to take; replace or leave.

Anyway, here is how to do it on one step:

Code:
$test_string = "archâ€ology"

$sc_length = 1 [COLOR=#EF2929][b]## <<<<------ This needs to be manually set to establish the array size[/b][/color] 

$convert_array = New-Object 'object[,]' $sc_length,2
$convert_array[0,0] = "â€"
$convert_array[0,1] = "ae"

## Looking for special characters
for ($sc = 0; $sc -lt $sc_length; $sc++)
	{
	 $search_char = $convert_array[$sc,0]
	 $found = $test_string.Contains($search_char)
	 ## Replacing special characters if they are found
	 if ($found)
		{
		 $search_char = $convert_array[$sc,0]
		 $replace_char = $convert_array[$sc,1]
		 $mod_test_string = $test_string -replace $search_char,$replace_char
		}
	}

$test_string
$mod_test_string


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
you may wish to look at https://lazywinadmin.com/2015/05/powershell-remove-diacritics-accents.html.
This will remove some of those characters you consider invalid (not that they are invalid!!!)

and take in consideration that when loading your file you may wish to load with the correct encoding as that particular combination is in some cases a different char altogether.
Just looks wrong if loaded incorrectly. Not always the case, and it may be that you are getting it "bad" already.

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Thanks guys this worked. Blister yours worked but only if I know the special chars ahead of time, I'm trying to prevent all special chars from getting through. Frederic the 2nd method you linked seems to work just fine, short and sweet. Appreciate the help!

Thanks,
Travis Mucklow
 
it works for some - but if you do have the euro sign (€) on its own as per the example posted here it will not replace it. (or at least it does not for me)

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top