Solved

Remove characters from a string in vbs

Posted on 2006-10-27
13
4,122 Views
Last Modified: 2011-08-18
I need to remove cn= from output generated by a vbs script.

cn=server1   needs to be just server1.

I'm flayling trying to use trim. Is there a way to strip off cn= for my results using a function?

Thanks!
0
Comment
Question by:Ponthecomputer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17817603
hi if you only have cn= and it is constant you could have

stringVal = right(stringVal, len(stringVal) - 3)

which removes the leftmost three characters from the string called stringVal

0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17817615
Otherwise you would be able to use a regular expression - but if the string you want to remove is at the start of the string and is always the same this is the fastest and easiset way.
0
 
LVL 3

Accepted Solution

by:
taycuong76 earned 350 total points
ID: 17817682
all_str: the whole string

result=replace(all_str,"cn=","")
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 35

Expert Comment

by:Raynard7
ID: 17817686
the replace is good - but if you are not wanting to remove it from the middle of the string you will get problems.  additionally this only removes it one time. how you would do this depends on your setup and requirements.
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17817693
trim (ltrim,rtrim) only use to remove " " at left or right or both from a string
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17817730
so, You can use instr() function to select the 1st occurence of "cn=" and remove it.

0
 
LVL 1

Expert Comment

by:ben_staples
ID: 17817734
Function replacefirst(string as string, toreplace as string)
for i = 1 to len(string) - len(toreplace)
    if mid(string,1,len(toreplace)) = toreplace then
        replacefirst = left(string,i-1) & right(string, len(string) - (i - 1) - len(toreplace))
        exit for
    end if
next i

end function

should replace first instance on supplied string
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17817784
'This function find the 1st occurence of substring in a string and remove it.

'LongStr: String to delete from
'SubStr: Substring to remove.      

Public Function StrDel(LongStr As String, SubStr As String) As String
  Dim Dest As Long, Src As Long

  Dest = InStr(LongStr, SubStr)

  If Dest = 0 Then
    StrDel = ""
    Exit Function
  End If

  Src = Dest + Len(SubStr)
  Mid$(TempStr, Dest) = Mid$(TempStr, Src)
  LongStr = Left$(LongStr, Len(LongStr) - Len(SubStr))

  StrDel = LongStr

End Function
====
To test it in VB, put a command button on a form  and paste the following code:

Private Sub Command1_Click()
Me.Caption = StrDel("cn=sadhsajh cn=323232 cn=21212121", "cn=")
End Sub

Public Function StrDel(LongStr As String, SubStr As String) As String
  Dim Dest As Long, Src As Long

  Dest = InStr(LongStr, SubStr)

  If Dest = 0 Then
    StrDel = ""
    Exit Function
  End If

  Src = Dest + Len(SubStr)
  Mid$(TempStr, Dest) = Mid$(TempStr, Src)
  LongStr = Left$(LongStr, Len(LongStr) - Len(SubStr))

  StrDel = LongStr

End Function

' Hope this help
' Notice: This function I found on web, not mine.
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17817865
This function must work:

Public Function StrIDel(LongStr As String, SubStr As String) As String
  Dim Dest As Long, Src As Long

  Dest = InStr(UCase$(LongStr), UCase$(SubStr))

  If Dest = 0 Then
    StrIDel = ""
    Exit Function
  End If

  Src = Dest + Len(SubStr)
  Mid(LongStr, Dest) = Mid(LongrStr, Src)
  LongStr = Left$(LongStr, Len(LongStr) - Len(SubStr))

  StrIDel = LongrStr

End Function
0
 

Expert Comment

by:MrMalone
ID: 17817899
The Trim function only removes leading spaces and trailing spaces, so you're barking up the wrong tree there ;-)

Regular expressions are overkill for what you need imo... although if you do want to use them (it's probably worth you learning about them anyway!) see http://www.regular-expressions.info/reference.html

I'd just use the replace function in your case as tay suggested
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17817921
'Sorry, mistake in my previous post. I repost the right solution here:

'To test it in VB, put a command button on a form  and paste the following code:

Private Sub Command1_Click()
Me.Caption = StrDel("sajh cn=323232 cn=21212121", "cn=")
End Sub

Public Function StrDel(LongStr As String, SubStr As String) As String
  Dim Dest As Long, Src As Long

  Dest = InStr(UCase$(LongStr), UCase$(SubStr))

  If Dest = 0 Then
    StrDel = ""
    Exit Function
  End If

  Src = Dest + Len(SubStr)
  Mid(LongStr, Dest) = Mid(LongStr, Src)
  LongStr = Left$(LongStr, Len(LongStr) - Len(SubStr))

  StrDel = LongStr

End Function
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17818106
what about using split ?

dim txt,a
txt="cn=server1"
a=Split(txt,"=")
msgbox a(1)

Something like that where a(#) would be the index of which item you want returned and Im guessing server1 will always end up being number 1 ie a(1)
0
 
LVL 1

Author Comment

by:Ponthecomputer
ID: 17840443
Spiffy. Thanks! -Paresh
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question