Solved

Modify text using vbscript

Posted on 2013-05-13
7
251 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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

10 Experts available now in Live!

Get 1:1 Help Now