Solved

Remove characters from a string in vbs

Posted on 2006-10-27
13
4,109 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now