Home > 类库下载 > java类库 > Strings in Java

Strings in Java

高洛峰
Release: 2016-10-29 11:52:17
Original
1743 people have browsed it

In Java language, strings are treated as objects, and the class String can be used to represent strings (the first letter of the class name is all capitalized).

1. String constant

A string constant is a string of characters enclosed in double quotes.

For example: "Hello World"

2. String represents a string variable

String is used to create a string object. String usage example:

1 String s=new String(); //Generate an empty string

2 String s1="Hello World"; // Declare s1 as a reference to the string "Hello World"

3. String.equals() method for judging equality of strings

There are particularities in judging equality in Java. Often, the answer obtained by directly using == may be What is correct may also be wrong, look at this example:

String s1="a";
 String s2="a";
 s1+="b";
 System.out.println(s1=="ab");         // false
 System.out.println(s1==s2+"b");     // false
 System.out.println(s2=="a");           // true
 System.out.println(s1.equals("ab")); // true
 System.put.println(new String("ab")==new String("ab"));  // false
Copy after login

After reading this code, you may understand. == judgment not only judges whether the contents in the memory address are equal, but also judges whether the referenced addresses are equal; and the equals() method is used to judge whether the contents are equal. Do you understand now?

There are also the following points to note:


In Java, only one copy of a string constant ("a") with the same content is saved to save memory, so s1 and s2 actually refer to the same an object.

When the compiler compiles the sentence s1, it will remove the "+" sign and directly connect the two strings to get one string ("ab"). This optimization work is done automatically by the Java compiler.

When you create a string object directly using the new keyword, although the values ​​are the same (both are "ab"), they are still two independent objects.

4. Access strings

The String class provides methods such as length(), charAt(), indexOf(), lastIndexOf(), getChars(), getBytes(), toCharArray(), etc.

public int length() This method returns the number of characters in the string

public char charAt(int index) This method returns the character at the index position in the string, where the index value range is 0~length-1

public int indexOf(int ch)

public lastIndexOf(in ch)

Returns the first and last position of the character ch in the string

public int indexOf(String str)

public int lastIndexOf(String str)

Returns the first and last position of the first character in the substring str that appears in the string

public int indexOf(int ch,int fromIndex)

public lastIndexOf(in ch,int fromIndex)

Return the first and last position of the character ch in the string after the position fromIndex

public int indexOf(String str,int fromIndex)

public int lastIndexOf(String str,int fromIndex)

Return sub The first and last positions of the first character in the string str that appear after the position fromIndex in the string.

public void getchars(int srcbegin,int end,char buf[],int dstbegin)

srcbegin is the position of the first character to be extracted in the source string, end is the last character to be extracted in the source string position, the character array buf[] stores the destination string, and dstbegin is the starting position of the extracted string in the destination string.

public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)

The parameters and usage are the same as above, except that the characters in the string are represented by 8 bits.

5. Modify the string

The purpose of modifying the string is to get a new string. For the use of each method, please refer to the java API.

Methods provided by the String class:


concat( )

replace( )

substring( )

toLowerCase( )

toUpperCase( )

public String contat(String str);

Used Concatenates the current string object with the given string str.

public String replace(char oldChar,char newChar);

Used to replace all specific characters appearing in the string with specified characters to generate a new string.

public String substring(int beginIndex);

public String substring(int beginIndex,int endIndex);

Used to get the substring within the specified range in the string.

public String toLowerCase();

Convert all characters in the string to lowercase.

public String toUpperCase();

Convert all characters in the string to uppercase.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template