문자열 대체 함수 설명

string_replace 함수는 특정 부분 문자열을 찾아 새로운 문자열로 대체하는 데 사용됩니다. 이 함수는 문자열에서 첫 번째 발생만 대체하며, 모든 발생을 대체하려면 string_replace_all 함수를 사용해야 합니다.

문법

string_replace(str, substr, newstr);

매개변수 설명

매개변수 타입 설명
str String 파싱할 문자열
substr String 대체할 문자열 내의 부분 문자열
newstr String 이전 문자열을 대체할 새로운 부분 문자열

반환값

  • 반환값: String

예제

str1 = "Hello Earth";
str2 = string_replace(str1, "Earth", "World");

위의 예제에서 str2str1의 첫 번째 "Earth"를 "World"로 대체하여 "Hello World"가 됩니다.

활용 예제

  1. 단어 교체 gml original = "I love programming."; updated = string_replace(original, "programming", "coding");
  2. 문장 수정 gml sentence = "The cat sat on the mat."; new_sentence = string_replace(sentence, "cat", "dog");
  3. 간단한 텍스트 편집기 gml text = "Good morning, everyone!"; modified_text = string_replace(text, "morning", "afternoon");
  4. 사용자 입력 처리 gml user_input = "Please replace this text."; result = string_replace(user_input, "text", "string");
  5. 게임 내 대화 수정 gml dialogue = "Welcome to the game, player!"; updated_dialogue = string_replace(dialogue, "player", "hero");

이와 같이 string_replace 함수를 활용하여 다양한 문자열 대체 작업을 수행할 수 있습니다.