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

Java Strings

Uploaded by

mahzaibshah23
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)
7 views3 pages

Java Strings

Uploaded by

mahzaibshah23
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

In java string with all types of function

Creating Strings
String str1 = “Hello”; // String literal
String str2 = new String(“World”); // Using a constructor

Basic Operations
Int length = str1.length(); // Length of the string
Char firstChar = str1.charAt(0); // Accessing characters
Boolean isEmpty = str1.isEmpty(); // Check if empty
String concatStr = str1.concat(str2); // Concatenation

Comparison
Boolean isEqual = str1.equals(str2); // Compare content
Boolean isEqualIgnoreCase = str1.equalsIgnoreCase(“hello”); // Compare ignoring case
Int compareResult = str1.compareTo(str2); // Lexicographical comparison

Searching and Extracting

Int indexOfChar = str1.indexOf(‘o’); // Index of character


Int indexOfStr = str1.indexOf(“lo”); // Index of substring
Int lastIndex = str1.lastIndexOf(‘l’); // Last index of character
String substring = str1.substring(1, 3); // Extract substring
Modifying

String replaced = str1.replace(‘l’, ‘L’); // Replace characters


String trimmed = “ Hello “.trim(); // Remove leading and trailing spaces
String lowerCase = str1.toLowerCase(); // Convert to lowercase
String upperCase = str1.toUpperCase(); // Convert to uppercase

Splitting and Joining

String[] parts = str1.split(“ “); // Split into array


String joined = String.join(“-“, “2020”, “05”, “08”); // Join strings

Checking and Formatting

Boolean startsWith = str1.startsWith(“He”); // Check prefix


Boolean endsWith = str1.endsWith(“lo”); // Check suffix
String formatted = String.format(“Hello, %s!”, “World”);

Converting

Int intValue = Integer.parseInt(“123”); // Convert to integer


String strValue = String.valueOf(456); // Convert to string
Char[] charArray = str1.toCharArray(); // Convert to character array
Byte[] byteArray = str1.getBytes(); // Convert to byte array

You might also like