Solved

Remove characters from a string in vbs

Posted on 2006-10-27
13
4,113 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

832 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