Important Java17 String

Source: Internet
Author: User
Tags locale

Strings are widely used in Java programming, where strings belong to objects, and Java provides a string class to create and manipulate strings.

Create a string

The simplest way to create a string is for example the following:

String greeting = "Hello world!";

When you encounter a string constant in your code, the value here is "Hello world!", which the compiler uses to create a string object.

Like other objects, you can use keyword and construction methods to create a string object.

The string class has 11 constructor methods that provide different parameters for initializing a string, such as providing a character array parameter:

public class StringDemo{    public static void main(String args[]){        char[] helloArray = { ‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘.‘};        String helloString = new String(helloArray);          System.out.println( helloString );    }}

The above example compiles execution results such as the following:

hello.

Note: The string class is a final class that cannot be changed , so once you create a string object, its value cannot be changed. If you need to make a lot of changes to a string, you should choose to use the StringBuffer & StringBuilder class.

Frequent usage
  • char charAt (int index) returns the char value at the specified index.
  • static string copyvalueof (char[] data) returns a string representing the sequence of characters in the specified array.
  • Boolean endsWith (string suffix) tests whether this string ends with the specified suffix.
  • Boolean Equals (object AnObject) to compare this string to the specified object.
  • The Boolean equalsignorecase (string anotherstring) Compare this string to another string, regardless of uppercase or lowercase.
  • Byte[] GetBytes () encodes this String into a byte sequence using the platform's default character set and stores the result in a new byte array.
  • Byte[] GetBytes (string charsetname) encodes this String into a byte sequence using the specified character set and stores the result in a new byte array.
  • int indexOf (int ch) returns the index of the specified character at the first occurrence of this string.
  • int indexOf (int ch, int fromIndex) returns the index at the first occurrence of the specified character in this string, starting with the specified index.
  • int indexOf (String str) returns the index where the specified substring first appears in this string.
  • int lastIndexOf (int ch) returns the index of the specified character at the last occurrence in this string.
  • int length () returns the length of this string.
  • A Boolean matches (string regex) tells whether this string matches a given regular table.
  • String replace (char OldChar, char Newchar) returns a new string that is obtained by replacing all OldChar that appear in this string with Newchar.
  • String ReplaceAll (string regex, string replacement replaces this string with the given replacement to match all substrings of the given regular table.
  • String[] Split (string regex) splits this string based on a match of the given regular table.
  • String[] Split (string regex, int limit) splits this string by matching a given regular table.
  • Boolean startsWith (string prefix) tests whether this string starts with the specified prefix.
  • string substring (int beginindex) returns a new string that is a substring of this string.
  • string substring (int beginindex, int endIndex) returns a new string that is a substring of this string.
  • String toLowerCase () converts all characters in this string to lowercase using the rules of the default locale.
  • String toUpperCase () converts all characters in this string to uppercase using the default locale's rules.
  • String Trim () returns a copy of the string, ignoring leading and trailing blanks.
  • The static string format (String format,object ... args) returns a formatted string using the specified format string and parameters.
Common topics (using JUnit demos)
@Testpublic void Testequels () {String test = "12345";    String test1 = "12345";    String test2 = new String ("12345");    String test3 = new String ("12345");    String t1 = test;    String t2 = "12" + "345";    Asserttrue (TEST==TEST1);    Asserttrue (TEST==T1);    Asserttrue (TEST==T2);    Asserttrue (TEST!=TEST2);       Asserttrue (TEST2!=TEST3);            } @Testpublic void Testnotchange () {String instring = "123";    String outstring = stubchangestring (instring);    Assertequals ("123", instring); Assertequals ("123test", outstring);}    private string Stubchangestring (String instring) {instring + = "Test"; return instring;}    @Testpublic void Testsubstring () {String test = "12345"; Assertequals ("2345", test.substring (1));}    @Testpublic void Testtoarrays () {list<string> ids = new arraylist<> ();    Ids.add ("1");    Ids.add ("2");    Ids.add ("3");    string[] id = ids.toarray (new string[0]);    Assertequals (3, id.length); Assertequals ("1", id[0]);}

You can also interview :http://txidol.github.io get a lot of other information


Important Java17 String

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.