I had to do something like this in XSLT. If the string is not too long (for some definition of "too long"), you can replace all newlines with "" and then take the difference in the two string lengths.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)