#
String methods in Java
#
String methods in Java
Some main string methods in Java are:
Here we can see how these methods are used: length()
, toUpperCase()
, toLowerCase()
, trim()
,
substring()
, replace()
, replaceFirst()
, startsWith()
, endsWith()
, charAt()
, indexOf()
, lastIndexOf()
,
equals()
, equalsIgnoreCase()
, compareTo()
, String.valueOf()
, String.join()
, chars()
, codePoints()
,
isBlank()
, lines()
, count()
, repeat()
, transform()
, formatted()
, split()
, toCharArray()
.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Arrays;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Demo1Application.class, args);
String str1 = " Hello word ! ";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("----------------------------------");
System.out.println("str1="+str1);
System.out.println("str2="+str2);
System.out.println("str3="+str3);
System.out.println("----------------------------------");
//length()
System.out.println("str1.length()="+str1.length());
//toUpperCase()
System.out.println("str1.toUpperCase()="+str1.toUpperCase());
//toLowerCase()
System.out.println("str1.toLowerCase()="+str1.toLowerCase());
//trim()
System.out.println("str1.trim()="+str1.trim());
//toCharArray()
char[] charArray = str2.toCharArray();
System.out.println("charArray.length="+charArray.length);
//substring()
System.out.println("str2.substring(0,2)="+str2.substring(0,2));
System.out.println("str2.substring(2)="+str2.substring(2));
//replace()
System.out.println("str2.replace(\"l\",\"3\")"+str2.replace("l","3"));
//replaceFirst()
System.out.println("str2.replaceFirst(\"l\",\"3\")="+str2.replaceFirst("l","3"));
//startsWith() & endsWith()
System.out.println("str2.startsWith(\"H\")="+str2.startsWith("H"));
//charAt()
System.out.println("str2.charAt(2)="+str2.charAt(2));
//indexOf() & lastIndexOf()
System.out.println("str2.indexOf(\"l\")="+str2.indexOf("l"));
System.out.println("str2.lastIndexOf(\"l\")="+str2.lastIndexOf("l"));
//equals() & equalsIgnoreCase()
System.out.println("str2.equals(str3)="+str2.equals(str3));
// "==" operator
System.out.println("str2 == str3 // returns "+str2 == str3);
//compareTo()
System.out.println("str2.compareTo(str3)="+str2.compareTo(str3));
//String.valueOf() => create a String
System.out.println("valueOf(4)="+String.valueOf(4));
//String.join()
System.out.println("String.join(\"-\", \"abc\", \"dcf\", \"nnn\")="+String.join("-", "abc", "dcf", "nnn"));
// chars() - returns an IntStream of char values of the given string
// codePoints() - returns an IntStream of Unicode code points of the chars of the given string
str2.chars().forEach(System.out::println);
//split()
Arrays.stream(str1.split(" ")).forEach( var1 -> {
System.out.println("var1="+var1);
});
//Java 11
//isBlank()
System.out.println("\n \n".isBlank());
//Java 11
//lines() & count()
System.out.println("\n\n".lines().count());
//Java 11
//repeat()
System.out.println("a".repeat(3));
//Java 12
//transform()
var strX = str2.transform(str -> str.concat(" John"))
.transform(String::toUpperCase);
System.out.println("strX= "+strX);
//Java 15
//formatted()
String strF = "1) %s, 2) %s";
System.out.println("strF.formatted() = "+strF.formatted("John", "Emma"));
}
}
When we run this code, we receive :
----------------------------------
str1= Hello word !
str2=Hello
str3=Hello
----------------------------------
str1.length()=14
str1.toUpperCase()= HELLO WORD !
str1.toLowerCase()= hello word !
str1.trim()=Hello word !
charArray.length=5
str2.substring(0,2)=He
str2.substring(2)=llo
str2.replace("l","3")He33o
str2.replaceFirst("l","3")=He3lo
str2.startsWith("H")=true
str2.charAt(2)=l
str2.indexOf("l")=2
str2.lastIndexOf("l")=3
str2.equals(str3)=true
false
str2.compareTo(str3)=0
valueOf(4)=4
String.join("-", "abc", "dcf", "nnn")=abc-dcf-nnn
72
101
108
108
111
var1=
var1=Hello
var1=word
var1=!
true
2
aaa
strX= HELLO JOHN
strF.formatted() = 1) John, 2) Emma