Common utilities like the String class

1. What is the String class
because the program often involves the design algorithm processing and related character sequences, so Java expertise to provide the String class to handle the character sequence. Since such in the java.lang package, and this package is default reference String class can use directly. But the Java String class is defined as the final class, that is not such a parent class.

.String class two commonly used methods
1.String (char a []) String This is a construction method in which use is:

char a[]={'j','a','v','a'};
String s=new String(a);

Equivalent: String s = new String ( "java");

2.String (char a [], int startIndex, int count) is also a constructor String class, its usage is:

char a[]={'0','1','2','3','4','5','6','7','8','9'};
String s=new String(a,2,4);

Equivalent String s = new String ( "2345");

3.public int length (), this method is to obtain a String object to a character sequence length method, which is used as follows:

String china="中华人民共和国";
int n1=china.length();//n1就是china的字符序列长度

4.public boolean equals (String s) whether the method is a method of comparing two strings are the same, which is used as follows:

boolean a ,b;
String tom = new String("天道酬勤");
String boy = new String("天道酬勤");
String jerry = new String("投机取巧");
a=tom.equals(boy);//a的值为true
b=tom.equals(jerry);//b的值为false

5.public boolean startsWith (String s), public boolean endsWith (String s) methods
which are currently determining whether the String object prefix or suffix sequence of characters is specified by the parameters String
character sequence s of objects, which are used as follows:

boolean a,b,c,d;
String tom="天气预报";
String jerry = "比赛终止";
a=tom.startsWith("天气");//a的值为true
b=jerry.startWith("天气");//b的值为flase
c=tom.endsWith("终止");//c的值是false
d=jerry.endsWith("终止");//d的值是true

6.public int compareTo (String s), this method is a lexicographical ordering parameter specified String object size s character sequence comparison, used as follows:

String str="adcde";
则有:
str.compareTo("boy")小于0
str.compareTo("aba")大于0
str.compareTo("abcde")等于0

Supplementary (string size comparison rule): is the ASCII character value determination, comparison rules, starting from the first character sequentially comparing rearwardly until the occurrence of different characters, different then the first character of the ASCII value determining the size comparison, for example: "abc" and "abadfg" comparison, the same as the first two characters, and the third character different, is a 'c', one of 'a', c ASCII code since the ratio a large, so the result of the comparison of these two strings are: "abc"> "abadfg".

7.public boolean contains (String s), this method is to determine the sequence of characters in the current sequence of characters contains a String object parameter s is used as follows:

String tom="student";
那么:
tom.contains("stu")的值是true
tom.contains("ol")的值是false

8.public int indexOf (String s) and public int lastIndexOf (String s)
This method is used to retrieve the position of the first occurrence of the character sequence and retrieving the position of the character sequence s s appear last, if not retrieved or -1. To specify the position of starting the search, there indeOf (String s, int startpoint) and lastIndexOf (String s, int startpoint) , behind temperament int startpoint starting the search for the location, is used as follows:

String tom="I am a good cat";
int n,m,a;
a=tom.indexOf("I");a的值为0
n=tom.indexOf("a");n的值为2
m=tom.indexOf("good",2);m的值为7

Note: The sequence of characters String object's escape character is a character, such as \ n represents the return. \ Path is a character representation.

9.public String substring (int startpoint) This method is used to obtain a new String object, the new object is a character sequence replication startpoint current String object to begin the sequence of characters resulting sequence of characters on the last character position. Usage is as follows:

String tom="I like football";
String str = tom.substring(1);//str的字符序列为" like football"

There substring (int start, int end) as above, but it is to copy the characters from start to end-1 range. E.g:

String tom="I like football";
String str = tom.substring(26);//str的字符序列为"like"

Three .String usage scenarios
1. Use string constant

String s1,s2;
s1="你好";
s2="你好";
则s1==s2的值为true

2. The string concatenation
String object can be "+" used for concatenation, i.e., end to end, to obtain a new String object, for example:

String you="你";
String me="我";
String testOne;
testOne=you+me;//testOne为"你我"

3. Conversion basic string data
1) convert the string data to
be used the following methods:
public static String valueOf (n-byte)
public static String valueOf (n-int)
... and so on, simply by changing the data type of the back on it.
E.g:

String str=String.valueOf(1235434.345)

2) conversion to a data string
, for example:

int x;
String s="234";
x=Integer.parseInt(s);//可以根据需要将parseInt中的Int改为其他数据类型

A string representation of the object 4.
Object class has a public String toString () method, this method can make a String object is obtained by calling the object represented by this method. This method is generally returned in the form of: creating an object class name @ string representation of the object with reference to
examples:

 class TV{
   double price;
   public void setPrice(double m){
   price=m;
   }
   public String toString(){
      String oldStr=super.toString();
      return oldStr+"\n这是电视机,价格是:"+price;
      }
   }


public class Main{
   public static void main(String args[]){
     Date date=new Date();
     System.out.println(date.toString());
     TV tv=new TV();
     tv.setPrice(5345);
     System.out.println(tv.toString());
   }
}

Here Insert Picture Description

The array of character strings, byte array

1) an array of character strings
String class provides a sequence of characters stored in the String object array: public void getChar (int start, int end, char c [], int offset) This method of current String object copying a portion of the sequence of characters specified by parameter c in the array, the character sequence from the start position to the character position on the end-1 c copied to the array and begins at offset from the characters stored array c.
Further, there is a concise manner the entire sequence of characters in a String object stored character array method:
public char [] toCahrArray (); This method returns an array of characters, the character sequence length String object with the array It is equal to (i-th character units exactly the i-th character)
example:

public class Example8_6{
  public static void main(String args[]){
  char []a,c;
  String s="1945年8月15日是抗战胜利日";
  a = new char[4];
  s.getChars(11,15,a,0);
  System.out.println(a);
  c="十一长假期间,学校都放假了".toCharArray();
  System.out.println(c);
  }
}

2) strings and bytes
a class constructor String String (byte []) with a specified byte array configuration String object.
String (byte [], int offset , int length) construction method with a portion of the specified byte array, i.e. the array offset from the start position to the start byte length, construct a String object.
public byte [] getBytes () method uses the platform's default character encoding, the sequence of characters currently stored in the String object to a byte array, and returns a reference to an array.
example:

public class Example{
 public static void main(String args[]){
     byte d[]="java你好".getBytes();
     System.out.println("数组d的长度:"+d.length);
     String s=new String(d,6,2);
     System.out.println(s);//输出"好"
     s=new String(d,0,6);
     System.out.println(s);//输出"java你"
 }
 }

3) the encryption algorithm of the string
materials is defined as: character sequence using the password as a String object code sequence of characters to another String object SourceString encrypted. Rule:
char [] = p password.toCharArray ();
sequence of characters assumed that the length of the array p is n, then it will be encrypted in order to sourceString of n characters as a group (the last set of characters may be less than n ), for each set of characters do summation with the corresponding character array of p.

6. Regular expressions and alternative character string and an exploded
1) regular expression
which is a sequence of characters in a String object, the character sequence contained characters have special significance, these special characters called meta-characters, such as "\ dcat" the \ d representative of any of a number of 0 to 9.
String object calls public boolean matches (String regex) method may determine whether the current sequence of characters String object are specified regex and regular expression matching. (Regular expressions can be used to efficiently filter)

2) the replacement string
String object can call public String replaceAll (String regex, String replacement) method returns a new String object. This new object character sequence is the current sequence of characters String object and all sub-character sequences matching parameters regex, after replacing the character sequence with the replacement of parameters obtained character sequence. For example:
String = STR "12hello567bird" .replaceAll ( "[A-zA-A] +", "Hello");
this operation is to replace the character sequence (i.e. 12hello567bird) all the English characters "Hello", i.e., str "12 567 Hello Hello."

3) decomposition of the sequence of characters
String class provides a method of public String [] split (String regex ).
This method may use the parameters specified by the regular expression regex as this word division marks decomposition String object sequence, and the decomposition of String of words stored in the array. E.g:

String str="1949年10月1日是中华人民共和国成立的日子";
String regex="\\D+";
String degitWord[] = str.split(regex);
为以非数字字符序列作为分隔符 有:
digitWord[0]="1949";
digitWord[1]="10";
digitWord[2]="1";

About the use of strings, there are many usage scenarios, but also to learn.

Published 35 original articles · won praise 0 · Views 1296

Guess you like

Origin blog.csdn.net/c1776167012/article/details/103897078