public class stackPracticeQuestion {
public static void checkParenthesis(String str){
Stack stack = new Stack();
boolean checkFlag = true;
for (String s: str.split("")){
if (s.equals("(")) {
stack.push(s);
} else {
if (stack.isEmpty()){
checkFlag = false;
break;
} else {
stack.pop();
}
}
}
}
for (String s: str.split("")) 를 기준으로 문자를 하나씩 잘라서 확인할 수 있다.
이외에도 tocharArray()로 변환하여 한 문자씩 가지고 올 수 있다. (다만, char로 문자형태가 바뀌긴 할 것이다.)
String str = "ABCDE";
char[] arr = str.toCharArray();
for (int i = 0; i <arr.length; i++){
System.out.println(arr[i]+" ");
}
String str = "안녕하세요";
char c = str.charAt(0);
System.out.println(c); // "안"
String[] arStr = "ABC DEF".split("\\s");
String[] arStr = "ABC DEF".split("\\s+");
s = s.replace("?"," ");
s = s.trim();
종합하면 아래와 같이 코드를 구성할 수 있다.
public class Solution {
public statuc void main(Stinrg[] args) {
String s = "?ab???c ";
s = s.replace("?"," "); // " ab c "
s = s.trim(); // "ab c"
String[] answer = s.split("\\s+"); // {ab , c}
}
}