6 Replies Latest reply on Dec 8, 2008 11:35 PM by <newbie />

    Splitting a text string

    tomtomtom Level 1
      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
        • 1. Re: Splitting a text string
          Level 7
          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>
          • 2. Re: Splitting a text string
            Level 7
            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=" ">
            • 3. Re: Splitting a text string
              tomtomtom Level 1
              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....
              • 4. Re: Splitting a text string
                Level 7
                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/
                • 5. Re: Splitting a text string
                  tomtomtom Level 1
                  Thank you Ian and Azadi

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

                  Thanks again & regards
                  • 6. Re: Splitting a text string
                    <newbie /> Level 1

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