String objects are immutable: their character contents stay unchanged after creation. Methods such as trim return a String value, so keep that result or return it directly. Calling a transformation and then returning the original variable leaves the old text in place. Use equals for character-content equality. The == operator compares references and can give misleading results when two separate String objects contain the same text.
Character positions start at zero. substring uses an inclusive start and an exclusive end, making a prefix of length n easy to express as substring(0, n). The examples use ASCII labels so their character counts are unambiguous. For text assembled in a loop, StringBuilder holds a mutable buffer. Append separators between entries, then call toString once for the finished result.