Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2010, 03:08 AM
Authorized User
 
Join Date: Dec 2007
Posts: 59
Thanks: 9
Thanked 0 Times in 0 Posts
Post How can I remove hyphens from string?

Hello everybody


I need to remove empty spaces and hyphens from strings (or any non alphanumeric characters).Can you please help and point me to somewhere i can find help?

ex: nha----trang --live
turn into
nha-trang-live



Thanks,


Phuc Hoang
 
Old February 2nd, 2010, 04:27 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

You can create function something like:
Code:
public string RemoveSpecialChars(string str) 
    { 
 //you can add more chars as well !!
 string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'" }; 
 for (int i = 0; i < chars.Length; i++) 
 { 
     if (str.Contains(chars[i])) 
     { 
  str = str.Replace(chars[i], ""); 
     } 
 } 
 return str; 
  }
This can be called as:
Code:
  string originalS = "T!t^t%!s#f&'#;$%m|i%t(te)  [o:h]i)t@*t}est";
  string newString = RemoveSpecialChars(originalS);
or use regular experssion to do the same.
Code:
  string s = Regex.Replace(originalS, "[^\\w\\.@-]", "");
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
The Following User Says Thank You to om_prakash For This Useful Post:
phuc2583 (February 2nd, 2010)
 
Old February 2nd, 2010, 05:55 AM
Authorized User
 
Join Date: Dec 2007
Posts: 59
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for replying me to do that
and i have convert your code into vb.net like this:

Public Shared Function RemoveSpecialChars(ByVal str As String) As String
'you can add more chars as well !!
Dim chars() As String = New String() {",", ".", "/", "!", "", "#", "$", "%", "^", "&", "*", "'"}
Dim i As Integer
For i = 0 To chars.Length - 1 Step i + 1
If str.Contains(chars(i)) Then
str = str.Replace(chars(i), "")
End If
Next
Return str
End Function


i use this function and i have met an error as " String cannot be of zero length.
Parameter name: oldValue


"
Thanks all
 
Old February 3rd, 2010, 12:52 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Please check the following code:
Code:
 
Dim originalS AsString = "T,.E/s! #T^"
Dim newString AsString = RemoveSpecialChars(originalS)
 
PublicSharedFunction RemoveSpecialChars(ByVal str AsString) AsString
'you can add more chars as well !!
Dim chars() AsString = NewString() {",", ".", "/", "!", " ", "#", "$", "%", "^", "&", "*", "'"}
Dim i AsInteger
For i = 0 To chars.Length - 1 Step i + 1
If str.Contains(chars(i)) Then
str = str.Replace(chars(i), "")
EndIf
Next
Return str
EndFunction
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
The Following User Says Thank You to om_prakash For This Useful Post:
phuc2583 (February 4th, 2010)
 
Old February 16th, 2010, 04:02 AM
Registered User
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

'String Manipulation - How to Remove two or more hyphens from a string
Sub main()
Dim sText As String
sText = "This test-string. This is -- the test-string. " & _
"This is --------- the test-string. This is - the ------ " & _
"test - string. This is the test--string."
Debug.Print sText
Do While InStr(sText, "--") > 0
sText = Replace(sText, "---", "--")
sText = Replace(sText, "--", "")
sText = Replace(sText, " ", " ")
Loop
Debug.Print sText
End Sub

'Or

Sub main()
'You Need to set reference to Microsoft VBScript Regular Expressions 5.5
Dim sText As String
Dim re As RegExp, m As MatchCollection
Set re = New RegExp

sText = "This test-string. This is -- the test-string. " & _
"This is --------- the test-string. This is - the ------ " & _
"test - string. This is the test--string."
Debug.Print sText

re.Global = True
re.Pattern = "[-]{2,}"
sText = re.Replace(sText, "")
re.Pattern = "[ ]{2,}"
sText = re.Replace(sText, " ")
Debug.Print sText
End Sub

May this will help you...........





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove non-number parts of string fixit XSLT 4 October 15th, 2009 12:59 PM
remove commas and space in a string kumiko Classic ASP Basics 1 January 10th, 2008 11:42 PM
Remove the unwanted space at end of the string gmbalaa General .NET 2 August 21st, 2007 11:35 PM
Stripping hyphens arholly Access 2 January 4th, 2007 09:16 AM
Replace or remove the last char from the string sabu21s XSLT 12 January 8th, 2004 05:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.