Solved

Modify text using vbscript

Posted on 2013-05-13
7
260 Views
Last Modified: 2013-05-16
Hello,
I have a dilemma,

In VBS, I have a variable that reads a text input:

strObj = "Text/Text2/Text3"

I need to edit strObj so it will say only "Text/Text2" (basically discard everything after the 2nd "/" including)

Any advise?
0
Comment
Question by:johnnyjonathan
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 200 total points
ID: 39163240
Try this:
Option Explicit

Dim strObj, iPos

strObj = "Text/Text2/Text3"

iPos = InStr(strObj, "/")
If iPos > 0 Then
	iPos = InStr(iPos + 1, strObj, "/")
	If iPos > 0 Then
		strObj = Left(strObj, iPos - 1)
	End If
End If

MsgBox strObj

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 200 total points
ID: 39163259
Hi, I just wrote this little function, which should work for you.  It returns the position of the specified instance of a character.

Regards,

Rob,

strObj =   "Text/Text2/Text3"

intChr = FindChrPos(strObj, "/", 2)

WScript.Echo intChr
WScript.echo Left(strObj, intChr - 1)

Function FindChrPos(strTheString, strTheChr, intInstance)
	intStart = 1
	For intNum = 1 To intInstance
		intFound = InStr(intStart, strTheString, strTheChr, vbTextCompare)
		intStart = intFound + 1
		If intStart > Len(strTheString) Then Exit For
	Next
	If intNum < intInstance Then intFound = 0
	FindChrPos = intFound
End Function

Open in new window

0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 50 total points
ID: 39163344
Here's a fairly simple approach.

arrTemp = Split(strObj, "/")
strObj = arrTemp(0) & "/" & arrTemp(1)

Open in new window

~bp
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 50 total points
ID: 39163348
function cut(x)
   arr=split(x,"/")
   cut=arr(0)&"/"&arr(1)
end function


strObj = "Text/Text2/Text3"
response.write cut(strObj)

Open in new window

0
 

Author Closing Comment

by:johnnyjonathan
ID: 39172019
Thanks all! worked like a charm
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39172518
Not griping, you are certainly free to select any working solution as you see fit.  Just want to understand if I missed anything, what made http:#a39163344 such an inferior solution for you, seems pretty compact and effective?  Or was it just because it wasn't first on the scene?

~bp
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39172600
I agree. I did the same thing as @billprew - we posted just about the same time.  The only difference is I made a function so you can call the function anywhere in the code multiple times with one line.

There are always many ways to the same visual outcome though.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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