Search code examples
actionscript-3apache-flexstring-formattingmasking

Flex: Replacing characters in a string based on a mask


I am attempting to apply a mask to another string to replace all wildcards in one string with the matching characters in the matching index while keeping the non-wildcard characters.

Eg:

starting string:         "1234-234-3456-45-9876"  
mask string:             "____-___-0001-__-____" 
when applied together:   "1234-234-0001-45-9876"

Is this some usage of Regex I haven't seen before? I tried to understand the string.replace() type methods, but I don't think these apply.


Solution

  • Hope this will help you:

    private function checkString():void
    {
    
    var starting:String = "1234-234-3456-45-9876"; 
    var mask:String = "____-___-0001-__-____";
    
    for(var i:int=0;i<starting.length;i++)
    {
      if(mask.charAt(i).match("[0-9]"))
        {
          starting = starting.substr(0,i) + mask.charAt(i) +  starting.substr(i+1);
        }
    }
        Alert.show(starting);
    }
    

    It will mask your string. Check result of alert.