Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove characters from a string in vbs

Posted on 2006-10-27
13
Medium Priority
?
4,129 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 1400 total points
ID: 17817682
all_str: the whole string

result=replace(all_str,"cn=","")
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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 about my first experience with programming Arduino.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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 …
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…

963 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