Solved

Modify text using vbscript

Posted on 2013-05-13
7
256 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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

12 Experts available now in Live!

Get 1:1 Help Now