[go: up one dir, main page]

0% found this document useful (0 votes)
0 views3 pages

Conversions part 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Conversions part 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Conversions part 2

 String to char array:

java
Copy code
String str = "hello";
char[] chars = str.toCharArray(); // Convert String to char array

 Purpose: To access or manipulate individual characters in a string.


 Example:

java
Copy code
for (char ch : chars) {
System.out.println(ch);
}

 char array to String:

java
Copy code
char[] chars = {'h', 'e', 'l', 'l', 'o'};
String str = new String(chars); // Convert char array to String

 Purpose: To create a string from individual characters.


 Example:

java
Copy code
System.out.println(str); // Output: hello

 String to Integer:

java
Copy code
String numStr = "123";
int num = Integer.parseInt(numStr); // Convert String to int

 Purpose: To convert a numeric string to an integer.


 Example:

java
Copy code
System.out.println(num + 5); // Output: 128

 Integer to String:

java
Copy code
int num = 456;
String numStr = Integer.toString(num); // Convert int to String

 Purpose: To convert an integer to a string for concatenation or display.


 Example:

java
Copy code
System.out.println("Number: " + numStr); // Output: Number: 456

 String to List of characters:

java
Copy code
String str = "hello";
List<Character> charList = new ArrayList<>();
for (char ch : str.toCharArray()) {
charList.add(ch); // Convert String to List of characters
}

 Purpose: To use characters of a string in a list for manipulation.


 Example:

java
Copy code
System.out.println(charList); // Output: [h, e, l, l, o]

 List of characters to String:

java
Copy code
List<Character> charList = Arrays.asList('h', 'e', 'l', 'l', 'o');
StringBuilder sb = new StringBuilder();
for (Character ch : charList) {
sb.append(ch); // Convert List of characters to String
}
String str = sb.toString();

 Purpose: To concatenate characters from a list into a single string.


 Example:

java
Copy code
System.out.println(str); // Output: hello

 String to List of Strings (splitting by delimiter):

java
Copy code
String str = "apple,banana,orange";
List<String> words = Arrays.asList(str.split(",")); // Convert String to List
of Strings
 Purpose: To split a string into smaller substrings and store them in a list.
 Example:

java
Copy code
System.out.println(words); // Output: [apple, banana, orange]

 List of Strings to a single String:

java
Copy code
List<String> words = Arrays.asList("apple", "banana", "orange");
String result = String.join(",", words); // Convert List of Strings to a
single String

 Purpose: To combine multiple strings into a single string.


 Example:

java
Copy code
System.out.println(result); // Output: apple,banana,orange

 String to double:

java
Copy code
String numStr = "45.67";
double num = Double.parseDouble(numStr); // Convert String to double

 Purpose: To convert a numeric string with decimal values to a double.


 Example:

java
Copy code
System.out.println(num + 5); // Output: 50.67

 double to String:

java
Copy code
double num = 78.9;
String numStr = Double.toString(num); // Convert double to String

 Purpose: To convert a double to a string for display or concatenation.


 Example:

java
Copy code
System.out.println("Value: " + numStr); // Output: Value: 78.9

You might also like