Home  >  Article  >  WeChat Applet  >  The use of strings in small programs

The use of strings in small programs

hzc
hzcforward
2020-06-18 09:50:254922browse

A string or string is a string of characters composed of numbers, letters, and underscores. - It is a data type that represents text in a programming language. In programming, a string is a continuous sequence of symbols or values, such as a symbol string (a string of characters) or a binary digit string (a string of binary digits).

Usually the entire string is used as the operation object, such as: finding a substring in the string, obtaining a substring, inserting a substring at a certain position in the string, deleting a substring, etc. In the WeChat applet, string strings can be declared with '' or "", and the length can be obtained with the length attribute. Commonly used methods can be divided into search, interception and conversion.

1 Search

charAt(index): Get the character at the specified position (the subscript index starts from 0);

charCodeAt(index): Get the Unicode encoding of the character at the specified position (the subscript index starts from 0);

indexOf(searchvalue,start): Search the substring backward from the beginning of the string, but no one was found If matched, -1 is returned; searchvalue is the string value to be retrieved, start is the starting position, and the default is 0.

lastIndexOf(searchvalue,start): Search for substrings from the end of the string forward, if no match is found, -1 is returned; searchvalue is the string value to be retrieved, start is the starting position, and the default is the end one character.

localeCompare(target): Compare two strings in local specific order;

match(regexp): Array to store matching results;

search(searchvalue) : Specifies the starting position of the searched string; searchvalue is the searched string or regular expression.

Page({  //字符串查找方法例子
  onLoad: function(options) {
    let str = '我不是程序,我是码农。';
    let at = str.charAt(3);
    let codeAt = str.charCodeAt(1);
    console.log('字符串:%s', str);
    console.log('charAt(3)=%s, charCodeAt(1)=%s', at, codeAt);
    let i = str.indexOf("码农");
    let l = str.lastIndexOf("程序");
    console.log('indexOf=%d,lastIndexOf=%d', i, l);
    let c = str.localeCompare('程序');
    let m = str.match('程序', '码农');
    console.log('localeCompare=%s,match=%s', c, m)
    let s = str.search('程序');
    console.log('search=%s', s)
  }
})

2 Interception

slice(start,end): Extract a certain part of the string , and returns the extracted part as a new string. Start is a required field, the starting index of the fragment to be extracted, and the first character position is 0. end is optional, followed by the subscript of the end of the segment to be extracted.

split(separator,limit): separator string or regular expression, optional. limit is optional and defaults to the maximum length of the array.

substring(from,to): from is required, a positive integer, specifies the position of the first character of the substring to be extracted in the string. to is optional. Positive integer, by default the returned substring will go to the end of the string.

Page({  //字符串截取例子
  onLoad: function(options) {
    let str = '不会编码的人,也能称码农?';
    let s = str.slice(2, 4);
    console.log('字符串=%s', str);
    console.log('str.slice(2,4)=%s', s);
    let a = str.split(',');
    console.log('str.split(,)=%s', a);
    let sb = str.substring(1,2);
    console.log('substring(1,2)=%s', sb);
  }
})

3 Conversion

toString() method; numerical value, string, object, Boolean; All have toString methods; the only thing this method can do is to return the corresponding string; null and undefined do not have toString() methods;

String() is a forced conversion, and the result of null conversion is null; undefined conversion The result is undefined; for the rest, if there is a toString() method, the method is called and the corresponding result is returned;

valueOf: Returns the original value of the String object, implicitly called;

String .fromCharCode(n1, n2, ..., nX): Convert Unicode encoding to a character;

toLowerCase: Used to convert a string to lowercase;

toLocaleLowerCase: and toLowerCase( ) The difference is that the toLocaleLowerCase() method converts the string to lowercase according to the local method. Only a few languages ​​(such as Turkish) have local case mapping, so the return value of this method is usually the same as toLowerCase().

toUpperCase: Convert the string to uppercase.

toLocaleUpperCase: Unlike toUpperCase(), the toLocaleUpperCase() method converts the string to uppercase according to the local method. Only a few languages ​​(such as Turkish) have local case mapping, so the return value of this method is usually the same as toUpperCase().

Page({  //字符串转换例子
  onLoad: function(options) {
    let str = "i love Programming.";
    let v = str.valueOf();
    console.log('字符串=%s', str);
    console.log('valueOf=%s', v);
    let l = str.toLowerCase();
    let u = str.toUpperCase();
    console.log('toLowerCase=%s,toUpperCase=%s', l, u);
    let f = String.fromCharCode('30721', '20892');
    console.log('fromCharCode=%s', f);
  }
})

4 Others

##concat(

string1, ..., stringX): Connect two or more strings and return a new string;

trim: Remove the whitespace on both sides of the string;

replace(searchvalue, newvalue): Find matching substrings in the string and replace the substrings that match the regular expression.

Page({  //字符串其他方法例子
  onLoad: function(options) {
    let str = " 我是程序猿";
    let c = str.concat(',','我喜欢听歌.');
    let t = c.trim();
    console.log('concat=%s', c);
    console.log('trim=%s', t);
    let r = t.replace(/我/g,'你');
    console.log('replace=%s',r);
  }
})

Recommended tutorial: "

WeChat Mini Program"

The above is the detailed content of The use of strings in small programs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete