replace() 메소드
교체() String 클래스의 메소드는 두 개의 String 값을 받아들입니다 -
-
교체할 문자열(하위 문자열)의 일부를 나타내는 것입니다.
-
지정된 하위 문자열을 대체해야 하는 문자열을 나타내는 또 다른 문자열입니다.
이 방법을 사용하여 Java에서 문자열의 일부를 바꿀 수 있습니다.
예시
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
public static void main(String args[]) throws FileNotFoundException {
String filePath = "D://input.txt";
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input;
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
String contents = sb.toString();
System.out.println("Contents of the file: \n"+contents);
System.out.println(" ");
//Replacing the word with desired one
contents = contents.replace("Tutorialspoint", "TP");
System.out.println("Contents of the file after replacing the desired word: \n"+contents);
}
} 출력
Contents of the file: Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free. Contents of the file after replacing the desired word: Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.
이 방법의 또 다른 변형은 기존 문자와 한 문자를 각각(같은 순서로) 나타내는 두 문자를 허용하고 전체 문자열에서 이전 문자를 새 문자로 바꿉니다.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
public static void main(String args[]) throws FileNotFoundException {
String filePath = "D://input.txt";
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input;
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
String contents = sb.toString();
System.out.println("Contents of the file: \n"+contents);
System.out.println(" ");
//Replacing the word with desired one
contents = contents.replace('T', '#');
System.out.println("Contents of the file after replacing the desired word: \n"+contents);
}
} 출력
Contents of the file: Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free. Contents of the file after replacing the desired word: Hello how are you welcome to #utorialspoint. At #utorialspoint provide hundreds of technical tutorials for free.
replaceAll() 메소드
replaceAll() String 클래스의 메소드는 정규 표현식과 교체 패턴/문자열을 나타내는 두 개의 문자열을 허용하고 일치하는 값을 지정된 패턴/문자열로 대체합니다.
예시
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class replaceAllExample {
public static void main(String args[]) throws FileNotFoundException {
String filePath = "D://input.txt";
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input;
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
String contents = sb.toString();
System.out.println("Contents of the file: \n"+contents);
System.out.println();
//Replacing the word with desired one
contents = contents.replaceAll("\\bTutorialspoint\\b", "TP");
System.out.println("Contents of the file after replacing the desired word: \n"+contents);
}
} 출력
Contents of the file: Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free. Contents of the file after replacing the desired word: Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.
replaceFirst() 메소드
replaceFirst() String 클래스의 메소드(또한) replaceFirst는 정규 표현식과 대체 문자열을 나타내는 두 개의 문자열을 허용하고 첫 번째 일치 항목을 대체 문자열로 대체합니다.
예시
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
public static void main(String args[]) throws FileNotFoundException {
String filePath = "D://input.txt";
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input;
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
String contents = sb.toString();
System.out.println("Contents of the file: \n"+contents);
System.out.println(" ");
//Replacing the word with desired one
contents = contents.replaceFirst("Tutorialspoint", "TP");
System.out.println("Contents of the file after replacing the desired word: \n"+contents);
}
} 출력
Contents of the file: Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free. Contents of the file after replacing the desired word: Hello how are you welcome to TP. At Tutorialspoint provide hundreds of technical tutorials for free.
참고 − 이 모든 메소드는 대소문자를 구분합니다.