×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to read special characters
2

How to read special characters

How to read special characters

(OP)
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?

RE: How to read special characters

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.

RE: How to read special characters

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 ## <<<<------ This needs to be manually set to establish the array size 

$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.

RE: How to read special characters

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
www.syssoft-int.com

FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: How to read special characters

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close