La stringa è una sequenza di caratteri. In Java, gli oggetti String sono immutabili, il che significa una costante e non possono essere modificati una volta creati.
Creazione di una stringa
Esistono due modi per creare una stringa in Java:
1. String letterale
String s = techcodeview.com;>
2. Utilizzo nuovo parola chiave
String s = new String (techcodeview.com);>
Costruttori di stringhe in Java
1. String(byte[] byte_arr)
Costruisci una nuova stringa decodificando il file matrice di byte . Utilizza il set di caratteri predefinito della piattaforma per la decodifica.
Esempio:
byte[] b_arr = {71, 101, 101, 107, 115}; String s_byte =new String(b_arr); //Geeks>2. String(byte[] byte_arr, Charset char_set)
Costruisci una nuova stringa decodificando il file matrice di byte . Utilizza il set_caratteri per la decodifica.
Esempio:
byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s_byte_char = new String(b_arr, cs); //Geeks>3. String(byte[] byte_arr, String char_set_name)
Costruisci una nuova stringa decodificando il file matrice di byte . Utilizza il nome_insieme_caratteri per la decodifica. Sembra simile ai costrutti precedenti e compaiono prima di funzioni simili ma richiede il file String (che contiene char_set_name) come parametro mentre il costruttore sopra prende Set di caratteri.
Esempio:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 'US-ASCII'); //Geeks>4. String(byte[] byte_arr, int start_index, int lunghezza)
Costruisci una nuova stringa da matrice di byte dipende da start_index(Posizione di partenza) E lunghezza (numero di caratteri dalla posizione iniziale).
Esempio:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 3); // eek>5. String(byte[] byte_arr, int start_index, int lunghezza, Charset char_set)
Costruisci una nuova stringa da matrice di byte dipende da start_index(Posizione di partenza) E lunghezza(numero di caratteri dalla posizione iniziale) .Usi set_caratteri per la decodifica.
Esempio:
codice c ass
byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s = new String(b_arr, 1, 3, cs); // eek>6. String(byte[] byte_arr, int start_index, int lunghezza, String char_set_name)
Costruisci una nuova stringa da matrice di byte dipende da start_index(Posizione di partenza) E lunghezza(numero di caratteri dalla posizione iniziale) .Usi nome_insieme_caratteri per la decodifica.
Esempio:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 4, 'US-ASCII'); // eeks>7. String(char[]char_arr)
Assegna una nuova stringa dal dato Matrice di caratteri
Esempio:
valore Java dell'enum
char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr); //Geeks>8. String(char[] char_array, int start_index, int count)
Assegna una stringa da un dato matrice di caratteri ma scegli contare personaggi del indice_iniziale .
Esempio:
char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr , 1, 3); //eek>9. String(int[] uni_code_points, int offset, int count)
Assegna una stringa da a uni_code_array ma scegli contare personaggi del indice_iniziale .
Esempio:
int[] uni_code = {71, 101, 101, 107, 115}; String s = new String(uni_code, 1, 3); //eek>10. String(StringBuffer s_buffer)
Assegna una nuova stringa dalla stringa in s_buffer
Esempio:
StringBuffer s_buffer = new StringBuffer('Geeks'); String s = new String(s_buffer); //Geeks>11. String(StringBuilder s_builder)
Assegna una nuova stringa dalla stringa in s_builder
Esempio:
StringBuilder s_builder = new StringBuilder('Geeks'); String s = new String(s_builder); //Geeks>
Metodi di stringa in Java
1. lunghezza intera()
Restituisce il numero di caratteri nella stringa.
'techcodeview.com'.length(); // returns 13>
2. Char charAt(int i)
Restituisce il carattere in ithindice.
'techcodeview.com'.charAt(3); // returns ‘k’>
3. Sottostringa di stringa (int i)
Restituisce la sottostringa da ithcarattere indice per terminare.
'techcodeview.com'.substring(3); // returns ksforGeeks>
4. Sottostringa di stringa (int i, int j)
Restituisce la sottostringa dall'indice i all'indice j-1.
'techcodeview.com'.substring(2, 5); // returns eks>
5. String concat( String str)
Concatena la stringa specificata alla fine di questa stringa.
String s1 = Geeks; String s2 = forGeeks; String output = s1.concat(s2); // returns techcodeview.com>
6. int indiceOf (String s)
Restituisce l'indice all'interno della stringa della prima occorrenza della stringa specificata.
Se String s non è presente nella stringa di input, viene restituito -1 come valore predefinito.
1. String s = Learn Share Learn; int output = s.indexOf(Share); // returns 6 2. String s = 'Learn Share Learn' int output = s.indexOf(Play); // return -1>
7. int indiceOf (String s, int i)
Restituisce l'indice all'interno della stringa della prima occorrenza della stringa specificata, a partire dall'indice specificato.
String s = Learn Share Learn; int output = s.indexOf('ea',3);// returns 13>8. Int ultimoIndiceOf( String s)
Restituisce l'indice all'interno della stringa dell'ultima occorrenza della stringa specificata.
Se String s non è presente nella stringa di input, viene restituito -1 come valore predefinito.
1. String s = Learn Share Learn; int output = s.lastIndexOf('a'); // returns 14 2. String s = 'Learn Share Learn' int output = s.indexOf(Play); // return -1>9. booleano è uguale a( Oggetto otherObj)
Confronta questa stringa con l'oggetto specificato.
funzione anonima Java
Boolean out = Geeks.equals(Geeks); // returns true Boolean out = Geeks.equals(geeks); // returns false>
10. booleano equalsIgnoreCase (String anotherString)
Confronta una stringa con un'altra stringa, ignorando le considerazioni sui casi.
Boolean out= Geeks.equalsIgnoreCase(Geeks); // returns true Boolean out = Geeks.equalsIgnoreCase(geeks); // returns true>
11. int compareTo( String anotherString)
Confronta lessicograficamente due stringhe.
int out = s1.compareTo(s2); // where s1 and s2 are // strings to be compared This returns difference s1-s2. If : out <0 // s1 comes before s2 out = 0 // s1 and s2 are equal. out>0 // s1 viene dopo s2.>
12. int compareToIgnoreCase( String anotherString)
Confronta due stringhe lessicograficamente, ignorando le considerazioni sui casi.
int out = s1.compareToIgnoreCase(s2); // where s1 and s2 are // strings to be compared This returns difference s1-s2. If : out <0 // s1 comes before s2 out = 0 // s1 and s2 are equal. out>0 // s1 viene dopo s2.>
Nota: In questo caso, non considererà maiuscole e minuscole di una lettera (ignorerà se è maiuscola o minuscola).
13. Stringa inLowerCase()
Converte tutti i caratteri nella stringa in minuscolo.
String word1 = HeLLo; String word3 = word1.toLowerCase(); // returns hello'>
14. Stringa inUpperCase()
Converte tutti i caratteri nella stringa in maiuscolo.
String word1 = HeLLo; String word2 = word1.toUpperCase(); // returns HELLO>
quindici. Taglio delle corde()
Restituisce la copia della String, rimuovendo gli spazi bianchi su entrambe le estremità. Non influisce sugli spazi bianchi al centro.
String word1 = Learn Share Learn ; String word2 = word1.trim(); // returns Learn Share Learn>
16. Sostituzione stringa (char oldChar, char newChar)
Restituisce una nuova stringa sostituendo tutte le occorrenze di vecchioChar con nuovoChar.
String s1 = feeksforfeeks; String s2 = feeksforfeeks.replace(‘f’ ,’g’); // return geeksforgeeks>
Nota: s1 è ancora feeksforfeeks e s2 è geeksgorgeeks
17. booleano contiene(stringa):
Restituisce vero se la stringa contiene contiene la stringa specificata
String s1='geeksforgeeks'; String s2='geeks'; s1.contains(s2) // return true>
18. Char[] aCharArray():
Converte questa stringa in una nuova matrice di caratteri.
oggetto a jsonobject java
String s1='geeksforgeeks'; char []ch=s1.toCharArray(); // returns [ 'g', 'e' , 'e' , 'k' , 's' , 'f', 'o', 'r' , 'g' , 'e' , 'e' , 'k' ,'s' ]>
19. stelle booleaneCon(stringa):
Restituisce vero se la stringa inizia con questo prefisso.
String s1='geeksforgeeks'; String s2='geeks'; s1.startsWith(s2) // return true>
Esempio di costruttore di stringhe e metodi di stringa
Di seguito è riportata l'implementazione dell'argomento sopra menzionato:
Giava // Java code to illustrate different constructors and methods // String class. import java.io.*; import java.util.*; // Driver Class class Test { // main function public static void main (String[] args) { String s= 'techcodeview.com'; // or String s= new String ('techcodeview.com'); // Returns the number of characters in the String. System.out.println('String length = ' + s.length()); // Returns the character at ith index. System.out.println('Character at 3rd position = ' + s.charAt(3)); // Return the substring from the ith index character // to end of string System.out.println('Substring ' + s.substring(3)); // Returns the substring from i to j-1 index. System.out.println('Substring = ' + s.substring(2,5)); // Concatenates string2 to the end of string1. String s1 = 'Geeks'; String s2 = 'forGeeks'; System.out.println('Concatenated string = ' + s1.concat(s2)); // Returns the index within the string // of the first occurrence of the specified string. String s4 = 'Learn Share Learn'; System.out.println('Index of Share ' + s4.indexOf('Share')); // Returns the index within the string of the // first occurrence of the specified string, // starting at the specified index. System.out.println('Index of a = ' + s4.indexOf('a',3)); // Checking equality of Strings Boolean out = 'Geeks'.equals('geeks'); System.out.println('Checking Equality ' + out); out = 'Geeks'.equals('Geeks'); System.out.println('Checking Equality ' + out); out = 'Geeks'.equalsIgnoreCase('gEeks '); System.out.println('Checking Equality ' + out); //If ASCII difference is zero then the two strings are similar int out1 = s1.compareTo(s2); System.out.println('the difference between ASCII value is='+out1); // Converting cases String word1 = 'GeeKyMe'; System.out.println('Changing to lower Case ' + word1.toLowerCase()); // Converting cases String word2 = 'GeekyME'; System.out.println('Changing to UPPER Case ' + word2.toUpperCase()); // Trimming the word String word4 = ' Learn Share Learn '; System.out.println('Trim the word ' + word4.trim()); // Replacing characters String str1 = 'feeksforfeeks'; System.out.println('Original String ' + str1); String str2 = 'feeksforfeeks'.replace('f' ,'g') ; System.out.println('Replaced f with g ->'+str2); } }> Produzione
String length = 13 Character at 3rd position = k Substring ksforGeeks Substring = eks Concatenated string = techcodeview.com Index of Share 6 Index of a = 8 Checking Equality false Checking Equality ...>
Per il Set – 2 puoi fare riferimento: Classe Java.lang.String in Java | Insieme 2
Questo articolo è stato fornito da Rahul Agrawal e i nostri utili utenti.