name: portada class: portada-slide, center, middle # Strings .footnote[Joan Puigcerver Ibáñez] --- layout: true class: regular-slide .right[.logo[]] --- # String - Un String és un array no modificable de chars. - Podem accedir a un char en la posició - char charAt(int i); - I llargada - int length(); ``` String someString = "My String"; char someChar = someString.charAt(0); int length = someString.length(); ``` --- # String - JavaDoc - Javadoc: documentació d'una classe https://docs.oracle.com/javase/8/docs/api/java/lang/String.html --- # Altres mètode de String - int compareTo(String anotherString) - int compareToIgnoreCase(String str) - boolean endsWith(String suffix) - boolean equals(Object anObject) - boolean equalsIgnoreCase(String anotherString) - ... ??? - int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. - boolean isEmpty() Returns true if, and only if, length() is 0. - boolean startsWith(String prefix) Tests if this string starts with the specified prefix. - String substring(int beginIndex) Returns a string that is a substring of this string. - String substring(int beginIndex, int endIndex) Returns a string that is a substring of this string. - String trim() Returns a string whose value is this string, with any leading and trailing whitespace removed. --- # String format - static String format(String format, Object... args) - Returns a formatted string using the specified format string and arguments. - Tenim l'equivalent per printar per pantalla - System.out.printf ``` // Joe has 35 years String output = String.format("%s has %d years", "Joe", 35); System.out.printf("%s has %d years", "Joe", 35); ``` --- # String format ### Variables
%c
- character
%d
- integer
%f
- floating point decimal number
%s
- any type String value
### Especial
%n
- new line
--- # String format - print integers ``` // Default formatting: String.format("%d", 93); // prints 93 // Specifying a width: String.format("|%20d|", 93); // prints: | 93| // Left-justifying within the specified width: String.format("|%-20d|", 93); // prints: |93 | // Pad with zeros: String.format("|%020d|", 93); // prints: |00000000000000000093| ``` --- # String format - print double ``` System.out.printf("rounded double: %.3f", 7.548746484); // rounded double: 7.549 ``` --- # String format - https://dzone.com/articles/java-string-format-examples - https://docs.oracle.com/javase/tutorial/java/data/numberformat.html