Solved

Remove characters from a string in vbs

Posted on 2006-10-27
13
4,117 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

756 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