// 31. stripLeading()
public class StripLeadingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedString = str.stripLeading();
System.out.println("Leading stripped string: '" + strippedString + "'");
}
}// 32. stripTrailing()
public class StripTrailingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedString = str.stripTrailing();
System.out.println("Trailing stripped string: '" + strippedString + "'");
}
}// 33. isBlank()
public class IsBlankExample {
public static void main(String[] args) {
String str = " ";
boolean isBlank = str.isBlank();
System.out.println("Is the string blank? " + isBlank);
}
}// 34. lines().count()
public class LinesCountExample {
public static void main(String[] args) {
String str = "Hello\nWorld\nWelcome";
long lineCount = str.lines().count();
System.out.println("Number of lines: " + lineCount);
}
}// 35. formatted()
public class FormattedExample {
public static void main(String[] args) {
String formattedString = "Name: %s, Age: %d";
String result = String.format(formattedString, "John", 30);
System.out.println("Formatted string: " + result);
}
}// 36. stripIndent()
public class StripIndentExample {
public static void main(String[] args) {
String str = " Line 1\n Line 2\n Line 3";
String strippedString = str.stripIndent();
System.out.println("Stripped indents:\n" + strippedString);
}
}// 37. translateEscapes()
public class TranslateEscapesExample {
public static void main(String[] args) {
String str = "Hello\\nWorld\\tJava";
String translatedString = str.translateEscapes();
System.out.println("Translated escapes: " + translatedString);
}
}// 38. indent(int n)
public class IndentExample {
public static void main(String[] args) {
String str = "Hello\nWorld";
String indentedString = str.indent(4);
System.out.println("Indented string:\n" + indentedString);
}
}// 39. repeat(int count)
public class RepeatCountExample {
public static void main(String[] args) {
String str = "Java ";
String repeatedString = str.repeat(3);
System.out.println("Repeated string: " + repeatedString);
}
}// 40. stripLeading(), stripTrailing()
public class StripLeadingTrailingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedLeading = str.stripLeading();
String strippedTrailing = str.stripTrailing();
System.out.println("Leading stripped string: '" + strippedLeading + "'");
System.out.println("Trailing stripped string: '" + strippedTrailing + "'");
}
}// 41. replaceFirst()
public class ReplaceFirstExample {
public static void main(String[] args) {
String str = "apple orange apple orange";
String replacedString = str.replaceFirst("apple", "banana");
System.out.println("String after replacing first occurrence: " + replacedString);
}
}// 42. regionMatches()
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello World";
String str2 = "WORLD";
boolean matches = str1.regionMatches(true, 6, str2, 0, 5);
System.out.println("Do regions match ignoring case? " + matches);
}
}// 43. offsetByCodePoints()
public class OffsetByCodePointsExample {
public static void main(String[] args) {
String str = "Hello World";
int index = str.offsetByCodePoints(0, 6);
System.out.println("Index after moving by code points: " + index);
}
}// 44. join()
public class JoinExample {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Java"};
String joinedString = String.join(", ", words);
System.out.println("Joined string: " + joinedString);
}
}40+ java strings methods 👆
Run directly and understand
or
go in @java_group_pro copy code
and you can also edit
then run by /jv command
Run directly and understand
or
go in @java_group_pro copy code
and you can also edit
then run by /jv command
🥰2
anyone java coder who love java programming and codes much in java ?
Who explores capabilities of java ?
Say me @Panditsiddharth
Who explores capabilities of java ?
Say me @Panditsiddharth
//sum two numbers in Java
public class Main {
public static void main(String[] args) {
int num1 = 5; // First number
int num2 = 7; // Second number
int sum = num1 + num2; // Adding the two numbers
System.out.println("Sum is: " + sum); // Printing the sum
}
}//multiply two numbers in Java
public class Main {
public static void main(String[] args) {
int num1 = 6;
int num2 = 5;
int product = num1 * num2;
System.out.println("product is: " + product);
}
}👍1
//Even odd using if else in java
public class Main {
public static void main(String[] args) {
int num = 4;
if( num % 2 == 0){
System.out.println("Number is Even");
}
else{
System.out.println("Nunber is Odd");
}
}
}👍4
// Remove duplicates from array
import java.util.Arrays;
import java.util.LinkedHashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
Integer[] arrayWithDuplicates = {1, 2, 3, 3, 4, 5, 5, 6};
// Convert array to LinkedHashSet to remove duplicates while preserving order
LinkedHashSet<Integer> set = new LinkedHashSet<>(Arrays.asList(arrayWithDuplicates));
// Convert back to array
Integer[] arrayWithoutDuplicates = set.toArray(new Integer[0]);
// Print array without duplicates
System.out.println(Arrays.toString(arrayWithoutDuplicates));
}
}