Page 1 of 1 1
Topic Options
#187656 - 2008-05-15 02:21 PM Detect uppercase characters
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Guy's,

Is there a clean and simple way to detect uppercase characters in a string?

A small example. Not real code but just something that is boiling in my head.
 Code:
$string1 = "FOObar
$string2 = "fooBAR"

$1 = UppercaseScan($string1)
$2 = UppercaseScan($string2)

?"1 = " $1
?"2 = " $2


will give you

1 = FOO
2 = BAR

Or maybe even something similar to the AScan() or InStr() functions.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#187657 - 2008-05-15 02:34 PM Re: Detect uppercase characters [Re: Mart]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
There may be a shiny UDF somewhere for this, REGEX would do it, and of course you could always do a char by char check... this is what I came up with first though...

 Code:
$string="FOObar"
if lcase($string) == $string
  ? "Has Caps"
endif


[Edit... missed the output of FOO and BAR Requirements... sorry. Thinking again ;\) ]


Edited by Allen (2008-05-15 02:38 PM)
_________________________
(... better days ahead)

Top
#187658 - 2008-05-15 02:44 PM Re: Detect uppercase characters [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Neat code-let Allen.

Except you got the logic slightly off \:\)

You meant
 Code:
$string="FOObar"
if lcase($string) == $string
  ? "No caps in string"
endif

Top
#187659 - 2008-05-15 02:50 PM Re: Detect uppercase characters [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
this one works ;\)
here is what i came up with:
 Code:
$string="FOObar"
$temp1 = $string

For $Index = 1 to ($string^)
  $temp2 = Left($temp1,1)
  If Asc($temp2) > 64 And Asc($temp2) < 91
    $Caps = $Caps + $temp2
  EndIf
  $temp1 = Right($temp1,~)
Next

? $Caps
? $string

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#187660 - 2008-05-15 02:52 PM Re: Detect uppercase characters [Re: Richard H.]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
uh hum. What Richard said.
_________________________
(... better days ahead)

Top
#187661 - 2008-05-15 02:55 PM Re: Detect uppercase characters [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Here is a sample script using a general purpose UDF.

Break ON


$sLCASE="abcdefghijklmnopqrstuvwxyz"
$sUCASE="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$sNumerics="0123456789"


$sString="FOObar123"


"Upper case characters in string: '"+udfGetChars($sString,$sUCASE)+"'"+@CRLF
"Lower case characters in string: '"+udfGetChars($sString,$sLCASE)+"'"+@CRLF
"Alpha characters in string: '"+udfGetChars($sString,$sLCASE+$sUCASE)+"'"+@CRLF
"Numeric characters in string: '"+udfGetChars($sString,$sNumerics)+"'"+@CRLF


Function udfGetChars($s,$sMatch)
Dim $SO
$udfGetChars=""
$SO=SetOption("CaseSensitivity","ON")
While $s<>""
If InStr($sMatch,Left($s,1)) $udfGetChars=$udfGetChars+Left($s,1) EndIf
$s=SubStr($s,2)
Loop
$SO=SetOption("CaseSensitivity",$SO)
EndFunction




Output:

 Quote:
Upper case characters in string: 'FOO'
Lower case characters in string: 'bar'
Alpha characters in string: 'FOObar'
Numeric characters in string: '123'

Top
#187662 - 2008-05-15 02:59 PM Re: Detect uppercase characters [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
showoff! ;\)

 Code:
$string="fooBAR"

? GetCaps($string)
? $string

Function GetCaps($string)
  $temp1 = $string
  For $Index = 1 to ($string^)
    $temp2 = Left($temp1,1)
    If Asc($temp2) > 64 And Asc($temp2) < 91
      $GetCaps = $GetCaps + $temp2
    EndIf
    $temp1 = Right($temp1,~)
  Next
EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#187663 - 2008-05-15 03:01 PM Re: Detect uppercase characters [Re: Benny69]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
Before the unsanctioned Golf tournament begins, here is Benny's code ungolfed

 Code:
For $Index = 1 to len($string)
  $temp2 = Left($temp1,1)
  If Asc($temp2) > 64 And Asc($temp2) < 91
    $Caps = $Caps + $temp2
  EndIf
  $temp1 = Right($temp1,-1)
Next

? $Caps
_________________________
(... better days ahead)

Top
#187664 - 2008-05-15 03:04 PM Re: Detect uppercase characters [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
LOL... I come back and two new posts already. \:\) KORG RULES!
_________________________
(... better days ahead)

Top
#187665 - 2008-05-15 03:07 PM Re: Detect uppercase characters [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Don't you just love UDF re-use:
 Code:
"There are "+Len(udfGetChars($sString,"aeiouAEIOU"))+" vowels in the string."+@CRLF


 Quote:
There are 3 vowels in the string.

Top
#187666 - 2008-05-15 03:19 PM Re: Detect uppercase characters [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
mine is Golfed and in pretty colors, so its better ;\)





$string="fooBAR"

? G($string)
? $string

Function G($)
Dim $,$i,$t
For $i = 1 to ($^)
$t = Left($,1)
If Asc($t) > 64 & Asc($t) < 91
$G = $G + $t
EndIf
$ = Right($,~)
Next
EndFunction


_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#187667 - 2008-05-15 03:26 PM Re: Detect uppercase characters [Re: Benny69]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Benny69
mine is Golfed and in pretty colors, so its better ;\)
....


Hmmmm.... hope your talking about the UDF if not then you should go and see a doctor \:o \:D

Thanks. I knew it had to be something easy. In the back of my mind I knew about the double = but just never did anything with it. After seeing the examples here it all came back. I really like Richards’ idea of a UDF to find certain characters. Thanks again.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1471 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.065 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org