Solved

Modify text using vbscript

Posted on 2013-05-13
7
263 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
[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
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 55

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
Technology Partners: 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 53

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 55

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 53

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

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!

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…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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