startsWith1 [자바] startsWith, split String 타입의 문자열을 한 줄로 입력받았을 때 여러 부분으로 나눠서 처리하고 싶다.이럴 때 사용할 수 있는 것이 startsWith와 split이다.예를 들면 java로 시작하는 문자열인지를 판별하여 문장의 두 번째 단어를 다루고 싶다면String command = sc.nextLine()if(command.startsWith("java")){ int value = Integer.parseInt(command.split(" ")[1]) }위 코드처럼 사용할 수 있다.공백이 아닌 - 혹은 , 등으로 문장을 구분하고 싶다면 command.split("-")[index], command.split(",")[index]처럼 사용하면 된다. 2025. 1. 6. 이전 1 다음