티스토리 뷰

JAVA

String methods in Java

seoca 2019. 1. 7. 10:46


Methods of String class



substring()

to get substring using substring(include index,exclude index);


1
2
String i="java";  
System.out.println(i.substring(1,2));//a
cs




length()

to get the length of the string.


1
2
3
String s="javastring";  
System.out.println("string length is: "+s.length());//10  
 
cs




equals() 

equals() method compares two strings found on the content of the string. if the all characters are matched, it returns true otherwise, it returns false. 


1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
  public String solution(String[] s) {
      String answer = "";
      String k = "Kim";
      for(int i = 0; i < s.length;i++){
        if(seoul[i].equals(k)){
              answer = "Kim is" + i+ "of Index";
          }
      }
      return answer;
  }
}
 
cs





compareTo() 

compareTo() method compares the String object with the String arguments. 

If the String object is lexicographically first, It returns < 0. 

If the parameter passed to the compareTo method is lexicographically first, it returns > 0.

It returns 0 when both String has the same value.


1
2
3
4
5
6
while(itr.hasNext()){
    String curStr = itr.next();
    System.out.printltn(curStr);
    if(curStr.compareTo("Third")==0)
        itr.remove();
}
cs





Integer.parseInt(String s) 

convert String to Integer. 


1
2
3
4
5
6
7
8
9
public class parse{ 
 
   public static void main(String args[]) {
      int a =Integer.parseInt("1234");
      double b = Double.parseDouble("3");
      System.out.println(a);//1234
      System.out.println(b);//3.0
   }
}
cs





Integer.valueOf() 

The difference between parseInt and valueOf is parseInt returns integer while valueOf returns Integer object. 

1
2
3
4
5
6
7
8
9
10
11
public class Main{
    public static void main(String[] args) {
 
        Integer obj = new Integer(7);
        String str = "1234";
 
        // It will return  a Integer instance
        System.out.println("Integer Value = " + obj.valueOf(str));
        // Integer Value = 1234
    }
}
cs



Integer.toString(int)
convert integer to String.
Null pointer exception.

1
2
3
4
5
6
7
8
public class Main{
    public static void main(String[] args) {
        int i = 5423;
        String str = Integer.toString(i);
        System.out.println("String str = " + str);
        //String str = 5423
    }
}
cs



String.valueOf(int)

convert integer to String. 
It gives NULL as value.

1
2
3
4
5
6
7
public class Main{
    public static void main(String[] args) {
        String str = String.valueOf(8765);
        System.out.println("String str = " + str);
        //String str = 8765
    }
}
cs


concat()

concat stands for 'Concatenate' which means to join together, as though in a chain so Concat() method combines strings. 


1
2
3
4
5
6
7
8
9
10
11
12
public class ConcatExample {  
    public static void main(String[] args) {      
        String str1 = "Hello";  
        String str2 = "JavaConcat";  
        String str3 = "Third";  
        String str4 = str1.concat(str2);          
        System.out.println(str4);//HelloJavaConcat
         // concat allows multiple strings
        String str5 = str1.concat(str2).concat(str3);  
        System.out.println(str5);//HelloJavaConcatThird
    }  
}  
cs




replace()

replace() method returns a string replacing old char to new char.


1
2
3
4
5
6
7
public class ReplaceExample{  
public static void main(String args[]){  
String s="replace in java";  
String replacechar=s.replace("java","char"); //replace in char
System.out.println(replacechar);  
}}  
cs






toUpperCase()

As you can see the name of this method, toUpperCase() returns the string in uppercase.


1
2
3
4
5
6
7
8
9
public class StringUpperExample{  
    public static void main(String args[]){  
        String s="change to upper";  
        String upperString=s.toUpperCase();  
        System.out.println(upperString);  
        //CHANGE TO UPPER
    }
}  
 
cs



toLowerCase()

change the string in lowercase.




IsEmpty()

IsEmpty method checks if the string is empty or not. 


1
2
3
4
5
6
7
8
9
public class IsEmptyExample{  
    public static void main(String args[]){  
        String s1="";  
        String s2="not empty";  
  
        System.out.println(s1.isEmpty()); //true
        System.out.println(s2.isEmpty()); //false  
    }
}
cs




matches()

matches() shows that the string matches regular expression or not.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
  public boolean solution(String s) {
      boolean answer = true;
   
      if(s.length() == 4 || s.length() == 6){
          if( s.matches("^[0-9]*$")){
              return answer;
          }else{
              return answer = false;
          }
      }else{
          return answer = false;
      } 
  }
}
cs






charAt()

charAt() returns the character at the specific index in a string.


1
2
3
4
5
6
7
8
9
10
11
12
public class charAtExample {
   public static void main(String args[]) {
    String str = "returns";
    char ch1 = str.charAt(0);
    char ch2 = str.charAt(1);
    char ch3 = str.charAt(3);
        
    System.out.println("0 index is: "+ch1); //r
    System.out.println("1st index is: "+ch2); //e
    System.out.println("3rd index is: "+ch3); //u
   }
}
cs

 




indexOf()

indexOf() returns the index of the specified character.


1
2
3
4
5
6
7
8
9
public class IndexOfExample{
   public static void main(String args[]) {
       String str1 = new String("This is a BeginnersBook tutorial");
       String str2 = new String("Beginners");
   
       System.out.println("Index of B in str1: "+str1.indexOf('B')); //Index of B in str1: 10
       System.out.println("Index of n in str2: "+str2.indexOf('n')); //Index of n in str2: 4
   }
}
cs



toCharArray()

toCharArray() method converts String to sequence of Character.

1
2
3
4
String convert = "colvertString";
char[] myConvert = convert.toCharArray();
myConvert[2= 'n';
convert = String.valueOf(myConvert); //convertString
cs


contains() 

contains() method returns true if it founds the same sequence of characters in a specific string.


1
2
3
4
String s = "This is string"
  
// returns true 
System.out.println(s.contains("string")); 
cs


'JAVA' 카테고리의 다른 글

toString method  (0) 2019.01.08
wrapper class in java  (0) 2019.01.07
Java Generics (runtime error,compile error)  (0) 2019.01.06
static in Java  (0) 2019.01.05
Overloading in Java with simple example  (0) 2019.01.04