Jump to content

UDF: _StringInChr


blindwig
 Share

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 *

Link to comment
Share on other sites

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 *

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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. :

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 month later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...