Software Development Exam  >  Software Development Notes  >  Basics of Java  >  String class in Java

String class in Java | Basics of Java - Software Development PDF Download

When programming in Java, one frequently encounters the need to manipulate and work with text data. Java provides a built-in class called String that is specifically designed to handle textual data. The String class is part of the Java Standard Library and offers a wide range of methods for manipulating and working with strings.
In this article, we will explore the various features and capabilities of the String class in Java. We will cover string creation, concatenation, comparison, manipulation, and more. By the end, you will have a solid understanding of how to effectively work with strings in Java.

Creating Strings

In Java, you can create a string by enclosing a sequence of characters within double quotes. Here's an example:

String greeting = "Hello, World!";

You can also create an empty string using the empty string literal "":

String emptyString = "";

Concatenating Strings


String concatenation is the process of combining two or more strings together. In Java, you can concatenate strings using the + operator. Here's an example:

String firstName = "John";

String lastName = "Doe";

String fullName = firstName + " " + lastName;


System.out.println(fullName); // Output: John Doe

You can also use the concat() method to concatenate strings:

String firstName = "John";

String lastName = "Doe";

String fullName = firstName.concat(" ").concat(lastName);


System.out.println(fullName); // Output: John Doe

Comparing Strings

To compare strings in Java, you can use the equals() method. This method compares the contents of two strings and returns true if they are equal, and false otherwise. Here's an example:

String str1 = "apple";

String str2 = "orange";

String str3 = "apple";


System.out.println(str1.equals(str2)); // Output: false

System.out.println(str1.equals(str3)); // Output: true

To perform a case-insensitive comparison, you can use the equalsIgnoreCase() method:

String str1 = "apple";

String str2 = "Apple";


System.out.println(str1.equalsIgnoreCase(str2)); // Output: true

Manipulating Strings

The String class in Java provides several methods to manipulate strings. Here are a few commonly used methods:

  • length(): Returns the length of the string.
  • charAt(index): Returns the character at the specified index.
  • substring(startIndex, endIndex): Returns a substring starting from the startIndex up to, but not including, the endIndex.
  • toUpperCase(): Converts the string to uppercase.
  • toLowerCase(): Converts the string to lowercase.

Let's see some examples of these methods in action:

String str = "Hello, World!";


System.out.println(str.length()); // Output: 13

System.out.println(str.charAt(0)); // Output: H

System.out.println(str.substring(7, 12)); // Output: World

System.out.println(str.toUpperCase()); // Output: HELLO, WORLD!

System.out.println(str.toLowerCase()); // Output: hello, world!

Sample Problems

Now that we have covered the basics of the String class, let's solve a few sample problems to reinforce our understanding.

Problem 1: Write a program to count the number of vowels in a given string.

String str = "Hello, World!";

int vowelCount = 0;


for (int i = 0; i < str.length(); i++) {

    char c = str.charAt(i);

    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||

        c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {

        vowelCount++;

    }

}


System.out.println("Number of vowels: " + vowelCount);

Problem 2: Write a program to check if a given string is a palindrome (reads the same forwards and backwards).

String str = "madam";

boolean isPalindrome = true;


for (int i = 0; i < str.length() / 2; i++) {

    if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {

        isPalindrome = false;

        break;

    }

}


if (isPalindrome) {

    System.out.println("Palindrome");

} else {

    System.out.println("Not a palindrome");

}

Conclusion

In this article, we have explored the String class in Java and learned how to create, concatenate, compare, and manipulate strings. The String class provides a rich set of methods that make working with textual data in Java a breeze. By understanding the various methods and features of the String class, you can efficiently handle and manipulate strings in your Java programs.

The document String class in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

shortcuts and tricks

,

ppt

,

Viva Questions

,

video lectures

,

Sample Paper

,

String class in Java | Basics of Java - Software Development

,

practice quizzes

,

mock tests for examination

,

String class in Java | Basics of Java - Software Development

,

Exam

,

pdf

,

String class in Java | Basics of Java - Software Development

,

Extra Questions

,

past year papers

,

Free

,

Semester Notes

,

MCQs

,

Important questions

,

Objective type Questions

,

Summary

,

study material

;