import java.nio.charset.Charset;
import java.util.Locale;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target="_blank">Manipolazione di una stringa</a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
/**
* @see String
* @see StringBuilder
* @see StringBuffer
*
* {@link Charset#forName(String)}
*
*/
public static void toInitializeString() {
// thread-safe
Charset charset = Charset.forName("UTF-16");
byte[] unicode = new byte[] { 73, 84, 65, 76, 79 };
// imposta un encoder valido per la JVM
String text = new String(unicode, charset);
System.out.println("default charset: " + charset + "\r\ntesto: " + text);
char[] arrCh = new char[] { 'I', 'T', 'A', 'L', 'O' };
// usa l'encoder di default della JVM
// javaw.exe -Dfile.encoding=UTF-8 -classpath
// "C:\Galileo\Eclipse-Workspace\Galileo_v1_3\bin"
// -XX:+ShowCodeDetailsInExceptionMessages
// org.buscati.knowledge.example.Example_006_05_No_Primitive_Strimg_Methods
text = new String(arrCh);
System.out.println(text);
// non thread-safe
StringBuilder text_ = new StringBuilder("ITALO");
System.out.println(text_);
// thread-safe
StringBuffer text__ = new StringBuffer("ITALO");
System.out.println(text__);
}
/**
* @see String#charAt(int)
* @see String#length()
* @see String#substring(int)
* @see String#substring(int,int)
* @see String#concat(String)
* @see String#indexOf(int)
* @see String#indexOf(int, int)
* @see String#compareTo(String)
* @see String#toUpperCase(Locale)
* @see String#trim()
* @see String#replace(CharSequence target, CharSequence replacement)
*
* {@link Locale#ITALIAN}
* {@link Locale#ITALY}
*
*/
public static void applayMethods() {
String text = new String("ITALO");
// carattere al n-esima posizione
for (int i = 0; i < text.length(); i++)
System.out.println(i + " -> " + text.charAt(i));
// restituisce se stessa
System.out.println(text.toString());
// lunghezza
System.out.println(text.length());
// sotto-stringa
System.out.println(text.substring(1));
// sotto-stringa da 0 (incluso) a length+1 (escluso)
System.out.println(text.substring(0, 5));
// concatenamento di due stringhe
String alias = "ITAitp";
String fullDescription = " - Italia Italo Persechino";
System.out.println(alias.concat(fullDescription));
System.out.println(alias + fullDescription);
// indice da 0 (incluso)
fullDescription = "Italia Italo Persechino";
System.out.println("Indice " + fullDescription.indexOf("P"));
System.out.println("Indice " + fullDescription.indexOf('I', 0));
System.out.println("Indice " + fullDescription.indexOf('I', 1));
// se non esiste la posizione restituisce ultima possibile
System.out.println("Indice " + fullDescription.indexOf('I', 3));
// verifica che il contenuto di una stringa sia uguale ad un'altra
System.out.println("Italia Italo Persechino".equals(fullDescription));
// compara il contenuto di due stringhe
// se uguale restituisce 0
// se minore restituisce -1..-n
// se maggiore restituisce +1..+n
System.out.println("Italia Italo Persechino".compareTo("Italia Italo Persechino"));
System.out.println("Italia Italo Persechino".compareTo("Italia Italo Persechino - ITAitp"));
System.out.println("Italia Italo Persechino".compareTo("ITAitp"));
// tutto maiuscolo
// Locale.ITALIAN (language)
// Locale.ITALY (country)
System.out.println(text.toUpperCase(Locale.ITALIAN));
// elimina spazzi vuoti a sinistra e a destra di una stringa
System.out.println(" #PI ENO# ".trim());
// sostituisci una sequenza all'interno di una stringa
System.out.println("SEDIA PORTA ".replace("PORTA", "POLTRONA"));
}
public static void main(String args[]) throws Exception {
// Unit test - costruisce una stringa a partire da una sequenza di byte e di
// caratteri
toInitializeString();
System.out.println();
// Unit test - metodi di utilità per la manipolazione di una stringa
applayMethods();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: