String extension in Microsoft AJAX Library

Source: Internet
Author: User
Document directory
  • String. startsWith Function
  • String. endsWith Function
  • String. trim Function
  • String. format Function

Provides extensions to the basic ECMAScript (JavaScript) String object through static and instance methods.

String. startsWith Function

OKStringWhether the start part of the object matches the specified string.

UseStartsWithThe function is deterministic.StringWhether the start part of the object matches the specified string.StartsWithThe function is case sensitive.

/* Paramprefix: String to match the start part of the String object. return: If the start part of the String object matches the prefix, the value is true; otherwise false */var hasPrefix = myString. startsWith (prefix );

 

String. endsWith Function

OKStringWhether the end of the object matches the specified string.

UseEndsWithThe function is deterministic.StringWhether the end of the object matches the specified string.EndsWithThe function is case sensitive.

/* Paramsuffix: the String to be matched with the end of the String object. Return: true if the end of the String object matches suffix; otherwise, false. */Var hasSuffixVar = myString. endsWith (suffix );

 

String. trim Function

SlaveStringRemoves leading and trailing white spaces from the object.

UseTrimFunctions can be retrieved from the currentStringRemoves leading and trailing white spaces from the object. Spaces and tabs are both blank characters.

/* Paramreturn: a copy of the string, which removes all white spaces from the start and end of the string */var trimmedStringVar = myString. trim ();

Similar to this function, the String. trimStart function and the String. trimEnd Function

Sample Code:

<body>    <form id="form1" runat="server">        <asp:ScriptManager runat="server" ID="ScriptManager1">        </asp:ScriptManager>        <script type="text/javascript">            // Determines if a string has a specified suffix as             // the last non white-space characters regardless of case.            function verifyStringSuffix(myString, suffix)             {                // Remove any trailing white spaces.                myString = myString.trimEnd();                // Set to lower case.                myString = myString.toLowerCase();                // Determine if the string ends with the specified suffix.                var isTxt = myString.endsWith(suffix.toString());                if (isTxt === true)                {                    alert("The string \"" + myString + "\" ends with \"" + suffix + "\"");                 }                else                {                   alert("The string \"" + myString + "\" does not end with \"" + suffix + "\"");                 }            }            verifyStringSuffix("some_file.TXT  ", ".txt");        </script>    </form></body>

 

String. format Function

Replace each format item in the String object with the text equivalent item of the corresponding object value.

/* Paramformat: format String args: array of objects whose format is to be set return: String copy with the applied format setting */var s = String. format (format, args );

UseFormatThe function can replace the specified format item with the text representation of the corresponding object value. The args parameter can contain a single object or an array of objects. The format parameter is composed of zero or multiple fixed text sequences and one or more format items. Each format item corresponds to an object in objects. At runtime, each format item is replaced by the string representation of the corresponding object in the list.

A format item contains a number enclosed in braces (for example, {0}) that identifies an item in the objects list. The number starts from scratch. To specify a single braces in the format, specify two leading or trailing braces, namely, "{" or "}". Nested braces are not supported.

By providingToFormattedStringThe special format setting object of the method. You can specify a custom format setting routine for the format item.ToFormattedStringThe method must accept a string parameter and return a string. At runtime,FormatThe function will pass any string enclosed in braces of the corresponding parameter descriptionToFormattedStringsMethod.ToFormattedStringThe string returned by the method will be inserted to the location of the corresponding parameter specifier in the formatted string.

Sample Code:

// Define an class with a custom toFormattedString// formatting routine.Type.registerNamespace('Samples');Samples.ToFormattedStringExample = function() {}Samples.ToFormattedStringExample.prototype = {    toFormattedString: function(format) {        return "This was custom formatted: " + format;    }}Samples.ToFormattedStringExample.registerClass('Samples.ToFormattedStringExample');var result = "";// Format a string.result = String.format("{0:d}", 123);// Displays: "123"alert(result);// Format a string with an object that has // a custom toFormattedString method.var o = new Samples.ToFormattedStringExample();result = String.format("{0:12345}", o);// Displays: "This was custom formatted: 12345"alert(result);

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.