Java - String

Java Conceptuel Diagram

About

Text - String in Java.

Class: javase/9/docs/api/java/lang/String.html

Management

Initialization

From a byte array

from a Java - Byte

new String(myByteArray, "UTF-8");

From a array of characters

Arrays.toString("Hello, World!".toCharArray())

From a collection of character

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(charactersList);
return stringBuilder.toString();

Construction of large String

  • A StringWriter is A character stream that collects its output in a string buffer, which can then be used to construct a string.
  • StringBuilder

Join

  • The string join method on array
String str = String.join(",", arr);
  • or with a stream and the Collectors.joining function
// Example a list of column names join with a comma delimiter 
String columnStatement = IntStream.range(0,source.getDataDef().getColumnDefs().size())
                    .mapToObj(i->source.getDataDef().getColumnDef(i).getColumnName())
                    .collect(Collectors.joining(", "));

Multiplication

  • 10 character A
final int generation = 10;
String multi = new String(new char[generation]).replace("\0", "A");
System.out.println(multi);
AAAAAAAAAA

File

With Java - (Nio|New I/O) Package

// Write
Files.write(path, query.getBytes());
// Read
new String(Files.readAllBytes(path));

toCharacter

  • Stream: Example when calculating the number of digits
// THe trick is that s.chars() returns characters as int
Math.toIntExact(s.chars()
    .filter(i->Character.isDigit((char) i)))
    .count())
  • For
for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        
    //Process char
}

toUpperCase

import static java.util.Locale.ENGLISH;
tableName = tableName.toUpperCase(ENGLISH);

Replace with regexp pattern

Pattern pattern = Pattern.compile("pattern");
String replaced = pattern.matcher(text).replaceFirst("");





Discover More
Java Conceptuel Diagram
Java - Byte

in Java from 0000 00002551111 1111 Byte array to or
Java Conceptuel Diagram
Java - IO - Character Stream

Character streams are build above byte stream. They decodes bytes into characters (or the other way around) using a specified charset. The Java platform stores character values using Unicode conventions....
Java Conceptuel Diagram
Java - Variable Memory Size

memory size of a variable. To get the memory size of a variable, you can do it: statically with the data type declaration dynamically with the Java runtime. See A single alphanumeric character...
Java Conceptuel Diagram
Java strings - null terminated

Java strings are not terminated with a null characters as in C or C++. However, when working with Jdbc, you can get Java String that are Null terminated. Null-terminated_string



Share this page:
Follow us:
Task Runner