Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Exploring Methods of String in Java: Part 2

In this lesson, you will learn

  • String ‘Formatting’ Methods
  • String ‘Data Conversion’ Methods
  • String ‘Manipulation’ Methods

 

String ‘Formatting’ Methods

In Java, toUpperCase() and toLowerCase() are methods of the String class used to convert all the characters in a given string to uppercase or lowercase, respectively.

 

toUpperCase() Method

  • Description: Converts all characters in the string to uppercase.
  • Syntax: public String toUpperCase()
  • Returns: A new string representing the original string converted to uppercase.

Example

String str = "Hello World";
String upperStr = str.toUpperCase();
System.out.println(upperStr); // Outputs: HELLO WORLD

 

toLowerCase() Method

  • Description: Converts all characters in the string to lowercase.
  • Syntax: public String toLowerCase()
  • Returns: A new string representing the original string converted to lowercase.

Example

String str = "Hello World";
String lowerStr = str.toLowerCase();
System.out.println(lowerStr); // Outputs: hello world

 

String ‘Data Conversion’ Methods

In Java, data conversion methods related to strings are crucial for transforming data between different types, such as converting strings to numbers (integers, floating-point numbers) and vice versa.

Converting Strings to Numbers

Java provides several wrapper classes like Integer, Double, Float, etc., which offer methods to convert strings into respective numeric types.

Integer.parseInt(String s) and Integer.valueOf(String s)

parseInt: Converts the string argument to an integer (int).

Syntax:

public static int parseInt(String s) throws NumberFormatException

Example

String str = "100";
int num = Integer.parseInt(str);
System.out.println(num); // Outputs: 100

 

valueOf(): Returns an Integer object holding the value extracted from the specified string.

Syntax:

public static Integer valueOf(String s) throws NumberFormatException

Example

String str = "200";
Integer num = Integer.valueOf(str);
System.out.println(num); // Outputs: 200

 

Double.parseDouble(String s) and Double.valueOf(String s)

Similar to the Integer class, for converting strings into doubles.

parseDouble()

Syntax:

public static double parseDouble(String s) throws NumberFormatException

Example

String str = "99.99";
double num = Double.parseDouble(str);
System.out.println(num); // Outputs: 99.99

 

valueOf()

Syntax:

public static Double valueOf(String s) throws NumberFormatException

Example

String str = "10.01";
Double num = Double.valueOf(str);
System.out.println(num); // Outputs: 10.01

 

Converting Numbers to Strings

String.valueOf() Method

Java provides a String.valueOf() method that can convert various data types, including numeric types to String.

Syntax:

public static String valueOf(dataType data)

Example

int num1 = 100;
String str1 = String.valueOf(num1);
System.out.println(str1); // Outputs: "100"

double num2 = 99.99;
String str2 = String.valueOf(num2);
System.out.println(str2); // Outputs: "99.99"

 

Converting Other Data Types to Strings

Besides numbers, String.valueOf() can also handle other data types, including characters, boolean values, and objects, converting them to their string representation.

Example with Boolean

boolean flag = true;
String boolString = String.valueOf(flag);
System.out.println(boolString); // Outputs: "true"

 

String ‘Manipulation’ Methods

Output

 

 

 

 

 

 

 

 

 

 

 

Output

 

 

 

 

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review