Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut String Into New Strings 2

Status
Not open for further replies.

MagnusFox

IS-IT--Management
Jan 12, 2001
51
US
Good day,

I have a string that I need to cut into smaller new strings. How is this done?

Given:
objGivenString = "QWERTYUIOPQW"

I want:
objFirstString = "QWER"
objNextString = "TYUI"
objLastString = "OPQW"

As you can see my given string is 12 chars long and my new strings are each 4 chars long. How do I loop through the given string 3 times to find the 4 chars in each part?

Thanks,
Magnus
 
dim str
str = "ABCDEFGHIJKL"
str1 = left(str, 4)
str2 = mid(str, 5, 4)
str3 = right(str, 4)

..or..

dim str
dim n
dim strs(3)
str = "ABCDEFGHIJKL"
for n = 0 to 2
strs(n) = mid(str, (n*4), 4)
next .
.. Eat, think and be merry .
... ....................... .
 
u can use functions in vb script to do it
left,mid and right
objGivenString = "QWERTYUIOPQW"
objFirstString =Left(objGivenString, 4)
objNextString = Mid(objGivenString, 5 , 4)
objLastString =Right(objGivenString, 4)

hope this solves the problem.
sunil
 
Swany,

I like your second choice best with the loop. However, Mid starts at position 1, not 0, so we need to change your math just slightly to make it work:

dim str
dim n
dim strs(3)
str = "ABCDEFGHIJKL"
for n = 0 to 2
strs(n) = mid(str, ((n*4)+1), 4)
next

Now all is well! Thanks much!
 
and to make it fully dynamic:

dim str
dim n
dim count
dim strs(3)
str = request("inputString")
' the following counts the sets of 4 letters in string
count = (Len(str)/4) + (Len(str) MOD 4)

for n = 0 to count-1
strs(n) = mid(str, ((n*4)+1), 4)
next


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top