본문 바로가기

RPA

[UiPath] String.Format 사용하기

보통 하나의 문자만 바꿀 때는 Replace를 사용한다.

근데 바뀌는 값이 많은 경우?! n번의 Replace를 해줘도 되지만.. 더 괜찮은 방법이 있다.

 

예를 들어 학생들에게 성적을 담은 이메일을 보내야하는 경우!

잔인한 .. 성적..
가 학생은 계절 방학을 보내게 되겠지..

위의 표같이 3명의 학생이 있을 때 아래의 형식으로 이메일을 보내려 한다.

Replace를 사용해서 다음과 같이 할 수 있다.

결과

strMailContent = strMailContent.Replace("[이름]",row(0).ToString)
strMailContent = strMailContent.Replace("[학과]",row(1).ToString)
strMailContent = strMailContent.Replace("[학번]",row(2).ToString)
strMailContent = strMailContent.Replace("[성적]",row(3).ToString)

 

column 개수가 적어서 assign으로 충분히 할 수 있지만 만약 column 개수가 꽤 많아지면 상당히 귀찮아질 것이다.

그래서 더 간단하게 바꿀 수 있는 방법은 String.Format이다.
자세한 내용은 아래 링크에서 확인할 수 있다.
https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0

 

String.Format Method (System)

Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see the Get started with the String.Format method section for a quick overview. See the Remarks sectio

docs.microsoft.com

 

String.Format을 사용하면 다음과 같이 바꿀 수 있다.

우선 Mail.txt를 조금 바꿔준다.

 

(좌) 변경 전 / (우) 변경 후
String.Format 결과

 

strMailContent = String.Format(strMailContent,row.ItemArray)
(row.ItemArray 말고 row(0).ToString, row(1).ToString,...으로 넣어줘도 된다)

{0}, {1}, {2}, {3}을 인덱스로 Array의 값을 넣어준다.
훨씬 편하다.

여러 개를 바꿀 일이 있다면 String.Format을 함 사용해보시길...

 

-----------

같은 값을 넣을 것이라면 동일한 인덱스를 또 넣어주면 된다.

가 학생.. 좋은 방학 보내세요...

 

'RPA' 카테고리의 다른 글

[UiPath] 디렉토리 사이즈 구하기  (0) 2021.12.16
[Zapier] zapier 알아보기  (0) 2021.11.16
[UiPath] iterator에서 원하는 값 찾기  (0) 2021.05.23
[UiPath] Chrome Alert Selector  (0) 2021.04.08
[UiPath] 파일 크기 구하기  (2) 2021.02.08