StartprogrammingusingObjectPascal:CopyFunction

From 흡혈양파의 번역工房
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Copy 함수

copy 함수를 사용하여 문자열의 일부를 복사할 수 있습니다. 예를 들어 문자열 ‘hello world’에서 ‘world’ 단어를 가져올 필요가 있다면, 아래 우리가 작성한 예제와 같이 해당 부분의 위치를 알고 있을 때 복사할 수 있습니다.

var
    Line: string;
    Part: string;
begin
    Line:= 'Hello world';

    Part:= Copy(Line, 7, 5);

    Writeln(Part);

    Writeln('Press enter key to close');
    Readln;
end.


이 구문을 사용하여 Copy 함수를 사용했습니다.

Part:= Copy(Line, 7, 5);


위 구문에 대한 설명입니다.

  • Part := 함수의 결과(하위 문자열 'world' )를 저장할 문자열 변수입니다.
  • Line ‘Hello world’ 문장이 들어있는 원본 문자열입니다.
  • 7 뽑고 싶은 하위 문자열의 시작 지점 혹은 인덱스 입니다. 이 경우 이 위치에 있는 문자는 'w' 입니다
  • 5 뽑을 부분의 길이입니다. 이 경우 단어 ‘world’의 길이를 나타냅니다.


다음 예제에서, 사용자에게 February와 같은 달 이름을 입력할 것을 요구한 다음, 프로그램에서 입력한 달 이름을 Feb와 같은 짧은 버전을 출력할 것입니다.

var
    Month: string;
    ShortName: string;
begin
    Write('Please input full month name e.g. January : ');
    Readln(Month);
    ShortName:= Copy(Month, 1, 3);
    Writeln(Month, ' is abbreviated as : ', ShortName);
    Writeln('Press enter key to close');
    Readln;
end.