Java String

Comparison

String one   = "hello";
String two = "world";
String three = "hello";
String four = new String("hello");
String five = "HELLO"
System.out.println(one==two); # false
System.out.println(one==three); # true
System.out.println(one==four); # false
# best to use this
System.out.println(one.equals(two)); # false
System.out.println(one.equals(three)); # true
System.out.println(one.equals(four)); # true
System.out.println(one.equalsIgnoreCase(five)); # true

String To Char Array

# string to char array
String hw = "hello world";
char[] ca = hw.toCharArray();
# print the array
for(char c:ch){
System.out.println(c);
}

Char To ASCII Number

# convert char to ASCII number
System.out.println((int)'A');

Char To String

# char to string
char c = 'A';
String s = String.valueOf(c);
System.out.println(s);

To Lower/Upper Case

String hw = "Hello World";

# to lower case
System.out.println(hw.toLowerCase());
# to upper case
System.out.println(hw.toUpperCase());

Substring

# substring params are start index, end index
String hw = "hello world";
System.out.println(hw.substring(6,7)); # w

Reverse

String s = "hello world";
char[] ca = s.toCharArray();
ArrayList<String> al = new ArrayList<String>();
for(char c:ca){
al.add(String.valueOf(c));
}
Collections.reverse(al);
System.out.println(String.join("",al));

Split

String s = "the!quick@brown#fox$jumps%over^the&lazy dog";
String[] a = s.split("[!@#$%^& ]");
for(String z:a){
System.out.println(z);
}

String To Integer

String s = "88";
int i = Integer.parseInt(s);
System.out.println(i);

--

--

--

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How the right Hackathon formula can do wonders for your teams

Get from Point A to B safely with Tech for Good

Deploy Secure Landing Zone on Oracle Cloud Infrastructure

First Version of NFT in Dapp ☄️

Shopify App Development: Is It Possible to Build an E-commerce App in 24 Hours at Hackathon?

Differences in Full Stack, Backend, Front End Developers

Onboarding and performance

Spring Festival Campaign

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin FOO

Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

More from Medium

OOPs in Java

Basics of Java Programming

Digging into JAVA — Chapter 04

Variables methods and Objects

Java Naming Conventions