JAVA Strings

JAVA INSTALL

W3School

String được sử dụng để lưu trữ văn bản. Biến String chứa một tập hợp các ký tự được bao quanh bởi dấu ngoặc kép:

String greeting = "Hello";

String Length – độ dài chuỗi

String trong Java thực sự là một đối tượng, chứa các phương thức có thể thực hiện các thao tác nhất định trên chuỗi. Ví dụ, độ dài của một chuỗi có thể được tìm thấy bằng phương thức length():

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());

Một số hàm String khác

Có nhiều phương thức String có sẵn, ví dụ toUpperCase() và toLowerCase():

String txt = "Hello World";
System.out.println(txt.toUpperCase());   // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase());   // Outputs "hello world"

Tìm ký tự trong một chuỗi

Phương thức indexOf () trả về chỉ mục (vị trí) của lần xuất hiện đầu tiên của một văn bản được chỉ định trong một chuỗi (bao gồm cả khoảng trắng):

String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7

Java đếm các vị trí từ 0. 0 là vị trí đầu tiên trong một chuỗi, 1 là thứ hai, 2 là thứ ba ..

String Functions

Lớp String có một tập hợp các phương thức dựng sẵn mà bạn có thể sử dụng trên các chuỗi.

MethodDescriptionReturn Type
charAt()Trả về ký tự tại chỉ mục được chỉ định (vị trí)char
codePointAt()Trả về Unicode của ký tự tại chỉ mục được chỉ địnhint
codePointBefore()Trả về Unicode của ký tự trước chỉ mục được chỉ địnhint
codePointCount()Trả về số lượng giá trị Unicode được tìm thấy trong một chuỗi.int
compareTo()So sánh hai chuỗi về mặt từ vựngint
compareToIgnoreCase()So sánh hai chuỗi về mặt từ vựng, bỏ qua sự khác biệt về chữ hoa và chữ thườngint
concat()Nối một chuỗi vào cuối một chuỗi khácString
contains()Kiểm tra xem một chuỗi có chứa một chuỗi ký tự hay khôngboolean
contentEquals()Kiểm tra xem một chuỗi có chứa cùng một chuỗi ký tự chính xác của CharSequence hoặc StringBuffer được chỉ định hay khôngboolean
copyValueOf()Trả về một chuỗi đại diện cho các ký tự của mảng ký tựString
endsWith()Kiểm tra xem một chuỗi có kết thúc bằng (các) ký tự được chỉ định hay khôngboolean
equals()So sánh hai chuỗi. Trả về true nếu hai chuỗi bằng nhau, ngược lại trả về false.boolean
equalsIgnoreCase()Vẫn là so sánh hai chuỗi, tuy nhiên không quan tâm đến chữa hoa thường.boolean
format()Trả về một chuỗi được định dạng bằng các đối số đã được chỉ định sẵn kiểu trả về.

String name=”sonoo”;
float age = 19;
String sf1=String.format(“name is %1$s, age is %2$f”,name, age);
String
getBytes()Chuyển từng ký tự trong một chuỗi vào trong một byte Array
VD: “ABC” => array(65,66,67)
byte[]
getChars()Sao chép các ký tự từ một chuỗi sang một mảng ký tự
void
hashCode()Returns the hash code of a stringint
indexOf()Returns the position of the first found occurrence of specified characters in a stringint
intern()Returns the canonical representation for the string objectString
isEmpty()Checks whether a string is empty or notboolean
lastIndexOf()Returns the position of the last found occurrence of specified characters in a stringint
length()Returns the length of a specified stringint
matches()Searches a string for a match against a regular expression, and returns the matchesboolean
offsetByCodePoints()Returns the index within this String that is offset from the given index by codePointOffset code pointsint
regionMatches()Tests if two string regions are equalboolean
replace()Searches a string for a specified value, and returns a new string where the specified values are replacedString
replaceFirst()Replaces the first occurrence of a substring that matches the given regular expression with the given replacementString
replaceAll()Replaces each substring of this string that matches the given regular expression with the given replacementString
split()Splits a string into an array of substringsString[]
startsWith()Checks whether a string starts with specified charactersboolean
subSequence()Returns a new character sequence that is a subsequence of this sequenceCharSequence
substring()Returns a new string which is the substring of a specified stringString
toCharArray()Converts this string to a new character arraychar[]
toLowerCase()Converts a string to lower case lettersString
toString()Returns the value of a String objectString
toUpperCase()Converts a string to upper case lettersString
trim()Removes whitespace from both ends of a stringString
valueOf()Returns the string representation of the specified valueString

W3School

Leave a Reply

Your email address will not be published. Required fields are marked *