|
By Prasanna Sherekar |
Check if given Value Contains in String Array – Java Example
|
String[] stringArray = new String[] {"AB","BC","CD","AE"}; Arrays.asList(stringArray).contains("XY"); // --> false Arrays.asList(stringArray).contains("AB"); // --> true // Note: This doesn't work for arrays of primitives // Using Java 8 Stream String[] values = {"AB","BC","CD","AE"}; Arrays.stream(values).anyMatch("s"::equals); // --> false Arrays.stream(values).anyMatch("CD"::equals); // --> true |
|
By Prasanna Sherekar |
String Comparison in Java
— Example shows different ways of comparing two String Values in Java.
|
new String("test").equals("test") // --> true new String("test") == "test" // --> false new String("test") == new String("test") // --> false "test" == "test" // --> true "test" == "te" + "st" // --> true Objects.equals("test", new String("test")) // --> true Objects.equals(null, "test") // --> false Objects.equals(null, null) // --> true |
|
By Prasanna Sherekar |
Reverse a String in Java
|
public class StringReverseExample { public static void main(String[] args) { String string = "abcdef"; String reverse = new StringBuffer(string). reverse().toString(); System.out.println("\nString before reverse:"+string); System.out.println("String after reverse:"+reverse); } } |