Coding Practice--java-string-api-Practice __ajax

Source: Internet
Author: User
Tags first string stringbuffer

Welcome to visit the blog initialization (constructors) various initialization methods

String str1 = new string ();
System.out.println (STR1);

Char[] arr = {' A ', ' B ', ' C '};
String str2 = new string (arr);
System.out.println (STR2);

String STR3 = "Zhang";
System.out.println (STR3);

String STR4 = new String ("Xuezhi");
System.out.println (STR4);
String Method Str.charat ()Returns the character at the specified position
String str1 = new String ("Hello, world!");

System.out.println (Str1.charat (4));
Str.codepointat ()Returns the Unicode encoding of the specified position character
public static void Main (string[] args) {
String str1 = new String ("Hello, world!");

System.out.println (Str1.codepointat (4)); o
System.out.println (Str1.codepointat (8));//o
Str.codepointbefore ()Returns the Unicode encoding of the character at the previous position in the specified position
String str1 = new String ("Hello, world!");

System.out.println (Str1.codepointbefore (3)); L
System.out.println (Str1.codepointbefore (4));//l
Str.codepointcount ()Returns the number of encodings for Unicode within a specified range
String str1 = new String ("Hello, world!");

System.out.println (Str1.codepointcount (0, 2)); He
Str.compareto ()Compares a dictionary order of two strings and returns 0 when equal
String str1 = "ba";
String str2 = "ba";
String STR3 = "be";

System.out.println (Str1.compareto (str2));
System.out.println (Str1.compareto (STR3));
str.comparetoignorecase ()Does not distinguish size form, compares the dictionary order of two strings, returns 0 when equal
String str1 = "ba";
String str2 = "be";

System.out.println (Str1.compareto (str2));
Case-insensitive
System.out.println (Str1.comparetoignorecase (str2));
Str.concat ()string concatenation
String str1 = "Hello";
String str2 = "world!";

String STR3 = Str1.concat (STR2); Connect
String STR4 = str1 + str2 via concat ();  Through + Connect
System.out.println (STR3);
System.out.println (STR4);
str.contains ()To determine if a string contains another string
String str1 = "Hello";
String str2 = "ll";
String STR3 = "AB";

System.out.println (Str1.contains (str2));
System.out.println (Str1.contains (STR3));
str.contentequals ()To determine whether two strings are equal
String str1 = "Hello";
String str2 = "ll";
StringBuffer STR3 = new StringBuffer ("Hello");

System.out.println (Str1.contentequals (str2));   String
System.out.println (str1.contentequals (STR3));   StringBuffer
str.copyvalueof ()Converts a character array to a string
Char[] arr = {' h ', ' e ', ' l ', ' l ', ' o '};
String str1 = string.copyvalueof (arr);
String str2 = string.copyvalueof (arr, 2, 2);

System.out.println (STR1);
System.out.println (STR2);
Str.endswith ()To determine whether to end with a substring
String str1 = "Hello, world";

System.out.println (Str1.endswith ("World"));
str.equals () & Str.equalsignorecase ()Sentenced etc
String str1 = "Hello, world";
String str2 = "Hello, world";

System.out.println (Str1.equals (str2));
System.out.println (Str1.equalsignorecase (str2));
Str.format ()String formatting
String str1 = String.Format ("Hi,%s!", "Xuezhi");
String str2 = String.Format ("Price:%d", 4);
String STR3 = String.Format ("Price:%.2f yuan", 4.5);

System.out.println (STR1);
System.out.println (STR2);
System.out.println (STR3);
str.getbytes ()Encodes a string into a byte array
String str1 = "abcdef";
byte[] BArr = Str1.getbytes ();
Print for
(byte Item:barr) 
    System.out.print (item + "");
Str.hashcode ()Returns its hash code
String str1 = "abcdef";

System.out.println (Str1.hashcode ());
Str.indexof ()Returns where the character first appears
String str1 = "ABCABC";

The position of System.out.println ("a") is: "+ str1.indexof (" a "));
System.out.println ("A's position is:" + str1.indexof (' a '));

The position of the System.out.println ("a") is: "+ str1.indexof (" a ", 2));
Str.lastindexof ()Returns where the character last appears
String str1 = "ABCABC";

The last position of the System.out.println ("a") is: "+ str1.lastindexof (" a "));
The last position of the System.out.println ("a") is: "+ str1.lastindexof (' a ')");

The last position of the System.out.println ("a") is: "+ str1.lastindexof (" a ", 2));
Str.intern ()。 Intern () returns equal value only if two strings are equal
String str1 = "ABCABC";

String str2 = Str1.intern ();

System.out.println (STR2);
Str.isempty ()Determines whether a string is empty
String str1 = "abc";
String str2 = "";

System.out.println (Str1.isempty ());
System.out.println (Str2.isempty ());
str.length ()Returns the length of a string
String str1 = "abc";
String str2 = "";

System.out.println (Str1.length ());
System.out.println (Str2.length ());
str.matches ()Whether to match the given regular expression
String str1 = "Hello, world";

System.out.println (Str1.matches ("(. *) World (. *)"));
str.replace ()Character substitution or string substitution
String str1 = "Hello, world";
String str2 = str1.replace (' h ', ' H '); Char
string str3 = Str1.replace ("World", "World");//String

System.out.println (str2);
System.out.println (STR3);
Str.replaceall ()Replace all the regular expressions
String str1 = "Hello, world";
String str2 = Str1.replaceall ("L", "L"); Char

System.out.println (STR2);
Str.replacefirst ()Replace the first string
String str1 = "Hello, world";
String str2 = Str1.replacefirst ("L", "L"); 

System.out.println (STR2);
Str.split ()String segmentation
String str1 = "ABCABCABCABC";

string[] Strarr = Str1.split ("a");
System.out.println (strarr.length);
for (String Item:strarr) 
    System.out.println (item);

string[] strArr2 = Str1.split ("A", 3);
System.out.println (strarr2.length);

for (String item:strarr2) 
    System.out.println (item);
Str.startswith ()Determines whether a string starts with a substring
String str1 = "Hello, world!";

System.out.println (Str1.startswith ("Hello"));
System.out.println (Str1.startswith ("World", 7));/offset
str.subsequence ()Returns the sequence of characters for a specified interval
String str1 = "Hello, world!";

System.out.println (Str1.subsequence (7, 12));
str.substring ()Returns the string for the specified interval
String str1 = "Hello, world!";

System.out.println (str1.substring (7));
System.out.println (str1.substring (7));
Str.tochararray ()Converts a string into a character array
String str1 = "Hello";

char[] Chararr = Str1.tochararray ();

for (char Item:chararr) 
    System.out.print (item + "");
str.tolowercase ()Convert to lowercase form
String str1 = "abc DEF";

System.out.println (Str1.tolowercase ());
str.touppercase ()Convert to Capital form
String str1 = "abc DEF";

System.out.println (Str1.touppercase ());
Str.trim ()Remove whitespace from the beginning and end of a string
String str1 = "  abc def  ";

System.out.println (Str1.trim ());
str.valueof ()Convert various types of data into strings
Char[] Chararr = {' A ', ' B ', ' C '};

System.out.println (String.valueof (false));
System.out.println (string.valueof (' a '));
System.out.println (string.valueof (Chararr));
SYSTEM.OUT.PRINTLN (string.valueof (4.5));
System.out.println (String.valueof (4));

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.