PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Substring in Java

← All stacks

Theory

77/270

Substring in Java

Substring is about working with text in Java. Remember the base rule first: String is immutable. Compare content with equals, not ==.

For Substring, common APIs from memory: length(), charAt(i), substring(start, end), contains, equalsIgnoreCase, toUpperCase. Index starts at 0.

If Substring is StringBuilder/StringBuffer, you change text in place with append. If it is comparison, show == false and equals true for two new String("Hi").

For Substring, say the output before you code. Off-by-one on substring is a common trap (end index is exclusive).

Let's take this on the board with one tiny Main class — no extra files. Substring in Java — shows length, substring, case change, and equalsIgnoreCase. Start from main, go line by line, and stop at each print. That output is the proof for Substring.

Exam tip

== vs equals() on String is a classic trap.

Example

// String methods
public class Main {
  public static void main(String[] args) {
    String s = "PrepPilot";
    System.out.println(s.length());
    System.out.println(s.substring(0, 4));
    System.out.println(s.toUpperCase());
    System.out.println("prep".equalsIgnoreCase("PREP"));
  }
}

Substring in Java — shows length, substring, case change, and equalsIgnoreCase.

Short notes

  • DefSubstring works with Java text. String itself is immutable.
  • RuleSubstring — compare content with equals, not ==.
  • RememberSubstring — length, charAt, substring. substring end index is exclusive.
  • TrapSubstring — == compares references. User input almost never matches with ==.

Questions

1

What is Substring?

2

Why do we use Substring in Java?

3

Write one small example of Substring.

4

What mistake do beginners make with Substring?

5

Where is Substring used in a real program?

Previous← String Concatenation in JavaNextJava String Methods →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.