The basic usage of the string class and the point of attention, the use of the StringBuffer class

Source: Internet
Author: User

Package cn.hncu.day8;

public class Regexpdemo {
public static void Main (string[] args) {
String str = "011-12345678";
Boolean boo = Str.matches ("0\\d{2}-\\d{8}");
System.out.println (boo);//true
str = "110-12345678";
Boo = Str.matches ("0\\d{2}-\\d{8}");
System.out.println (boo);//false

str = "Dsõådsjksdj ÁSAA";
Boo = Str.matches (". *õå.* á.*");//õådsjksdj á
System.out.println (boo);//false


}
}

The place to send a match on the string, you can go to Baidu search for example:

\w match numbers, letters, underscores

\b With opening or ending

\d Digital

^ Match start

$ match End

{n} repeats n times

/content/Compose a case-insensitive regular expression

----------------------------------------------------------

Package cn.hncu.day8;

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

public class RegExpDemo2 {
public static void Main (string[] args) {
String str = "011-12345678";
Pattern p = pattern.compile ("0\\d{2}-\\d{8}"),//p--can be understood as a regular expression tool
Matcher m = p.matcher (str);//m--can be interpreted as a package of matching results
Boolean boo = M.matches ();//Read match result information
System.out.println (Boo);
}
}

--------------------------------------------------------------------------

Package cn.hncu.day6;

Import java.io.UnsupportedEncodingException;

public class Stringdemo {

public static void Main (string[] args) {
System.out.println ("========1 demo format () method =========");
String str;
for (int i=1;i<10;i++) {
str = String.Format ("hncujava-%3d", I);//This is equivalent to the printf ("%3s") inside C;
System.out.println (str);
}
System.out.println ("========2=========");
2, output string s = "1234567" Length, 3rd character, number of bytes
String s= "1234567";
System.out.println ("Length:" + s.length ()); 7
System.out.println ("3rd character:" + S.charat (2));//3
System.out.println ("Number of bytes:" + s.getbytes (). length);//7

System.out.println ("========3=========");
3, Output s= "1234567" length, the 3rd character, the number of bytes
S= "1234567";
System.out.println ("Length:" + s.length ()); 8
System.out.println ("3rd character:" + S.charat (2));//3
System.out.println ("Number of bytes:" + s.getbytes (). length);//gbk=9 utf-8=10

System.out.println ("========4=========");
4, determine whether a paragraph of characters contain Chinese characters (assuming only English characters)
s = "JJ kks me w you ew";
if (S.getbytes (). Length==s.length ()) {
System.out.println (s+ "---no Chinese characters");
}else{
System.out.println (s+ "---has Chinese characters, the number is:" + (S.getbytes (). Length-s.length ()));
}

System.out.println ("========5=========");
5, the output of a string of Chinese characters, English letters, numbers
s = "JJ kk3d me s0w4w";
Char cs[] = S.tochararray ();
for (char C:cs) {
if (("" +c). GetBytes (). Length > ("" +c). Length ()) {
System.out.println ("Kanji:" +c);
}else if (c>= ' a ' &&c<= ' z ' | | c>= ' a ' &&c<= ' Z ') {
System.out.println ("English alphabet:" +c);
}else if (c>= ' 0 ' &&c<= ' 9 ') {
System.out.println ("Number:" +c);
}
}

System.out.println ("========6=========");
6 Demonstrating coding issues
S= "1234567";
try {
To get an array of bytes in a string by specifying encoding
System.out.println (S.getbytes ("UTF-8"). length);
byte bytes[] = "China wqqew Me". GetBytes ("UTF-8");
String ss1 = new string (bytes);//Use the Platform Default Code table, if the default is GBK is garbled, if the default is Utf-8 OK
System.out.println (SS1);
String ss2 = new String (bytes, "GBK");//specified code table, garbled----because the decoding table and the encoding table are different
System.out.println (SS2);
String ss3 = new String (bytes, "utf-8");//Specify the code table, OK----because the decoding table and the encoding table are consistent
System.out.println (SS3);

} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}

}

}

---------------------------------------------------------------------------------

Package cn.hncu.day6;

Import java.io.UnsupportedEncodingException;

public class StringDemo2 {

public static void Main (string[] args) {
System.out.println ("========1=========");
1. String comparison size
String str1 = "hw in 1234567";//48--' 0 ' 51--' 3 '
String str2 = "12hello";//97--' a ' 104--' h '
System.out.println (STR1&GT;STR2);//Wrong, only basic data type to use ">"
System.out.println (Str1.compareto (STR2));//subtract each character of two strings in turn, as long as one character is not equal, the result of these two words typeface minus as the return value of the whole function---less than 0 means str1 is smaller, Greater than 0 means str1 is larger, equals 0 str1 is equal to STR2

System.out.println ("========2=========");
2, sub-string location search
String s= "he";
System.out.println (Str1.indexof (s));//-1, representing no substring s in str1
System.out.println (Str2.indexof (s));//2, representing the position of the substring s in str2 starting at the 2nd character

System.out.println ("========3=========");
3. Find the person with the word "Fei" in the name
String[] names={"Zhang San", "John Doe", "Zhang Fei", "Zhang Sanfei", "Li Shifei", "Fei Fei Zhang", "Ye Feiyan", "Xiao Zhang"};
String s1= "Fly";
for (String name:names) {
if (Name.indexof (S1) >=0) {
System.out.print (name+ "");
}
}
System.out.println ();

Find the person with the surname "Zhang"
Method 1
String s2= "Zhang";
for (String name:names) {
if (Name.indexof (s2) ==0) {
System.out.print (name+ "");
}
}
System.out.println ();
Method 2
for (String name:names) {
if (Name.startswith (S2)) {
System.out.print (name+ "");
}
}
System.out.println ();

System.out.println ("========4=========");
4. Look for the file name with the extension ". Java".
String[] filenames={"Aa.java", "a.java.c", "Ba.c.java", "We.txt", "Java.js", "1.java"};
String s3= ". Java";
for (String filename:filenames) {
if (Filename.endswith (S3)) {
System.out.print (filename+ "");
}
}
System.out.println ();

System.out.println ("========5=========");
5 Practice substring
String str3= "Welcome you!";
System.out.println (Str3.substring (3));//come you!
System.out.println (str3.substring (2,4)); The//LC---specification: the one that takes an interval range is: Left contains right does not contain

System.out.println ("========6=========");
Equals and Equalsignorecase ()
String str4= "Welcome you!";
System.out.println (Str3.equals (STR4));//false
System.out.println (Str3.equalsignorecase (STR4));//true

System.out.println ("========7=========");
※ Note ※----The method of handling the string variable, the result is returned with a new string (so we have to manually go to the return result), the original string variable is unchanged!---the idea with the string variable we talked about as the parameter of the method, Usage and basic data types are the same as value passing---thought is consistent!
String ss1= str4.touppercase ();
System.out.println (STR4)//welcome you!--old
System.out.println (SS1);//The result we want
String SS2 = str4.replace (' o ', ' 6 ');
System.out.println (STR4)//welcome you!--old
System.out.println (SS2);//The result we want

System.out.println ("========8=========");
8. Packaging and dismantling of user information
String userinfo= "hncu,1234,13612345678";
String strs[] = Userinfo.split (",");
User user = new user ();
User.setname (Strs[0]);
User.setpwd (Strs[1]);
User.settel (strs[2]);
SYSTEM.OUT.PRINTLN (user);

String STR5 = "aa,bb,cc,a1 b2 dd";
String strs2[] = Str5.split (", | +");
for (String str:strs2) {
System.out.println (str);
}

System.out.println ("----------------");
String STR6 = "AA|BB|CC|A1|B2.DD";
Method 1
String strs3[] = Str6.split ("\\.| \\|");/ /If a character with a special meaning in a regular expression is used as a delimiter, it must be escaped
for (String Str:strs3) {
System.out.println (str);//\\t
}

System.out.println ("--------+--------");
Method 2
String STR7 = str6.replace ("|", ",");
STR7 = Str7.replace (".", ",");
String strs4[] = Str7.split (",");
for (String str:strs4) {
System.out.println (str);//\\t
}

System.out.println ("========8=========");
9, user Data background check before the filter
String str9= "Dsjsdjksd dskkds";
String SS9 = Str9.trim ();
System.out.println (STR9);//Old
System.out.println (SS9);//The result we want

}

}

Class user{
private String name;
Private String pwd;
Private String Tel;
Public User (string name, string pwd, string tel) {
Super ();
THIS.name = name;
This.pwd = pwd;
This.tel = Tel;
}

Public User () {
Super ();
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String getpwd () {
return pwd;
}

public void SetPwd (String pwd) {
This.pwd = pwd;
}

Public String Gettel () {
return Tel;
}

public void Settel (String tel) {
This.tel = Tel;
}

@Override
Public String toString () {
Return "User [name=" + name + ", pwd=" + pwd + ", tel=" + Tel + "]";
}
}

-----------------------------------------------------------------------------------------

Package cn.hncu.day8;

public class StringBufferDemo2 {

public static void Main (string[] args) {
String str = new String ("Java");
Strreplace (str);
System.out.println (str);//Unchanged

StringBuffer sb = new StringBuffer ("Java");
Sbreplace (SB);
System.out.println (Sb.tostring ());//change
}

private static void Sbreplace (StringBuffer sb) {//Parameter usage same as: reference type---reference pass
Sb.append ("EE");
}

private static void Strreplace (String str) {//Parameter usage same as: basic data---value pass
str = Str.replaceall ("J", "I");
}

}stringbuffer is multithreaded security, and StringBuilder is not thread-safe, StringBuffer class is slightly faster;

The basic usage of the string class and the point of attention, the use of the StringBuffer class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.