1

In flex,

I have a below string:

Chocolate has become one of the most popular food types and flavors in the world, and a vast number of foodstuffs involving chocolate have been created. Chocolate chip cookies have become very common, and very popular, in most parts of Europe and North America.

Gifts of chocolate molded into different shapes have become traditional on certain holidays. Chocolate is also used in cold and hot beverages, to produce chocolate milk and hot chocolate.

Cocoa mass was used originally in Mesoamerica both as a beverage and as an ingredient in foods.

Above string contains three paragraphs. How could I count paragraph from above string ?

I have tried like this:

    for(var i:int = 0; i < bodyPhrases.length; i++)
     {
                var para:int = bodyPhrases.indexOf("\r\r");
                var p:String = bodyPhrases.substring(1,para);
                paraCount++;
                bodyPhrases = bodyPhrases.replace(p,"");
            }        

It takes more loop. how to find it in a easy way.

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
viji
  • 119
  • 2
  • 3
  • 12

1 Answers1

1

You could alternatively use a regular expression:

var regExp:RegExp = /\r\r/g;
var paragraphCount:int = bodyPhrases.match(regExp).length;

and you can check that it works with:

trace(paragraphCount:int);

Useful link to learn more about RegExp: http://coursesweb.net/actionscript/regexp-regular-expressions-actionscript

Creative Magic
  • 3,143
  • 3
  • 28
  • 47
  • It worked. But it returns value as 1. But above given string contains 3 para. how to proceed further ? Thanks in advance!!! – viji Aug 24 '13 at 10:12
  • Well, I did a small research and got an interesting peace of info. If you use Unix, use \r, Mac OS \n, Win \r\n Here's in detail: http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them – Creative Magic Aug 24 '13 at 10:53