need to break up a string by character count

J

Joe Reynolds

say i have a string that is "hello this is my string and i like it very
much" and i want to insert a newline at every 20th character, how could i go
about doing this?
 
B

Bob Barrows [MVP]

Joe said:
say i have a string that is "hello this is my string and i like it
very much" and i want to insert a newline at every 20th character,
how could i go about doing this?

I'm sure someone will post a regex solution for this, but until then:

dim buffer, i, newstring,s
s="hello this is my string and i like it very much"
buffer=20
for i = 1 to len(s) step 20
newstring=newstring & mid(s,i,20) & vbcrlf
next
msgbox newstring
 
J

Joe Reynolds

that just might work

:)


Bob Barrows said:
I'm sure someone will post a regex solution for this, but until then:

dim buffer, i, newstring,s
s="hello this is my string and i like it very much"
buffer=20
for i = 1 to len(s) step 20
newstring=newstring & mid(s,i,20) & vbcrlf
next
msgbox newstring
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
 
D

Dave Anderson

Bob said:
dim buffer, i, newstring,s
s="hello this is my string and i like it very much"
buffer=20
for i = 1 to len(s) step 20
newstring=newstring & mid(s,i,20) & vbcrlf
next
msgbox newstring

Won't that put an extra line break at the end, Bob?
 
G

Guest

that doesnt hurt me

:)

Dave Anderson said:
Won't that put an extra line break at the end, Bob?



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use of this email address implies consent to these terms.
 
B

Bob Barrows [MVP]

Dave said:
Won't that put an extra line break at the end, Bob?

Ummmm ... errr. .. I left that as an exercise for the reader ... yeah,
that's the ticket!
 
A

Anthony Jones

Bob Barrows said:
I'm sure someone will post a regex solution for this, but until then:

Here's then:-

s = InsertStringAtInterval(s, vbCrLf, 20))

Function InsertStringAtInterval(rsSource, rsInsert, rlInterval)

Dim rgx
Set rgx = new RegExp
rgx.Pattern = "([\s\S]{" & rlInterval & "})"
rgx.Global = true

InsertStringAtInterval = rgx.Replace(rsSource, "$1" & rsInsert)

End Function


BTW Joe, I still think you should persue a different solution than this.
If you use this in the TD you will actually need:-

s = InsertStringAtInterval(Server.HTMLEncode(s), "<br />", 20))
 
J

Joe Reynolds

thank you. that works great, but i had another thought (posted a new
question also)
what i want to do is first check the users input. if i find any strings of
characters that are longer than 40 characters that dont contain a space THEN
i would pass it to your function. as long as there is at least 1 space for
every 40 chars, im ok



Anthony Jones said:
Bob Barrows said:
I'm sure someone will post a regex solution for this, but until then:

Here's then:-

s = InsertStringAtInterval(s, vbCrLf, 20))

Function InsertStringAtInterval(rsSource, rsInsert, rlInterval)

Dim rgx
Set rgx = new RegExp
rgx.Pattern = "([\s\S]{" & rlInterval & "})"
rgx.Global = true

InsertStringAtInterval = rgx.Replace(rsSource, "$1" & rsInsert)

End Function


BTW Joe, I still think you should persue a different solution than this.
If you use this in the TD you will actually need:-

s = InsertStringAtInterval(Server.HTMLEncode(s), "<br />", 20))


dim buffer, i, newstring,s
s="hello this is my string and i like it very much"
buffer=20
for i = 1 to len(s) step 20
newstring=newstring & mid(s,i,20) & vbcrlf
next
msgbox newstring
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
 
B

Bob Lehmann

every 40 chars, im ok

How did you determine that 40 chars is OK?

Is that when I have my font size set to any of smallest, smaller, medium,
larger or largest in IE, or use anthing but normal in FireFox?

Bob Lehmann


Joe Reynolds said:
thank you. that works great, but i had another thought (posted a new
question also)
what i want to do is first check the users input. if i find any strings of
characters that are longer than 40 characters that dont contain a space THEN
i would pass it to your function. as long as there is at least 1 space for
every 40 chars, im ok



Anthony Jones said:
Bob Barrows said:
Joe Reynolds wrote:
say i have a string that is "hello this is my string and i like it
very much" and i want to insert a newline at every 20th character,
how could i go about doing this?

I'm sure someone will post a regex solution for this, but until then:

Here's then:-

s = InsertStringAtInterval(s, vbCrLf, 20))

Function InsertStringAtInterval(rsSource, rsInsert, rlInterval)

Dim rgx
Set rgx = new RegExp
rgx.Pattern = "([\s\S]{" & rlInterval & "})"
rgx.Global = true

InsertStringAtInterval = rgx.Replace(rsSource, "$1" & rsInsert)

End Function


BTW Joe, I still think you should persue a different solution than this.
If you use this in the TD you will actually need:-

s = InsertStringAtInterval(Server.HTMLEncode(s), "<br />", 20))


dim buffer, i, newstring,s
s="hello this is my string and i like it very much"
buffer=20
for i = 1 to len(s) step 20
newstring=newstring & mid(s,i,20) & vbcrlf
next
msgbox newstring
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top