How I find that string contain a repeating character more then 6 time in Flex?

◇◆丶佛笑我妖孽 提交于 2019-12-13 00:18:35

问题


How I find that string contain a repeating character more then 6 time in Flex? In my case the user input is only digits (0-9) and i am doing for US Fax no validation. like 11111112255 , 225555555522 etc


回答1:


A regular expression way:

/(.)\1{6}/

This will search for any character (not necessarily digit) repeated 7 times.

/(\d)\1{6}/

The same, but for digits only.

There are techniques that will be generally faster than regexp, for example, slightly modified Bitap algorithm may prove to be faster.

Here's an altered version of Bitup algorithm to search for repeating characters in a string:

package
{
    import flash.display.Sprite;

    public class BitapConsequent extends Sprite
    {
        public function BitapConsequent()
        {
            super();
            test();
        }

        private function test():void
        {
            // 10 -1 3 0
            trace(bitapConsequent("---####$$$^^^^^^", 6),
                bitapConsequent("---####$$$^^^^^^", 7),
                bitapConsequent("---####$$$^^^^^^", 4),
                bitapConsequent("---####$$$^^^^^^", 3));
        }

        private static function bitapConsequent(
            text:String, consequent:uint):int
        {
            // Unless found, the result is negative
            var result:int = -1;
            var len:int = text.length;
            var offset:int;
            var reverse:int;

            if (len >= consequent)
            {
                consequent--;
                len -= consequent;
                // loop through the whole string sans
                // the substring which is shorter than
                // the number of characters that have to
                // be the same
                outer: for (; offset < len; offset++)
                {
                    // jump to the farmost end of the possible
                    // match and walk back checking that each
                    // two characters match, i.e. if this is
                    // the array of characters a = ['a', 'b', 'b']
                    // then first check if a[2] == a[1], then
                    // if a[1] == a[0], if characters are equal,
                    // continue to the next iteration, else--
                    // restart the search
                    for (reverse = offset + consequent;
                        reverse > offset; reverse--)
                    {
                        if (text.charAt(reverse) !== 
                            text.charAt(reverse - 1))
                            continue outer;
                    }
                    // If we got here, we've found `consequent'
                    // equal characters, so terminate the loop
                    result = offset;
                    break;
                }
            }
            return result;
        }
    }
}

The earlier version indeed used bitup algorithm, but after some thought, I've realized it wasn't required, so this is a bit more refined version, which also doesn't limit one to the 32-characters matches only.




回答2:


You can use a regex:

var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
    trace( "The string '" + testString + "' contains more than 6 repeating digits" );

Edit:

As @wvxvw points out, this will break if your string is something like 1112222 or 1234567 - his regex gets around that




回答3:


I do this in this way,

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        protected function button1_clickHandler(event:MouseEvent):void
        {
            if(id_input.text.length >=10)
            {
                for(var i:uint=0; i<4; i++)
                {
                    var counter:int = 0;
                    var str:String = id_input.text.substr(i,1);
                    var index:uint = 0;
                    index = i;
                    index += 1;
                    //case 1
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 2
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 3
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 4
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 5
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 6
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    if(counter >= 6){
                        Alert.show('More then 6 char present in string');
                        break;
                    }
                }
            }
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:VGroup width="100%" height="100%">

    <s:TextInput id="id_input"/>
    <s:Button label="Validate" click="button1_clickHandler(event)" />

</s:VGroup>



来源:https://stackoverflow.com/questions/13193426/how-i-find-that-string-contain-a-repeating-character-more-then-6-time-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!