Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

give me different ways of using string code with examples

+13 votes
asked Dec 18, 2025 by Hilton Joshua (230 points)

3 Answers

+2 votes
answered Dec 18, 2025 by Joyce Mary (180 points)
public class StringExample {
    public static void main(String[] args) {
        // 1. Creation and Initialization
        String str1 = "Java Programming"; // String literal (stored in String Pool)
        String str2 = new String("Java Programming"); // Using 'new' keyword (stored in Heap)

        // 2. Accessing Information
        int length = str1.length(); // Returns total characters (16)
        char firstChar = str1.charAt(0); // Returns 'J'
        int index = str1.indexOf("Prog"); // Returns 5

        // 3. Transformation (Returns new strings)
        String upper = str1.toUpperCase(); // "JAVA PROGRAMMING"
        String lower = str1.toLowerCase(); // "java programming"
        String sub = str1.substring(0, 4); // Extracts "Java"
        String replaced = str1.replace("Java", "Advanced"); // "Advanced Programming"

        // 4. Comparison
        boolean isEqual = str1.equals(str2); // true (checks content)
        boolean sameRef = (str1 == str2); // false (checks memory reference)
        boolean ignoreCase = str1.equalsIgnoreCase("JAVA programming"); // true

        // 5. Manipulation Tools
        String messy = "  Hello Java  ";
        String clean = messy.trim(); // "Hello Java" (removes outer spaces)
        
        String data = "apple,banana,cherry";
        String[] fruits = data.split(","); // Splits into String array

        // 6. Efficient Concatenation (StringBuilder)
        StringBuilder sb = new StringBuilder("Building"); // Mutable sequence
        sb.append(" a ").append("string..."); // Faster for multiple modifications
        String result = sb.toString();

        // Outputs
        System.out.println("Original: " + str1);
        System.out.println("Substring: " + sub);
        System.out.println("Joined via StringBuilder: " + result);
    }
}
+1 vote
answered Dec 20, 2025 by Nematjon Orifov (250 points)

For loop, While loop and Do while loop

+1 vote
answered Jan 5 by Linda Anderson (160 points)

if you’re talking Java String usage, there’s a ton of ways beyond the usual literal vs new String(). some of my go-to patterns:

  • String concatenation & formatting

String name = "Hilton"; String greeting = "Hello, " + name + "!"; // simple + String formatted = String.format("Hello, %s!", name); // cleaner for complex strings

  • StringBuilder / StringBuffer for mutable stuff

StringBuilder sb = new StringBuilder(); sb.append("Java").append(" Strings").append(" rock"); String result = sb.toString();

  • Substring, replace, and regex

String str = "apple,banana,cherry"; String[] fruits = str.split(","); // array of fruits String replaced = str.replace("banana", "orange"); String sub = str.substring(0, 5); // "apple"

  • Comparisons & checks

str.equalsIgnoreCase("APPLE,BANANA,CHERRY"); // true str.startsWith("apple"); // true str.contains("banana"); // true

  • Looping over chars

for(char c : str.toCharArray()){ System.out.println(c); }

basically, if you mix literals, builders, formatting, and regex, you can cover almost any string manipulation in Java without ever hitting performance issues.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...