문자열을 나누는 함수를 작성하는것이 어려운것인가요?

gurumong의 이미지

요즘 필요한 일이 있어서 엑셀의 VBA(Visual Basic)를 익히고 있습니다
하나의 셀에 3D:1-3 와 같은 값이 있을 경우에
':'나 '-' 같은것을 구분자로 인식해서
3D, 1, 3로 잘게 나누어야 하는 함수를 작성하고있습니다

그런데 가볍게 보고 작성하다보니 코드가 길어지고 길어지고
결국에는 지저분해져서 코드를 삭제하고 새로 작성하고 싶은 마음이 생겼습니다
하지만 어떤식으로 해야 깔끔하게 될지 몰라 고민중입니다

처음 제가 작성한것은 이렇게 입니다
구분자가 아닌 단어 부분의 시작과 끝 부분을 찾아낸 다음 배열에 차곡 차곡 쌓습니다
말은 간단합니다
하지만 코드는 이렇습니다

Function spritStr(str As String, list As Variant)
'잘게 나누어진 단어를 채울 동적 배열
Dim sprite() As Variant
Dim s As String
 
ReDim sprite(0)
s = str
Trim (s)
 
Dim char As String
Dim indexA As Integer, indexB As Integer
'단어의 시작 위치, 끝 위치
indexA = 0
indexB = 0
 
Dim i As Integer
 
Do While (True)
    If (indexB + 1 >= Len(str)) Then Exit Do
 
    For i = indexB + 1 To Len(str)
        char = Mid(s, i, 1)
        If (isSeparator(char, list) = False) Then indexA = i: Exit For
    Next i
 
    If (indexA + 1 >= Len(str)) Then
        If indexA > indexB Then indexB = indexA
        Exit Do
    End If
 
    For i = indexA + 1 To Len(str)
        char = Mid(s, i, 1)
        If (isSeparator(char, list) = True) Then indexB = i - 1: Exit For
    Next i
 
    Dim upper As Integer
    upper = UBound(sprite) + 1
    ReDim Preserve sprite(upper)
    upper = UBound(sprite)
    sprite(upper) = Mid(s, indexA, indexB - indexA + 1)
 
Loop
 
'배열을 리턴
spritStr = sprite
Exit Function
 
End Function

아직 덜 작성되었는데
어쩔수 없는 구조 때문에 시작 부분을 찾았는지, 끝부분을 찾았는지를 체크하는 각각의 변수를 추가해야될꺼 같은데

정말 이 방법은 아닌거 같습니다
어떤 쉽게 할수있는 그런 알고리즘이 없을까요?

snowall의 이미지

http://www.cpearson.com/excel/splitondelimiters.aspx

이런거 찾으시는건가요?

그나저나, 어차피 "앞 단어의 끝"의 바로 직후는 "다음 단어의 시작" 아닌가요?

끝 부분과 시작 부분을 모두 찾아야 하는 이유가 있는지요

피할 수 있을때 즐겨라! http://melotopia.net/b

emptynote의 이미지

문자열 분리를 위해서는 우선 문자열 검색이 선행되어야 하겠지요.

저두 모 문자열 검색쯤이야 이렇게 생각하다가,

"C로 배우는 알고리즘 II권 활용알고리즘" 10장 읽고서

제가 생각한 방법은 러시아 페이트공 알고리즘으로

울물안 개구리임을 인정해야 했습니다.

만약 알고리즘 책이 없다면 저는 이 2권의 책을 추천해 드립니다.

10장에서 언급하는 4가지 검색법이 있습니다.

(1) Rabin-karp
(2) Boyer-More
(3) automata
(4) Knuth-Morris-Pratt

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.