views:

68

answers:

2

i need to trim a string to its first 100 characters using jquery/javascript.

also is it possible to scan a string and look for a particular combination of keywords such as #key?

thanks a lot for the help.

+1  A: 

To answer your first question, regular JavaScript will do just fine:

 var trimmed = MyString.substr(0, 100);

To your second, you can use regular expressions to scan for patterns: http://www.regular-expressions.info/javascript.html

Justin Ethier
+6  A: 

To keep only the first 100 characters of a string use substring:

s = s.substring(0,100)

To search for a substring use indexOf:

s.indexOf("#key")
Mark Byers
+1, simple solution. `match` can also be used for searching: http://www.w3schools.com/jsref/jsref_match.asp
Felix Kling