1. String Class Concept
In Java programs, the Java.lang package is imported by default, and the string class such as Java.lang.String belongs to the final class, the user cannot extend the string class, and the string class has no subclasses. The common methods of the string class are as follows:
public int length (); public boolean equals (); public boolean startsWith (); Public Bolean endsWith (); public int compareTo (); Package com. Example1; Public boolean contains (); public int indexOf (); Public String substring (int startpoint); Public String trim (); public Staticparseint (); public static String valueOf ();
public void GetChars (int start,int end,char[],int offset); Public char[] to Chararray (); Public Boolean matches (String regex);
Instance:
The for (string i:a) {} Traversal output is used in the For loop output to determine the type of a before the I De type is primarily seen before the declaration
Import Java.util.Arrays;
public class Example_2 {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String[] A = {"Mellon", "apple", "pear"};
String[] B = {"Xigau", "Apple", "Xihuonshi"};
System.out.println ("Arrange the values in the array using a dictionary order");
System.out.println ("****************");
Sortstring.sort (a);
for (int i = 0; i < a.length; i++) {
System.out.println ("" + a[i]);
}
System.out.println ("***************");
System.out.println ("Using the arrays sort dictionary in the class Library");
Arrays.sort (b);
for (String i:b) {
System.out.println (i);
}
}
}
Class Sortstring {
public static void sort (String a[]) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < A.length; J + +) {
if (A[j].compareto (A[i]) < 0) {
String temp = a[i];
A[i] = A[j];
A[J] = temp;
}
}
}
}
}
Instance rewrite of toString
Package com. Example1;
Import Java.util.Date;
public class Example8_5 {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Date date = new Date ();
System.out.println (Date.tostring ());
TV2 TV2 = new TV2 ("Changhong TV");
System.out.println (Tv2.tostring ());
}
}
Class TV2 {
String name;
Public TV2 () {
}
Public TV2 (String s) {
THIS.name = s;
}
Public String toString () {
String oldstr = super.tostring ();
return oldstr + "\ n This is the TV, Brand is:" + name;
}
}
Using the string class for encryption and decryption
Package com. Example1;
Import Java.util.Scanner;
public class Example8_8 {
public static void Main (string[] args) {
String soucestring = "Ning I love You";
Encryptanddecrypt person = new Encryptanddecrypt ();
System.out.println ("Enter encryption password to encrypt the text" + soucestring);
Scanner Scanner = new Scanner (system.in);
String password = scanner.nextline ();
String secret = Person.encrypt (soucestring, password);
System.out.println ("ciphertext" + secret);
SYSTEM.OUT.PRINTLN ("Input decryption password");
Password = Scanner.nextline ();
String Source = person.decrypt (soucestring, password);
System.out.println ("Clear text" + source);
}
}
Class Encryptanddecrypt {
String Encrypt (string soucestring, string password) {
Char[] p = password.tochararray ();
int n = p.length;
Char[] C = Soucestring.tochararray ();
int m = c.length;
for (int k = 0; k < m; k++) {
int Mima = c[k] + p[k% n];
C[k] = (char) Mima;
}
return new String (c);
}
String Decrypt (string soucestring, string password) {
Char[] p = password.tochararray ();
int n = p.length;
Char[] C = Soucestring.tochararray ();
int m = c.length;
for (int k = 0; k < m; k++) {
int mima = c[k]-p[k% n];
C[k] = (char) Mima;
}
return new String (c);
}
}
2. Substitution concepts for regular expressions and string classes
Original address: 76735996
Java Common Utility Classes