String processing in Javascript

Source: Internet
Author: User
1. Get the string width

< Script >
Function Bounddim (oobject)
{
VaR Colltextarea = Document. All. Tags ( " Textarea " );
If (Colltextarea ! = Null )
{
VaR Otextrange = Oobject. createTextRange ();
If (Otextrange ! = Null )
{
Alert ("The width is \ n"+Otextrange. boundingwidth );
}
}
}
</ Script >
</ Head >
< Body >
< Textarea Cols = 100 Rows = 2 ID = Otextarea onclick = " Bounddim (this) " > Aaa </ Textarea >

2. halfwidth and fullwidth Conversion

How to deal with JS again

Function qjtobj (OBJ)
{
VaR Str = OBJ. value;
VaR result = "" ;
For (VAR I =   0 ; I < Str. length; I ++ )
{
If (Str. charcodeat (I) = 12288 )
{
Result+ =String. fromcharcode (Str. charcodeat (I)-12256);
Continue;
}

If (Str. charcodeat (I) > 65280   && Str. charcodeat (I) < 65375 )
Result + = String. fromcharcode (Str. charcodeat (I) - 65248 );
Else  
Result + = String. fromcharcode (Str. charcodeat (I ));
}  

OBJ. Value = Result;
}  

 

3. How to Use JSON

The JSON structure is based on the following two points:
1. in different languages of the set of "name/value" pairs, It is understood as an object, record, structure (struct), Dictionary (dictionary ), hash table and keyed list
2. The ordered list of values is interpreted as an array in most languages)

JSON represents a JavaScript Object in a specific string. If a string with such a form is assigned to any JavaScript variable, the variable becomes an object reference, and this object is constructed by the string.

JSON format
· An object is a set of attributes and value pairs. An object starts with "{" and ends "}". Each attribute name and value is separated by a ":" prompt, and.
· An array is a set of ordered values. An array starts with "[", ends with "]", and values are separated.
· The value can be a string, number, true, false, or null in quotation marks, or an object or array. These structures can be nested.
· The definition of a string is basically the same as that of C or Java.
· The definition of numbers is also basically the same as that of C or Java.

VaR Userliststr =   ' [{Name: "Michael", email: "17bity@gmail.com", homepage: "http://www.jialing.net /"}, '
+   ' {Name: "John", email: "john@gmail.com", homepage: "http://www.jobn.com /"}, '
+   ' {Name: "Peggy", email: "peggy@gmail.com", homepage: "http://www.peggy.com/"}] ' ;
Function Showuser ()
{
Eval ( " Userlist = " + Userliststr );
Alert (userlist [ 1 ]. Name );
}

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.