This project is read-only.

String Extensions

NinJa provides a ton of extended functions for the base string object in JavaScript, along wrapping existing functionality with new names that follow closely the naming scheme of the .NET Framework. Here are some of the functions provided:

  • ToBoolean(), ToColor(), ToDateTime(), ToFloat(), ToInt() - Converts the string to the specific data type. Any failed conversion will throw an exception.
  • IsDate(), IsDateTime(), IsFloat(), IsInt(), IsTime() - Returns whether the string represents a valid data type.
  • PadLeft(number, char) - Returns a copy of the string that pads the left of the string up to the number provided with the char.
  • PadRight(number, char) - Returns a copy of the string that  pads the right of the string up to the number provided with the char.
  • Trim(), TrimLeft(), TrimRight() - Returns a copy of the string with removed white space from either from both sides  or just from the left or right sides.
  • Contains(string, ignoreCase) - Returns whether the provided string is found in the string. Ignore case, if set to true, will make the search case insensitve.
  • IndexOf(string, startIndex) - Returns the index of the string if it's found. If startIndex is provided, it starts at the specified character. If the string is not find, it returns -1.
  • LastIndexOf(string, startIndex) - Same as IndexOf except if finds the last occurance.
  • Trunctate(length, moreString) - This will return a copy of the string, truncating it at the given length. If moreString is provided, this will be attached to the resulting string to indicate that it was truncated.

This is just a portion of the extra functions provided for strings. There's much more that can be find simply by studying the Intellisense in Visual Studio.

String.Format & String.BindFormat

NinJa provides a String.Format function that provides the basic functionality of String.Format in .NET where indexed tokens surrounded by curly braces, "Such as {0} <- that", and the parameters provided there after are replaced with the provided markers. To escape curly braces, simply use {{} and {}} to use { and } respectively.

String.BindFormat is slightly different. Instead of using indexed tokens, you use the names of properties to replaced the tokens with. This kind of "binding" uses the binding syntax provided by NinJa that's goes well beyond the scope of this page. Here's an example:

$Span("clock").Text(String.BindFormat("{Hours}:{Minutes}:{Seconds}", DateTime.Now());
This will display the current time. What's important to note here is that Hours, Minutes, and Seconds are not "fields" or "members" of the DateTime object, they are property functions. With BindFormat, you can provide either field/member names or function names that return values. BindFormat will take care of it all!

Last edited Jun 15, 2012 at 6:13 PM by dahrkdaiz, version 1


No comments yet.