본문 바로가기
개발/LUA

[LUA] 문자열 다루기 간단 예제

by esstory 2019. 7. 27.

Lua를 사용한 간단한 문자열 예제 

(기존 파이썬 문자열 예제 소스를 루아에 맞도록 수정)

 

[파이썬] 문자열 다루기

파이썬 문자열 관련 자주 사용하는 함수들 위주로 샘플 코드를 만들었습니다 sText = ' *# They said: "Of course." and you said "Thank you" #* ' print('원문:', sText) #결과: 원문: *# They said: "Of course..

eslife.tistory.com

-- 목적: 루아 문자열 예제

local sEng = 'abc'
local sKor = '가나다'

print('--------------------------------------------------------------------')
print('문자열 길이 - strin.len')
print('--------------------------------------------------------------------')
local nLen1  = string.len(sEng)
local nLen2  = string.len(sKor)
print('문자열 길이', nLen1, nLen2)
--  [Outpt] 문자열 길이	3	9

local sText = '   *# They said: "Of course." and you said "Thank you" #*   '
print('원문:', sText)
-- [Outpt] 원문:    *# They said: "Of course." and you said "Thank you" #*

print('--------------------------------------------------------------------')
print('문자열 Trim - 공백제거')
print('--------------------------------------------------------------------')
local sText2 = string.gsub(sText, '%s', '')
-- %s는 공백문자를 의미 
print('공백제거:', sText2)
-- [Outpt] 공백제거: #Theysaid:"Ofcourse."andyousaid"Thankyou"#*
-- 루아는 trim Left, trimRight 같은 함수가 없어서 모든 공백을 제거해 버린다. 


print('--------------------------------------------------------------------')
print('특정 문자열 위치 찾기')
print('--------------------------------------------------------------------')
local nStart, nEnd = string.find(sText, 'They')
print('"They" 단어 시작 및 끝 위치:', nStart, nEnd)
-- [Outpt] "They" 단어 시작 및 끝 위치:	7	10
if string.find(sText, 'They') ~= nil then 
    print('They 문자열 있음')
end
if string.find(sText, 'young') == nil then 
    print('young 문자열 없음')
end
nStart, nEnd = string.find(sText, 'She')
print('"She" 문자 찾기 [Outpt]', nStart, nEnd)
-- find 로 못찾을 경우에는 nil 을 리턴한다.
-- [Outpt] "She" 문자 찾기 [Outpt]	nil	nil


print('--------------------------------------------------------------------')
print('특정위치 문자열 가져오기')
print('--------------------------------------------------------------------')
local sSubText = string.sub(sText, 7, 10)
print('7~10 위치 문자 가져오기:',sSubText)
-- [Outpt] 7~10 위치 문자 가져오기:	They



print('--------------------------------------------------------------------')
print('sText 내의 특정 단어를 변경 해서 sText2 에 저장')
print('--------------------------------------------------------------------')
sText2 = string.gsub(sText, 'They', 'He')
print('They --> He 로 변경:',sText2)
-- [Outpt] They --> He 로 변경:	   *# He said: "Of course." and you said "Thank you" #*   

print('--------------------------------------------------------------------')
print('소문자 만들기')
print('--------------------------------------------------------------------')
sText2 = string.lower( sText)
print('소문자 만들기:', sText2)
-- [Outpt] 소문자 만들기:	   *# they said: "of course." and you said "thank you" #*   
 
print('--------------------------------------------------------------------')
print('문자열 포맷- c 와 유사 ')
print('--------------------------------------------------------------------')
local Pie = 3.14159
sText2 = string.format('%.2f', Pie)
print('Pie =', sText2)
-- [Outpt] Pie = 3.14
 
print('--------------------------------------------------------------------')
print('문자열을 공백으로 split 하기')
print('--------------------------------------------------------------------')
for value in string.gmatch(sText, "%S+") do 
    print(value)
end

 
print('--------------------------------------------------------------------')
print('문자/숫자 전환 ')
print('--------------------------------------------------------------------')
local sText4 = "3.14"
local fData = tonumber(sText4)
fData = fData + 0.1
print('문자열 3.14 숫자로 바꾸기:', fData, type(fData))
-- [Outpt] 문자열 3.14 숫자로 바꾸기:	3.24	number


sText4 = "1024"
local nData = tonumber(sText4)
print('문자열 1024 숫자로 바꾸기:', nData, type(nData))
-- [Outpt] 문자열 1024 숫자로 바꾸기:	1024	number

sText5 = tostring(fData)
print(sText5, type(sText5))
-- [Outpt] 3.24	string

sText5 = tostring(nData)
print(sText5, type(sText5))
-- [Outpt] 1024	string

 

결과

[Running] lua "d:\WS\luaPrj\luastring.lua"


--------------------------------------------------------------------
문자열 길이 - strin.len
--------------------------------------------------------------------
문자열 길이	3	9
원문:	   *# They said: "Of course." and you said "Thank you" #*   


--------------------------------------------------------------------
문자열 Trim - 공백제거
--------------------------------------------------------------------
공백제거:	*#Theysaid:"Ofcourse."andyousaid"Thankyou"#*


--------------------------------------------------------------------
특정 문자열 위치 찾기
--------------------------------------------------------------------
"They" 단어 시작 및 끝 위치:	7	10
They 문자열 있음
young 문자열 없음
"She" 문자 찾기 [Outpt]	nil	nil


--------------------------------------------------------------------
특정위치 문자열 가져오기
--------------------------------------------------------------------
7~10 위치 문자 가져오기:	They


--------------------------------------------------------------------
sText 내의 특정 단어를 변경 해서 sText2 에 저장
--------------------------------------------------------------------
They --> He 로 변경:	   *# He said: "Of course." and you said "Thank you" #*   


--------------------------------------------------------------------
소문자 만들기
--------------------------------------------------------------------
소문자 만들기:	   *# they said: "of course." and you said "thank you" #*   


--------------------------------------------------------------------
문자열 포맷- c 와 유사 
--------------------------------------------------------------------
Pie =	3.14


--------------------------------------------------------------------
문자열을 공백으로 split 하기
--------------------------------------------------------------------
*#
They
said:
"Of
course."
and
you
said
"Thank
you"
#*


--------------------------------------------------------------------
문자/숫자 전환 
--------------------------------------------------------------------
문자열 3.14 숫자로 바꾸기:	3.24	number
문자열 1024 숫자로 바꾸기:	1024	number
3.24	string
1024	string

[Done] exited with code=0 in 0.044 seconds

'개발 > LUA' 카테고리의 다른 글

[LUA] 문자열 다루기 간단 예제 #2  (0) 2020.05.24
[LUA] TABLE 정렬(SORT)  (2) 2019.10.19
[LUA] 개발 환경 만들기  (3) 2019.09.30
[LUA] FOR 문  (2) 2019.09.29
[LUA] 함수 호출 CALL BY VALUE/CALL BY REFERENCE  (1) 2019.07.28
[LUA] 테이블 다루기 - 배열/맵/클래스  (1) 2019.07.28

댓글