String methods in java with examples pdf free download

Tammy 110 Published: 10/17/2024

String methods in java with examples pdf free download

I'm happy to help! Here are the string methods in Java with examples:

1. Concatenation

In Java, strings can be concatenated using the + operator or by calling the concat() method.

Example:

String str = "Hello";

str += " World!";

System.out.println(str); // prints "Hello World!"

or

String str = "Hello";

str = str.concat(" World!");

System.out.println(str); // prints "Hello World!"

2. Length

The length() method returns the length of a string.

Example:

String str = "Hello";

int length = str.length();

System.out.println(length); // prints 5

3. IndexOf

The indexOf() method searches for a specified character or substring in a string and returns its starting index.

Example:

String str = "Hello World!";

int index = str.indexOf("World");

System.out.println(index); // prints 6

4. Substring

The substring() method extracts a subset of characters from a string.

Example:

String str = "Hello World!";

String subStr = str.substring(6);

System.out.println(subStr); // prints "World!"

5. UpperCase/LowerCase

The toUpperCase() and toLowerCase() methods convert strings to uppercase or lowercase, respectively.

Example:

String str = "hello";

String upperStr = str.toUpperCase();

System.out.println(upperStr); // prints "HELLO"

String lowerStr = str.toLowerCase();

System.out.println(lowerStr); // prints "hello"

6. Replace

The replace() method replaces a specified character or substring with another string.

Example:

String str = "Hello World!";

str = str.replace("World", "Java");

System.out.println(str); // prints "Hello Java!"

7. Split

The split() method splits a string into an array of substrings based on a specified separator.

Example:

String str = "Apple, Banana, Orange";

String[] fruits = str.split(",");

for (String fruit : fruits) {

System.out.println(fruit);

}

// prints each fruit on a separate line: Apple, Banana, Orange

8. Trim

The trim() method removes whitespace characters from the beginning and end of a string.

Example:

String str = "   Hello   ";

str = str.trim();

System.out.println(str); // prints "Hello"

9. StartsWith/EndsWith

The startsWith() and endsWith() methods check if a string starts or ends with a specified prefix/suffix, respectively.

Example:

String str = "Hello World!";

boolean startsWith = str.startsWith("Hello");

System.out.println(startsWith); // prints true

boolean endsWith = str.endsWith("World!");

System.out.println(endsWith); // prints true

10. Contains

The contains() method checks if a string contains a specified substring.

Example:

String str = "Hello World!";

boolean contains = str.contains("World");

System.out.println(contains); // prints true

Download the PDF: You can download this information as a PDF by searching for "Java String Methods" or "Java Programming Language" online. There are many resources available that provide in-depth information on Java programming, including tutorials, examples, and reference materials.

Free Resources:

Oracle's Official Java Tutorials: https://docs.oracle.com/javase/tutorial/ Codecademy's Java Course: https://www.codecademy.com/learn/java Java Programming Language Reference: https://docs.oracle.com/javase/8/docs/api/index.html

Remember, practice makes perfect! Try these examples and more to improve your Java programming skills. Happy coding!

String methods in java with examples pdf download

I'm happy to help you with that!

String methods in Java are used to manipulate and process strings. Here's a comprehensive guide on string methods in Java, along with examples.

1. concat(String str)

The concat() method concatenates the specified string at the end of this string.

Example:

String str = "Hello";

str = str.concat(" World!");

System.out.println(str); // Outputs: HelloWorld!

2. substring(int beginIndex, int endIndex)

The substring() method returns a new string that is a substring of this string.

Example:

String str = "Hello World!";

String subStr = str.substring(6);

System.out.println(subStr); // Outputs: World!

3. toLowerCase()

The toLowerCase() method converts all uppercase characters in this string to lowercase characters.

Example:

String str = "HELLO";

str = str.toLowerCase();

System.out.println(str); // Outputs: hello

4. toUpperCase()

The toUpperCase() method converts all lowercase characters in this string to uppercase characters.

Example:

String str = "hello";

str = str.toUpperCase();

System.out.println(str); // Outputs: HELLO

5. trim()

The trim() method returns a copy of the string, without leading and trailing white spaces.

Example:

String str = "   Hello World!  ";

str = str.trim();

System.out.println(str); // Outputs: Hello World!

6. split(String regex)

The split() method splits this string around matches of the given regular expression pattern.

Example:

String str = "Hello,World!,Java!";

String[] arr = str.split(",");

for (String s : arr) {

System.out.println(s);

} // Outputs: Hello World! Java!

7. replaceFirst(String regex, String replacement)

The replaceFirst() method replaces the first substring of this string that matches the given regular expression pattern with the given replacement.

Example:

String str = "Hello,World!";

str = str.replaceFirst(",", "-");

System.out.println(str); // Outputs: Hello-World!

8. contains(String str)

The contains() method returns a boolean indicating whether this string contains the specified substring.

Example:

String str = "Hello World!";

boolean result = str.contains("World");

System.out.println(result); // Outputs: true

9. equalsIgnoreCase(String str)

The equalsIgnoreCase() method compares two strings ignoring case and returns true if they are equal.

Example:

String str1 = "Hello";

String str2 = "hello";

boolean result = str1.equalsIgnoreCase(str2);

System.out.println(result); // Outputs: true

10. endsWith(String suffix)

The endsWith() method tests whether this string ends with the given suffix.

Example:

String str = "Hello World!";

boolean result = str.endsWith("World!");

System.out.println(result); // Outputs: true

These are some of the most commonly used string methods in Java. You can download a comprehensive PDF guide on these methods from here.

Remember, it's essential to use the correct method for your specific task to avoid errors and ensure efficient processing.

That's it! I hope you found this information helpful. Happy coding!