USD in code $

Source: Internet
Author: User
Tags add numbers
ArticleDirectory
    • Javascript
    • Java
    • Regular Expression
    • CSS
    • PHP
    • BASH Shell
    • Ibatis
    • Visual FoxPro
    • Others

This topic is a bit subject-oriented. In fact, the main content of this article should be"CodeDollar sign in )". I want to summarize the role of the dollar sign I have seen in the Code. Of course, it does not mean that it is used as a formatted character before a number as a coin unit.

Valid variable name

When getting a variable name, we usually only use letters, numbers, and underscores, and numbers cannot start. But in manyProgramming LanguageVariable names are more broadly limited. Many special characters can also be used as variable names, including $.

Javascript

Javascript allows the $ symbol to be used as a variable name or method name for effective identification. The most famous example is jquery. $ is everywhere throughout the use process. $ Is just an alias for jquery variables, but it is also short and highly identifiable. It brings a lot of convenience when writing code.

 
$ ("Button"). Click (Function() {$ ("P"). Toggle ();});

You can use JavaScript variable name validator to verify the validity of JavaScript variables.

Java

Java can also use $ as a valid variable, but it cannot be defined by users, but is reserved for the compiler.

Variable names are case-sensitive. A variable's name can be any legal Identifier-an unlimited-length sequence of Unicode letters and digits, beginning with a letter,The dollar sign"$", Or the underscore character"_". The Convention, however, is to always begin your variable names with a letter, not"$"Or"_".Additionally, the dollar sign character, by convention, is never used at all. you may find some situations where auto-generated names will contain in the dollar sign, but your variable names shoshould always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with"_", This practice is discouraged. white space is not permitted.

For example, a nested class (nested class), after compilation, the actual class names are all layers of Nested classes "bonded" with the $ separator. Let's take a look at the following simple example:

 PackageCom. ider;Public ClassDollar {Static ClassSign {}Public Static VoidMain (string [] ARGs) {dollar. Sign sign=NewDollar. Sign (); system. Out. println (sign. getclass (). getname ());}}

 

Output result:

 
// Com. ider. Dollar $ sign
Placeholder escape Regular Expression

$ Represents the end position of a string in a regular expression. For example, the following expression is used to check whether a string ends with a number.

VaRReg =/\ D $/; Reg. Test (STR );

In the implementation of some regular expressions, $ can also be used as the expression separator (delimiter) to indicate that the wrapped content is part of the expression, such as PHP:

 
//Match string that contain 'php'$ Pattern = '$ PHP $'; Preg_match ($ pattern, $ subject, $ matches );

Only the content of the expression must be escaped with a slash (\) If $ is used. If it is necessary to make it truly match the $ symbol instead of the end, it must be further escaped, then the system will be dizzy. Therefore, $ is generally not used as the separator. Generally, a backslash (/) is used to match the Javascript format, or # is used because it is rarely used.

In addition to matching, regular expressions are more important for replacement. The most powerful replacement is back-reference ). Most text editors that support regular expressions use backslash (\ 1, \ 2, \ 3…) to add numbers ...) To indicate the matched content. However, JavaScript uses $ to add numbers ($1, $2, $3 ...) .

VaRFriends = 'friends of ider, friend of angie';VaRResult = friends. Replace (/(Friends ?) Of (\ W +)/g, "$2's $1 ");

This article has been introduced in the eccentric javascript string operation method.

CSS

Attribute selector of CSS not only allows you to set styles for tags that contain specified attributes, but also for the content contained in attributes. One of them is to specify the end Of the attribute content, which is similar to a regular expression and is also useful. For example, if you want to add a border for a jpg image to make it look like a frame, you can

 
IMG [SRC $ = ". jpg"]{Border:10px solid # dddddd;}

 

Get variable content PHP

PHP variables always carry the $ symbol (except for the define constant), which can be considered as part of the PHP variable, but not strictly speaking, it is more like taking the content contained in a variable. For example, the following code:

$ X= 7;$ Y= "X";Echo$$ Y;

The result is

 
7

The first $ goes to the content in Y as X, and the second $ gets the content in X as 7. In addition, you can also understand the object-based content.

Not only PHP, but also shell scripts and Perl are the same. For example, shell scripts are completely different. Variable values are assigned without $ statements, and $ is used to obtain the variable content.

Other bash Shells

In bash shell, $ is often used as a command prompt character (command prompt ). You can also modify the environment variable ps1.

In addition, $ is also a variable in addition to the content previously mentioned as a variable. It stores the ID of the current process.

Ibatis

This is a Java ORM framework, but I first came into contact with ibatis.net transplanted on the. NET platform. In this framework, the $ variable is used to escape its content to prevent SQL injection attacks. Or use # without escaping.

Visual FoxPro

UseCsearchfor $ csearchinTo check whether the character style appears in another character style.

Others

In other code, you can see different usage of $. Please add. I don't know if the $ symbol can have different functions in the code.

References:
  1. Valid JavaScript variable names-Mathias bynens
  2. Variable-the Java tutorials
  3. The 30 CSS selectors you must memorize-nettuts +
  4. What are the special dollar sign shell variables? -Stackoverflow
Welcome to my new blog address to read this 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.