Correlation function summary Java string classes and arrays

A, string class

1, string search

(1) str.indexOf (substr); // return substr in str first appears in the index

   str: any string objects

   substr: string to search for

(2) str.lastIndexOf (substr); // Returns the index of the last occurrence of substr in str years

   str: any string

   substr: string to search for

2, to obtain the specified index character

   str.charAt (index); // Returns the index is the index of the character, index to int

   str: any string objects

   index: specified index

3, to get a substring

(1) str.substring (beginIndex); // return from beginIndex taken start until the end of the string substring, an int beginIndex

(2) str.substring (beginIndex, endIndex); // return from the beginIndex (inclusive) to intercept a endIndex (not included) ending substring

4, removal of spaces

   str.trim () // return copy of the string, ignore leading spaces and trailing spaces

   eg: String str = " ab cd ";

     String str1 = str.trim();//str1 = "ab cd";

5, the replacement string

   str.replace (oldStr, newStr); // replace () will replace all oldstr newStr

   oldStr: To replace a character or string

   newStr: string for replacing the original content of the new

6, the start and end of the string is determined

(1) str.startsWith (prefix); // str determines whether the prefix is ​​a prefix, prefix of string type

(2) str.endsWith (suffix); // str determines whether the suffix is ​​a suffix, suffix of string type

7, it is determined whether the same character string

(1) str1 == str2;

(2) str.equals (substr); // the same characters (case sensitive) and a length, returns true

(3) str.equalslgnoreCase (substr); // ignore case compare

8, by comparing two strings lexicographically

   str.compareTo (substr); // str substr located before returning a positive integer, then returns a negative integer, is equal to (the equals returns true) returns 0

9, character case conversion

   str.toUpperCase (); // uppercase letters to lowercase

   str.toLowerCase (); // lowercase letters changed to uppercase

10, the string is divided

(1) str.split (sign); // The split given string sign, sign for the string type, regular expressions may be used

(2) str.split (sign, limit); // limit limits the number of divisions

11, the date and time format string

   str.format(format, args);

   format: format string (character conversion)

   args: an identifier referenced by the format string description format

   You must import java.util.Date class

(1) Date Format

   eg:

   

   Output: 2019

       19

(2) Time Format

(3) the date and time format combination

(4) a conventional type format

   And (1) much the same, just not the same format

12, Regular Expressions

   str.matches (regex); // check whether the str format represented by regex

13, string builder

(1) str1 + str2; // generates a new string instance, increasing the overhead

(2) StringBuilder class greatly improves the efficiency increase in frequency of the string

   StringBuilder builder = new StringBuilder ( ""); // creates a string builder

(3) builder.append (content); // append to the string builder content, content can be any data type or other objects

(4)builder.insert(index, arg);//向字符串生成器指定的位置index插入arg,arg可以是任何数据类型或其他对象

(5)builder.delete(start, end);//删除字符串从指定的索引start(包含)开始到end(不包含)的内容

二、数组

   必须先导入java.util.Arrays类

1、填充替换数组元素

(1)Arrays.fillf(arr, value);//将arr数组全部初始化为value

(2)Arrays.fill(arr, start, end, value);//将arr数组从start(包含)到end(不包含)初始化为value

2、对数组进行排序

   Arrays.sort(arr);//将arr数组中的元素按照字典序排序,数字优先于字母,大写字母优先于小写字母

3、复制数组

(1)Arrays.copyOf(arr, copylenth);//将arr从0开始复制,复制长度为copylenth,不够用0补

(2)Arrays.copyOfRange(arr, start, end);//将arr从start(包含)开始复制到end(不包含)停止,start必须在arr长度范围内,end可大于arr长度

4、数组查询

(1)Arrays.binarySearch(arr, key);//返回arr数组中key的索引值,否则返回“-1”或者“-插入点”。如果有多个则无法保证返回的是哪一个

                       //调用函数前必须对数组排序

(2)Arrays.binarySearch(arr, start, end, key);//在arr从start(包含)到end(不包含)的范围内查找key,返回索引

Guess you like

Origin www.cnblogs.com/xiaohanghuo/p/11976164.html