Convert from Integer to Character


Java
int intValue = 65; // Unicode value of 'A' 
char charValue = (char) intValue; // Output: A

Convert from String to Integer


Java
// If `currStrVal` contains anything that is not a valid integer, it will throw a `NumberFormatException`
// Leading or trailing whitespace in the string is ignored
String currStrVal = "123";
int num = Integer.parseInt(currStrVal);

Delimited String Operations


Java
// Joins multiple strings with a specified delimiter.
String result = String.join(",", "Java", "is", "fun"); // Output: "Java,is,fun"
 
// Splits a string into an array based on a delimiter.
String[] parts = "Java,is,fun".split(","); // Output: String[] { "Java", "is", "fun" }

String Slicing


Java
// Extracts chars from index 7 (inclusive) to 12 (exclusive).
String result = "Hello, World!".substring(7, 12); // Output: "World"

String Comparison


Java
// Checks if two strings are equal.
"Java".equals("Java"); // Output: true
 
// Compares strings lexicographically.
"Apple".compareTo("Banana"); // Output: -1 (Apple < Banana)

Alphabetical Sorting of String


Java
char[] charArray = s.toCharArray();
Arrays.sort(charArray);
String sorted = new String(charArray);
  • To sort a string, we first need to convert it into a character array
  • The Arrays.sort() method is a void method and does not return a value. it sorts the array in place.
  • Finally, we need to convert the sorted character array back into a string
Python
sorted_arr = ''.join(sorted(s))
  • We can sort a string using sorted() without converting it to a character array.
  • However, we still need to use ''.join() to obtain a sorted string because sorted() returns a list
C++
string sorted = s;
sort(sorted.begin(), sorted.end());
  • C++ std::sort() works directly on a std::string without needing to convert it to a character array, because a std::string in C++ is essentially a sequence of characters (similar to a char array)
  • Just like in Java, std::sort() returns void