# Comparing Strings

**URL:** <https://forum.kirupa.com/t/comparing-strings/309711>\
**Category:** open source\
**Created:** [May 22, 2010, 12:18am UTC](https://forum.kirupa.com/t/comparing-strings/309711 "2010-05-22T00:18:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreeHugs](https://avatars.discourse-cdn.com/v4/letter/f/59ef9b/32.png) [@FreeHugs](https://forum.kirupa.com/u/FreeHugs)\
**Post date:** [May 22, 2010, 12:18am UTC](https://forum.kirupa.com/t/comparing-strings/309711/1 "2010-05-22T00:18:53Z")

</div>

Hey guys,

I was fiddling around with how I might compare two strings and return a value based on how similar they are. This is the best I came up with and it was surprisingly simple too 😏

```php

package 
{
	public class String2
	{		
		public static function compare (arg1:String, arg2:String):Number
		{
			arg1 = arg1.toLowerCase();
			arg2 = arg2.toLowerCase();
			
			var diff:Number = 0;
			
			for(var i:int = 0; i < arg1.length; i++)
			{
				if(arg2.indexOf(arg1.charAt(i)) == -1)
				{
					diff ++;
				}
			}
			
			diff += Math.abs(arg1.length - arg2.length) / arg1.length;
			
			return 1 / (diff + 1);
		}
	}
}

```

Pretty much self explanatory I think. Compares the first parameter to the second and returns and number between 0 and 1 based on the difference between them.

Thought it might come in useful to some peoples 🙂
