Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

This is wat i have done...But i can see this is not an efficient way...

VB
For x = 126 To 255
      If strName.Contains(Chr(x)) Then
          strName = strName.Replace(Chr(x), " ")
          dr("Name") = strName

      Else
          dr("Name") = parts(0)
      End If
  Next


Can someone suggest me a better way.

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-13 12:41pm    
How come these characters are "special". How about some characters you did not take into account, for example: ¡¦§©«®°†‡‖‰‱※‼‽⁂? What, are they not so "special"? :-)
—SA
vidkaat 14-Mar-13 12:55pm    
Halo,

Chr(126) to chr(255) covers everything .....Here is the link http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp.....
Sergey Alexandrovich Kryukov 14-Mar-13 12:59pm    
Well, tell this to someone else, not to me. What do you think about the character in your example? Do you have an idea where they come from?
I'll explain. You are talking about ASCII encoding, but .NET supports Unicode, of course. ASCII is a small subset of it. Do you know how Unicode works.

Anyway, I provided you a comprehensive answer on performance problem. Please accept it formally (green button)...
—SA
Sarrrva 14-Mar-13 13:01pm    
hi try
Public Function IsSpecialChar(ByVal c As Char) As Boolean
' Need you to fill this out
End Function

Public Function RemoveSpecialChars(ByVal s As String) As String
Dim builder = New System.Text.StringBuilder()
Dim cur
For Each cur In s
If Not IsSpecialChar(cur) Then
builder.Append(cur)
End If
Next
Return builder.ToString()
End Function
Member 11383189 15-May-15 9:23am    
hi

Hi,

You can use a regular expression to replace special characters with whitespace. In this example, all characters which aren't a-z, A-Z or 0-9 will be replaced with whitespace:
VB
Dim input As String = "Hello^world" ' change this into your input
Dim replaced As String = System.Text.RegularExpressions.Regex.Replace(input, "[^a-zA-Z0-9]", " ")
Console.WriteLine(replaced) ' output: Hello world

More about regular expressions:
The 30 Minute Regex Tutorial[^]
Regex.Replace(String, String, String) method:
http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx[^]
 
Share this answer
 
The first improvement you could make is to get rid of that completely.
If the string does not contain the char value 255, then dr("name") will always contain parts(0) and (presumably) the revised strName value will be discarded - because the final value in the loop determines the value of the array element.
So I assume that is not what you are trying to do...

What are you trying to achieve?
The reason I ask is that unicode characters (which is what strings are made up of) are not necessarily just in the range 0..255 If you are after printable characters only then it's relatively simple - but I would probably use a Regex


"I dont want to declare the special characters. I shud use ch(x) where x=126 to 255 ...I want to basically check for the special characters in a string. If found replace them with a space else i want to store the string in my datarow dr."

What about the special characters below space? Don't you want to get rid of them too?
Can I suggest that you start with a list of *permitted* characters and use a regex to replace all the characters which aren't on it? It's a simpler solution:
VB
Public Dim regex As Regex = New Regex("[^\w\.\,!""$%^&*\(\)-_+=::@']")


' This is the replacement string
Public Dim regexReplace As String = " "


' Replace the matched text in the InputText using the replacement pattern
Dim result As String = regex.Replace(InputText,regexReplace)
 
Share this answer
 
v2
Comments
vidkaat 14-Mar-13 12:50pm    
I dont want to declare the special characters. I shud use ch(x) where x=126 to 255 ...I want to basically check for the special characters in a string. If found replace them with a space else i want to store the string in my datarow dr.
OriginalGriff 14-Mar-13 13:00pm    
Answer updated
There is lot of better way,

trivial code snippet,

import your namespace,
C#
Imports System.Text.RegularExpressions


write your own function,

C#
Public Shared Function RemoveSpecialCharacters(ByVal input As String) As String
       Dim r As New Regex("(?:[^a-z0-9 ]|(?<=['""])s)", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)
       Return r.Replace(input, [String].Empty)
   End Function


usage:
C#
MsgBox("Result :" & RemoveSpecialCharacters("C# language"))


regards
sarva
 
Share this answer
 
First of all, the purpose itself is quite questionable. Please see my comment to the question.

Indeed, your code is utterly ineffective. You don't see to understand that the type string is immutable. The method string.Replace does not actually modify any string as this is impossible. Instead, it creates a brand new string each time you call it, and initialize this new string with a modified copy of original string. You assignment simply abandons old reference to string data, and it is eventually garbage-collected. Enormously wasteful. And you make it even worse by iterating by character range (weird concept of "special") and repeating search every time. You would have to through out search and iterate by string's characters instead. But don't do it. You should never use even Replace in a cycle.

Instead, you should use mutable type System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

You need to start with empty instance of System.Text.StringBuilder and append some characters just ignoring some others. Note that System.Text.StringBuilder.ToStrings will give you the resulting string.

Through out all your code, try again.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900