Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

UDF: _StringInChr


  • Please log in to reply
15 replies to this topic

#1 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 11 June 2005 - 03:32 AM

Here's another function I wrote out of necessity. It works much like StringInStr, but instead of finding the substring in the string, it finds any of the chars from the substring in the string.

Here's the code:
CODE

Func _StringInChr($String, $Chars, $CaseSense = 0, $Occurance = 1)
Dim $i, $low=StringLen($String), $ret, $found = 0
If $Occurance = 0 Then
SetError(1)
Return 0
EndIf
For $i = 1 to StringLen($Chars)
$ret = StringInStr($String, StringMid($Chars, $i, 1), $CaseSense, $Occurance)
If $ret <> 0 Then
$found = -1
If $ret < $low Then $low = $ret
EndIf
Next
If $Found Then
Return $low
Else
Return 0
EndIf
EndFunc








#2 JSThePatriot

JSThePatriot

    carpe diem. vita brevis.

  • MVPs
  • 3,692 posts

Posted 11 June 2005 - 03:56 AM

Just as a note. I havent acutally looked the function over very well, but I think to go along with a logical naming method it should be CharInStr().

Though I noticed you used StringInStr() in your function. What exactly does yoru function do that StringInStr() doesnt do? I would love an explanation.

Just my thoughts to this point,
JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)


#3 ryeguy

ryeguy

    Wayfarer

  • Active Members
  • Pip
  • 78 posts

Posted 11 June 2005 - 04:05 AM

It says above his code.

#4 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 11 June 2005 - 04:11 AM

Just as a note. I havent acutally looked the function over very well, but I think to go along with a logical naming method it should be CharInStr().

Yeah, I debated myself over what to call it. CharInStr makes more sense from an English point of view, but going by the convention (module)(function), since it has to do with strings, it should be in the string module, and I guess it would make more sense to call it StringChrIn, but I went with the convention to match the StringInStr function.

Though I noticed you used StringInStr() in your function. What exactly does yoru function do that StringInStr() doesnt do? I would love an explanation.

Instead of looking for a substring in the string, in looks for characters in the string. It goes through each character in the substring and looks for that character in the string.

Oops, looking at it now, I see a big logic problem if you try to use $Occurance. I'll do a rewrite and get ack to you...

#5 JSThePatriot

JSThePatriot

    carpe diem. vita brevis.

  • MVPs
  • 3,692 posts

Posted 11 June 2005 - 04:37 PM

Yeah, I debated myself over what to call it.  CharInStr makes more sense from an English point of view, but going by the convention (module)(function), since it has to do with strings, it should be in the string module, and I guess it would make more sense to call it StringChrIn, but I went with the convention to match the StringInStr function.

Instead of looking for a substring in the string, in looks for characters in the string.  It goes through each character in the substring and looks for that character in the string.

Oops, looking at it now, I see a big logic problem if you try to use $Occurance.  I'll do a rewrite and get ack to you...

<{POST_SNAPBACK}>

I know what your function does, but StringInStr() can also find the characters in a string? Your function goes through and numbers how many occurances of the letter?

StringInStr() = SubString in String is how I took it. Which is why I said Char in String. You understand that as you said. I am still not sure why you would want it the other way around. It is a string function, but it would just go into the string.au3 header file. I had never heard that the function names are under a (module)(function) though that makes sense. :( Its your function I was just offering a suggestion.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)


#6 /dev/null

/dev/null

    Universalist

  • MVPs
  • 2,946 posts

Posted 11 June 2005 - 05:20 PM

how about StringContainsChar() or StringIsSuperSetofChar() :(

Cheers
Kurt
__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

#7 Helge

Helge

    Yup

  • Active Members
  • PipPipPipPipPipPip
  • 2,049 posts

Posted 11 June 2005 - 05:23 PM

I like StringCharsInStr(), however it doesn't exactly
explain what it does.. StringGetAllChars() ? :(

#8 /dev/null

/dev/null

    Universalist

  • MVPs
  • 2,946 posts

Posted 11 June 2005 - 07:33 PM

StringFindAnyChar() ??

Cheers
Kurt
__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

#9 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 11 June 2005 - 07:54 PM

StringFindAnyChar is the most 'AutoIt'
"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

#10 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 13 June 2005 - 06:16 AM

I know what your function does, but StringInStr() can also find the characters in a string? Your function goes through and numbers how many occurances of the letter?

<{POST_SNAPBACK}>

You're still not quite getting it:
StringInStr('abc','xyz') will look for 'xyz' in 'abc' and return it's position
StringFindAnyChar('abc','xyz') (if that's what I end up calling it) will look for 'x' or 'y' or 'z' in 'abc'

I had never heard that the function names are under a (module)(function) though that makes sense. :( Its your function I was just offering a suggestion.

<{POST_SNAPBACK}>

Think about it, all string-related functions start with string:
StringUpper
StringTrim
StringIsANum
etc
All file-related functions start with File, all GUI related functions start with GUI, etc...

And thanks for the suggestion. :( The whole reason I post here is for peer review and suggestions to make thing work better. :'(

#11 JSThePatriot

JSThePatriot

    carpe diem. vita brevis.

  • MVPs
  • 3,692 posts

Posted 13 June 2005 - 01:18 PM

You're still not quite getting it:
StringInStr('abc','xyz') will look for 'xyz' in 'abc' and return it's position
StringFindAnyChar('abc','xyz') (if that's what I end up calling it) will look for 'x' or 'y' or 'z' in 'abc'

Think about it, all string-related functions start with string:
StringUpper
StringTrim
StringIsANum
etc
All file-related functions start with File, all GUI related functions start with GUI, etc...

And thanks for the suggestion. :(  The whole reason I post here is for peer review and suggestions to make thing work better. :'(

<{POST_SNAPBACK}>

Okay I understand. I just thought everything was logically named :) That is why I would have said CharInStr(), but StringFindChars() would be fine I think.

Sounds good and somewhat descriptive.

JS :(

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)


#12 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 13 June 2005 - 09:05 PM

OK, here is the function, re-written from scratch:
CODE

Func _StringFindChr($String, $Chars, $CaseSense = 0, $Occurance = 1)

Dim $i = 0, $j, $s1, $sl = StringLen($String), $c1, $cl = StringLen($Chars), $Found

If $Occurance > 0 Then ;Start from left
$SearchDir = 1
$i = 1
ElseIf $Occurance < 0 Then ;Start from right
$SearchDir = -1
$i = $sl
Else ;error
SetError(1)
Return 0
EndIf

;loop through the string
While 0 < $i And $i <= $sl and $Occurance <> 0
$s1 = StringMid($String, $i, 1)
$Found = 0

;loop through the characters
$j = 1
While $j <= $cl and not $Found
$c1 = StringMid($Chars, $j, 1)
If $s1 == $c1 or ($s1 = $c1 and $CaseSense = 0) Then
$Found = -1
Else
$j = $j + 1
EndIf
WEnd

If $Found Then
$Occurance = $Occurance - $SearchDir
EndIf
If $Occurance <> 0 Then $i = $i + $SearchDir
WEnd

if $i > $sl Then $i = 0
Return $i
EndFunc

And this is how it works, for example:
_StringFindAnyChr('the quick brown fox jumped over the lazy dog','aeiou',0,3)
this will find the 3rd occurance of any vowel (regardless of case) in the given string and return it's location.

#13 cal

cal

    Wayfarer

  • Active Members
  • Pip
  • 96 posts

Posted 16 June 2005 - 08:00 PM

OK, here is the function, re-written from scratch:


Thanks. I've being trying to write something like this today using StringInStr without alot of luck. This is more then I need but your loops within it are exacty what I was looking for.

#14 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 16 June 2005 - 09:06 PM

Thanks.  I've being trying to write something like this today using StringInStr without alot of luck.  This is more then I need but your loops within it are exacty what I was looking for.

<{POST_SNAPBACK}>

What is it that you're trying to do?
Maybe my _StringInSet would be better suited to your task?

#15 cal

cal

    Wayfarer

  • Active Members
  • Pip
  • 96 posts

Posted 20 June 2005 - 04:53 PM

What is it that you're trying to do?
Maybe my _StringInSet would be better suited to your task?

<{POST_SNAPBACK}>

Nothing to fancy. I had a small script that took tv listings from TvTome and stripped them to just a ep number and title. I thought I'd redo it to work with Tv.com now that Tome is no more.

Tome had a very easy format to change. A few ctrl arrows and ctrlshiftarrows cut the lines down to exactly as I wanted them. tv.com has a different format, I need to search for numbers (skipping the ep numbers) and then backtrack to find the end of the title keeping any numbers in the (x) format as they are part of the title. Nothing to difficult.

Now that I've thought about it, there are several other ways of doing this as well.

I'll take a better look at your other code as well. Thanks.

#16 Baku

Baku

    Seeker

  • Active Members
  • 33 posts

Posted 29 July 2005 - 02:10 PM

Really nice idea, really nice udf. Thank you :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users