String Operations

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 52:-  String Operations

In Java, the String class provides a wide range of methods to perform various operations on strings. Here are some common string operations that you can perform using the String class methods:

  1. Concatenation: The + operator is used for string concatenation, which combines two or more strings into a single string.

    javaCopy code

    String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName; // fullName is "John Doe"

  2. Length: The length() method returns the number of characters in a string.

    javaCopy code

    String text = "Hello, World!"; int length = text.length(); // length is 13

  3. Substring: The substring() method extracts a portion of the string based on the specified indices.

    javaCopy code

    String text = "Hello, World!"; String substring1 = text.substring(0, 5); // substring1 is "Hello" String substring2 = text.substring(7);    // substring2 is "World!"

  4. Conversion: You can convert a string to uppercase or lowercase using the toUpperCase() and toLowerCase() methods, respectively.

    javaCopy code

    String text = "Hello, World!"; String upperCaseText = text.toUpperCase();   // upperCaseText is "HELLO, WORLD!" String lowerCaseText = text.toLowerCase();   // lowerCaseText is "hello, world!"

  5. Trimming: The trim() method removes leading and trailing white spaces from a string.

    javaCopy code

    String text = "   Hello, World!   "; String trimmedText = text.trim(); // trimmedText is "Hello, World!"

  6. Replacement: The replace() method replaces occurrences of a substring with another substring.

    javaCopy code

    String text = "Hello, World!"; String replacedText = text.replace("Hello", "Hi"); // replacedText is "Hi, World!"

  7. Splitting: The split() method is used to split a string into an array of substrings based on a delimiter.

    javaCopy code

    String text = "apple,orange,banana"; String[] fruits = text.split(","); // fruits is {"apple", "orange", "banana"}

  8. Checking Contains: The contains() method checks if a particular substring is present in the string.

    javaCopy code

    String text = "Java Programming"; boolean containsJava = text.contains("Java"); // containsJava is true boolean containsPython = text.contains("Python"); // containsPython is false

  9. Formatting: You can format strings using the String.format() method or by using the + operator with placeholders.

    javaCopy code

    String name = "John"; int age = 30; String formattedString = String.format("My name is %s and I am %d years old.", name, age); // formattedString is "My name is John and I am 30 years old."

These are just a few examples of the many string operations that you can perform using the String class methods in Java. Strings are a fundamental part of programming, and understanding how to work with strings is essential in many Java applications.

9. Strings

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?