May 11, 2024

String Manipulation in Java

String manipulation

String manipulation involves modifying or analyzing strings to achieve your desired outcomes. In Java, you can perform various operations such as concatenation, substring extraction, and searching within strings.

Examples

// Example 1: Concatenating strings
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // fullName = "John Doe"

// Example 2: Extracting substring
String message = "Hello, World!";
String substring = message.substring(0, 5); // substring = "Hello"

// Example 3: Searching within a string
String text = "Java is a powerful language.";
boolean containsJava = text.contains("Java"); // containsJava = true

5 FAQ (interview questions and answers)

  1. What is string manipulation?
    Modifying or analyzing strings to achieve desired outcomes
    Converting strings to other data types
    Displaying strings on the console
  2. What is the result of concatenating two strings?
    A new string containing the combined contents of the original strings
    An error
    A numeric value
  3. How do you extract a substring from a string in Java?
    Using the slice() method
    Using the substring() method
    Using the substr() method
  4. Can you search for a specific substring within a string?
    No, it's not possible
    Only with regular expressions
    Yes, using the contains() method
  5. What is the index of the first character in a string?
    0
    1
    -1

Your Total Score: 0 out of 5

Remember to just comment if you have any doubts or queries.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.