Notes for Java

Methods of String

Sloth Coder 2022. 7. 1. 01:31

String.format
- returns a formatted String value.
ex) String.format("The answer is %d", 3) returns "The answer is 3"

- can be used with System.out.println
ex) System.out.println(String.formate("The answer is %d", 3))



String.split("delimiter")
- splits the String value using delimiter and returns a String type array.

String hi = "Hi Hello";
String[] prac = hi.split("H");
//prac = ["", "i ", "ello"]


- can be used without any parameter. In this case, delimiter is a space(" ").

String hi = "Hi Hello";
String[] prac = hi.split();
//prac = ["Hi", "Hello"]




String.substring(int i, int k)
- returns a String value of the String from i to k - 1.
- i and k are the index numbers of the String(the index number starts from 0).

String hi = "Hi Hello";
System.out.println(hi.substring(0, hi.length() - 1));
//"Hi Hell"


- can be used without the endpoint parameter. In this case, it returns a String value from the startingpoint to the end of the String.

String hi = "Hi Hello";
System.out.println(hi.substring(2);
// Hello

 

'Notes for Java' 카테고리의 다른 글

String, StringBuffer and StringBuilder  (0) 2022.07.01