Use ivx achieve formatted string Experience

When the case is frequently used text component is present some information, and sometimes the information necessary to deal with some formatting, such as the display of the mobile phone number as we may want to show only demo in the beginning and the end of this number, while the middle four hidden, today said about how to deal with such string formatting.
Here Insert Picture Description

1. Data Binding
first demo is the establishment of a one-dimensional array to store several phone number, and then as an array loop to create a data source, for text components in a container data binding, and in the current 1, insert some data function method. JavaScript functions are methods and syntax used here. (JS method needs to use the data model binding)
Here Insert Picture Description
2. regular expression
regular expression is a logical formula of string operations, it is to use some combination of a particular pre-defined characters, and these particular character, composed of a "rule string", this "rule string" is used to express a filtering logic of the string. Regular expressions are a very powerful feature in itself, but to be familiar with special needs a lot of practice. There is a saying that says that when you use regular expressions to solve a problem, you now have two problems. Here only a brief demo of the meaning of the code, interested friends can go to learn in detail about regular expressions.
1 is a current data string object, there is a replace () method, as will be appreciated in parentheses "," the contents back to double quotes between two alternate content "/", g represents a global matching performed, That finds all matches rather than stopping after the first match. Between the content "/" can be divided into three parts, ^ (\ d {3} ), \ d {4}, (\ d {4}) $. "^" Character string matches the input start position, "$" matches the position of the input end of the string, \ D represents a digital lookup, {3} represents digits lookup. So here is the phone number into the beginning of the three, four and three parts among four end. SUMMARY comma double quotes inside, $ 1 represents (\ d {3}), $ 2 represents the (\ d {4}), where the use of regular expressions in the capture set, the capture group is to the regular expression content type neutron expression matching, is saved to memory in digital numbers or explicitly named the group behind the convenient reference only added to the bracketed portion of the captured (capture specific group number naming and references can be Internet access to information). Finally, this completes the first three and four reserved after the two parts, and into the middle , splicing out the final result.
Here Insert Picture Description
3.substr () method
Here again a relatively simple method is to use substr (), that is, from the interception of a substring in the original string. The method parameters only two numbers, representing the start position of the front, rear is the length of the substring. Detailed description can refer to FIG.
Here Insert Picture Description
For example demo of the phone number "13245876647", position number 0-12 from left to right, we have to realize the effect of the demo can be written as follows, the final effect is the same. The first part is taken of the first three phone number, and the second part is "
" character string, the third part is the last four digits of the phone number. It should be noted here that if the variable data binding does not require double quotes, if it is what you enter is the need to double quotes, and double quotes must be in English, and finally with a plus sign to connect the various parts on it.
Here Insert Picture Description
4.slice () method
as well as a method may be selected, is comparative () method slice () method, with the difference that the parameters can be found substr substr () method specifies the start position and length, the slice () method parameters are directly specified by start and end positions.
Here Insert Picture Description
It can also be written as the pattern of FIG. (Taken note string contains the starting position, but not including the stop position so fill (0,3))
Here Insert Picture Description
5.substring () method
similar to the slice () method there is a substring () method. And slice () method is different though his argument is also the starting and ending coordinates, but he does not accept a negative number as a parameter, and if you do not accidentally end coordinate fill even smaller than the starting coordinates, then the method in execution it will replace the former two parameters, if the starting coordinates and ending coordinates are equal, it will return an empty string.
Here Insert Picture Description
Use substring () method to achieve the effect of the demo FIG. Note that like slice () method string containing the start position taken, but it does not contain a fill end position (0,3)
Here Insert Picture Description
6.split () method
Then share a relatively common method to it, it is split () method. For example, submitting time the database is "2020-03-16 15:37:36", we might only want to show the front desk to show the date, time and eliminating the need for minutes and seconds.
Here Insert Picture Description
In this case the following data may be bound. .split ( "") [0] is the original strings by double quotes string segmentation, this is a null string space divided into two elements, [0] is extracted array element of the segmented the first element, if you fill out the split ( "") [1] is obtained when minutes and seconds.
Here Insert Picture Description
7. Conversion case the toLowerCase () and toUpperCase ()
Finally, add a case conversion method bar, also very simple, it is an example to convert it to lowercase. Such data binding can put text variables in all lowercase letters are converted to uppercase.
Here Insert Picture Description
8. A summary
of the format string processing methods are diverse, we can also explore their new approach. ivx itself also maintains a number of functions and interface methods in combination, but also have a special function components, in dealing with some of the small details still very convenient. Knowledge really is more the more comprehensive it okay!

Guess you like

Origin blog.51cto.com/14556317/2478983
Recommended