Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Splitting a text string

Participant ,
Nov 17, 2008 Nov 17, 2008
Hello all

I'm trying for some time to split a text string into several parts without cutting words apart. Some time ago I found a great UDF but it doesn't exactly do what I need (see code). It only displays the amount of words I set in 'words'.

Let's assume that I've got a text string with 1203 chars. I would love to set a variable charcount = 400. At the very end I would like to have four variables containing each (about) 400 chars and the last one containing the last 3 chars (approx). The function should not cut words apart (that's why I used brackets before). And the diamond would be that the function would tell me how many variables (containing each about 400 chars) it built.

Did somebody understand what I mean?

Many thanks for inputs
TOPICS
Advanced techniques
687
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Nov 17, 2008 Nov 17, 2008
Thank you Ian and Azadi

Meanwhile I was able to put together what I need...

Thanks again & regards
Translate
LEGEND ,
Nov 17, 2008 Nov 17, 2008
Questions you are going to have to answer for your self.

What constitutes a word? Spaces, Dashes, Punctuation?

What constitutes 'about' 400 chars?
As close as possible but under?
First word that goes over?
The closest either over or under?

But you should be able to get somewhere if you treat your string as a
list with spaces and|or other characters as delimiters. Real basic
starter code, completely untried and untested.

<cfset string = "The quick brown fox jumped over the lazy dog">

<cfset subString = "">
<cfloop list="#string#" index="word">
<cfset subString = subString + word>
<cfif len(subString) GT 10>
<cfoutput>#subString#<br/></cfoutput>
<cfset subString = "">
</cfif>
</cfloop>
<cfoutput>#subString#</cfoutput>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2008 Nov 17, 2008
Hello Ian

Happy to read from you again!

>What constitutes a word? Spaces, Dashes, Punctuation?
I suggets Spaces

>What constitutes 'about' 400 chars?
For what I need it everything is fine but let's say 'As close as possible but under'

<cfset string = "The quick brown fox jumped over the lazy dog">

<cfset subString = "">
<cfloop list="#string#" index="word" delimiters=" ">
<cfset subString = subString + word>
<cfif len(subString) GT 10>
<cfoutput>#subString#<br/></cfoutput>
<cfset subString = "">
</cfif>
</cfloop>
<cfoutput>#subString#</cfoutput>

I tested this code and it would throw the following error:

The value '' cannot be converted to a number.

The error occurred in ...: line 5
3 : <cfset subString = "">
4 : <cfloop list="#string#" index="word" delimiters=" ">
5 : <cfset subString = subString + word>
6 : <cfif len(subString) GT 10>

And it seems that I got a knot in my brain - I can't resolve this....
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2008 Nov 17, 2008
what Ian meant is, of course, <cfset subString = subString & word>
(or, on CF8, <cfset subString &= word>)

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2008 Nov 17, 2008
Thank you Ian and Azadi

Meanwhile I was able to put together what I need...

Thanks again & regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2008 Nov 17, 2008
Ian Skinner wrote:
> <cfset string = "The quick brown fox jumped over the lazy dog">
>
> <cfset subString = "">
> <cfloop list="#string#" index="word">
> <cfset subString = subString + word>
> <cfif len(subString) GT 10>
> <cfoutput>#subString#<br/></cfoutput>
> <cfset subString = "">
> </cfif>
> </cfloop>
> <cfoutput>#subString#</cfoutput>

Oops forgot the delimiter parameter for the <cfloop...> tag.
<cfloop list="#string#" index="word" delimiter=" ">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 08, 2008 Dec 08, 2008
LATEST

Here's another similar approach using a UDF...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources