Jump to content

Java Tutorial/String and Character Manipulation

From Wikiversity

Strings

[edit | edit source]
  • String str = "abc"
  • String str = "abc"+"def"
  • String str = "abc".substring(2,3);
  • charAt
  • concat
  • String.format
  • indexOf
  • length
  • split
  • substring
  • String.valueOf

String builder

[edit | edit source]

In Java, Strings are normally immutable objects - making a change necessitates creating a new String, which will be inefficient if you need to manipulate large amounts of text.

The StringBuilder class is designed for such instances.

The following methods are important for the string builder:

  • append(addition): adds the value to the end of the string.
  • charAt(index)
  • delete(start, end)
  • insert(offset, addition)
  • length
  • replace(start, end, str)
  • toString