Sign in to follow this  
Followers 0
Dethredic

Separating a Srting by Capital Letters

12 posts in this topic

#1 ·  Posted (edited)

I need something that will help me turn this:

NaO - $variable1 = Na, $variable2 = O or

SiCl - $variable1 = Si, $variable2 = Cl or

FO - $variable1 = F, $variable 2 = O

I have this, which will tell me the number of capitals in the string

Func CountCapitals($sString)
    Local $sReturn = 0
    $sSplit = StringSplit($sString,"")
    For $sN = 1 to $sSplit[0]
        If StringIsUpper($sSplit[$sN]) Then $sReturn += 1
    Next
    Return $sReturn
EndFunc

I can't figure out how to split the string using StringSplit or anything else.

Edited by Dethredic

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites



$sString = 'This Is A String'
$nCount = _StringCountUL($sString)
MsgBox(0, 'Count Upper', $nCount)
$nCount = _StringCountUL($sString, False)
MsgBox(0, 'Count Lower', $nCount)

Func _StringCountUL($sString, $bUpper = True)
    If $bUpper Then
        StringRegExpReplace($sString, '[A-Z]', '')
        Return @extended
    Else
        StringRegExpReplace($sString, '[a-z]', '')
        Return @extended
    EndIf
    Return SetError(1, 0, 0)
EndFunc


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include <Array.au3>
MsgBox(0, "", _splitCaps("HiMyNameIsMephy")) ;Executes the function
Func _splitCaps($string)
    $string = $string & "A" ;You need this because otherwise it won't add the last $tempstring. Just leave it
    Local $ma[1], $char, $tempstring, $char2 ;Declare our vars
    For $i = 1 to StringLen($string) step 1 ;Loop through the whole string
        $char = StringMid($string, $i, 1) ;Get each char in the string
        if StringIsUpper($char) Then ;Selfexplanatory
            $tempstring = $char ;Start a new tempstring to be added to the array
            for $u = $i + 1 to StringLen($string) ;start a new loop
                $char2 = StringMid($string, $u, 1) ;get each char from the current capital char to the end of the string
                if StringIsLower($char2) Then ;explanatory
                    $tempstring = $tempstring & $char2 ;add it to the tempstring if it's lowercase
                Else ;if it's uppercase..
                    _ArrayAdd($ma, $tempstring) ;add the tempstring to the array (this is why we need the capital A at the end of the string
                    ExitLoop
                EndIf
            Next
        EndIf
    Next
    _arrayDelete($ma, 0)
    Return _ArrayToString($ma, ",")
EndFunc

Edit: Added Comments

Edited by the_lord_mephy

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites
#include <Array.au3>
MsgBox(0, "", _splitCaps("HiMyNameIsMephy"))
Func _splitCaps($string)
    $string = $string & "A"
    Local $ma[1], $char, $tempstring, $char2
    For $i = 1 to StringLen($string) step 1
        $char = StringMid($string, $i, 1)
        if StringIsUpper($char) Then
            $tempstring = $char
            for $u = $i + 1 to StringLen($string)
                $char2 = StringMid($string, $u, 1)
                if StringIsLower($char2) Then
                    $tempstring = $tempstring & $char2
                Else
                    _ArrayAdd($ma, $tempstring)
                    ExitLoop
                EndIf
            Next
        EndIf
    Next
    _arrayDelete($ma, 0)
    Return _ArrayToString($ma, ",")
EndFunc
oÝ÷ Ûú®¢×v÷öÛay!{§¶Ëh±êZ­«^¶°¢·lnëZ~׫$x·*.­ìZ^ʦk'âÚÞéZ­ìi­ë,ì{^­ë-xºÚ"µÍÙÐÞ
    ][ÝÉ][ÝËÜÜ]ØÊ   ][ÝÒS^S[YRÓYI][ÝÊJB[ÈÜÜ]ØÊ    ÌÍÜÔÝ[ÊBSØØ[    ÌÍØP^HHÝ[ÔYÑ^
    ÌÍÜÔÝ[Ë   ÌÎNÖÐKVJÖØK^JÉÌÎNËÊK ÌÍÜÒÛRYÐ^J    ÌÍØP^JH[BQÜ ÌÍÚPÐÈHÈPÝ[
    ÌÍØP^JHLBBBIÌÍÜÒÛ   [ÏH    ÌÍØP^VÉÌÍÚPÐ×H [È ÌÎNË ÌÎNÂBS^Q[YT]Ý[Õ[TYÚ
    ÌÍÜÒÛJB[[

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I don't think he wants to separate the words... but after looking at your example, you may find regular expressions interesting:

MsgBox(0, "", _splitCaps("HiMyNameIsMephy"))
Func _splitCaps($sString)
    Local $aArray = StringRegExp($sString, '[A-Z]+[a-z]+', 3), $sHold
    If IsArray($aArray) Then
        For $iCC = 0 To UBound($aArray) -1
            $sHold &= $aArray[$iCC] & ','
        Next
    EndIf
    Return StringTrimRight($sHold, 1)
EndFunc
Touche.. i haven't used autoit in a while (3 years or so) so I'm not familiar with all the functions and such ;p

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Touche.. i haven't used autoit in a while (3 years or so) so I'm not familiar with all the functions and such ;p

You'd like this one even better... It's come a ways (AutoIt) taking 40 lines of code and shrinking it to a few with just the use of the Regular Expressions.

MsgBox(0, "", _splitCaps("HiMyNameIsMephy"))
Func _splitCaps($sString)
    $sString = StringRegExpReplace($sString, '[A-Z]+[a-z]+', ',');Dammit ',' is supposed to be back slash + zero + comma but the forum keeps omitting it!!!
    Return StringTrimRight($sString, 1)
EndFunc
There... out did myself :P

Edit:

Hmm, left out the back reference for some reason :whistle:

Edit2:

No I didn't, it's the AutoIt code tags again!!! Switched to regular code tags!

Edit3:

Check comment in script for back slash + zero + comma omission .

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

hahaha i was trying to figure out what was wrong, that's incredible though, autoit has come a long ways O_O

Edit:

btw, @dethredic you get your answer? lol

Edited by the_lord_mephy

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites

They work great, but I can't figure out how to make each word a variable afterwards


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

They work great, but I can't figure out how to make each word a variable afterwards

#include <array.au3>
$aArray = _StringSplitCaps("HiMyNameIsMephy")
If IsArray($aArray) Then
    _ArrayDisplay($aArray, 'Returned array')
EndIf

Func _StringSplitCaps($sString);using chr(92) because \ won't show in the forum tags
    Return StringSplit(StringTrimRight(StringRegExpReplace($sString, '[A-Z]+[a-z]+', Chr(92) & '0,'), 1), ',')
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You could even make it 0 based if you didn't want to use StringSplit

#include <array.au3>
$aArray = _StringSplitCaps("HiMyNameIsMephy")
If IsArray($aArray) Then
    _ArrayDisplay($aArray, 'Returned array')
EndIf

Func _StringSplitCaps($sString);using chr(92) because \ won't show in the forum tags
    Return StringRegExp($sString, '[A-Z]+[a-z]+', 3)
EndFunc


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thanks SmOke_N, I can always count on your help


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

@the_lord_mephy

In your example, if you change this $string = $string & "A" to this $string = $string & "_", it seems to be able to handle single uppercase letters at the end, eg. SoAmI will now display as So,Am,I before it just removed the single I.

I know this is a followup on a really old topic but someone else might need the input.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0