JAVA — CHAPTER 7

Strings, NLP and Regex · Generative AI Foundations · Cheat Sheet
String StringBuilder Character Tokenizing NLP Basics Regex
1 CHARACTERS & STRING FUNDAMENTALS

Basics

  • A char literal uses single quotes: 'A'
  • A String literal uses double quotes: "Hello"
  • Strings are immutable — once created, their contents never change
  • String literals are pooled/shared by the JVM for efficiency
String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2); // true (same pooled literal) System.out.println(s1.equals(s2)); // true (content check)
2 CLASS String — COMMON METHODS
MethodPurpose
length()number of characters
charAt(i)character at index i
substring(a,b)portion from a to b-1
indexOf(str)first position of str, or -1
concat(str)join two strings
equalsIgnoreCase()compare ignoring case
compareTo()lexicographic comparison
split(regex)break into a String[] array
String s = "Hello, Java!"; System.out.println(s.substring(7, 11)); // "Java" System.out.println(s.indexOf("Java")); // 7
3 CLASS StringBuilder — MUTABLE STRINGS
StringBuilder sb = new StringBuilder("Java"); sb.append(" rocks"); // "Java rocks" sb.insert(4, "!!!"); // "Java!!! rocks" sb.reverse();

Why use it?

Unlike String, a StringBuilder can be modified in place — much more efficient when building/editing text in a loop instead of creating a new String each time.

4 CLASS Character & TOKENIZING STRINGS

Character (static tests)

  • isDigit(c), isLetter(c)
  • isUpperCase(c), isWhitespace(c)
  • toUpperCase(c), toLowerCase(c)

Tokenizing (splitting into words)

String[] words = "Java is fun".split(" "); // ["Java", "is", "fun"]

Why it matters

Breaking text into tokens (words/sub-words) is the very first step of almost every text-processing pipeline.

5 INTRO TO NLP — ROOT OF GENERATIVE AI
Raw text
Tokenization
(split into words/tokens)
Normalization
(lowercase, remove punctuation)
Numeric vectors
(feed into a model)
Model output
(prediction / generated text)

Natural Language Processing (NLP) teaches computers to understand and generate human language. The string-handling techniques in this chapter — tokenizing, pattern matching, cleaning text — are the same foundational steps that large language models (LLMs) use before any AI-generated text is produced.

6 OBJECTS-NATURAL CASE STUDY: REGULAR EXPRESSIONS
import java.util.regex.*; Pattern p = Pattern.compile("[0-9]+"); Matcher m = p.matcher("Room 42B"); if (m.find()) { System.out.println(m.group()); // "42" }

Common metacharacters

PatternMatches
\da digit
\wa word character
+ *one-or-more / zero-or-more
^ $start / end of string
7 METHOD REFERENCE TABLE

class String

MethodPurpose
length()character count
charAt(i)char at index i
substring(a,b)portion of the string
indexOf(s)first position of s, or -1
replace(a,b)swaps all occurrences of a with b
split(regex)breaks into a String[]
trim()/strip()removes leading/trailing whitespace
compareTo(s)lexicographic order

class StringBuilder

MethodPurpose
append(x)adds to the end
insert(i, x)inserts at index i
delete(a,b)removes characters a to b-1
reverse()reverses the sequence
setCharAt(i, c)replaces a single character
toString()converts back to a String

class Character (static)

MethodPurpose
isDigit(c)true if c is 0-9
isLetter(c)true if c is a letter
isLetterOrDigit(c)true if letter or digit
isWhitespace(c)true if space/tab/newline
isUpperCase(c)/isLowerCase(c)case check
toUpperCase(c)/toLowerCase(c)case conversion