开发者

What's the best way to shorten/transform a string

开发者 https://www.devze.com 2023-02-26 03:26 出处:网络
I have several users and I would like to generate a referral url for each of the开发者_如何学编程m, but I don\'t think it would be nice to have something like

I have several users and I would like to generate a referral url for each of the开发者_如何学编程m, but I don't think it would be nice to have something like

http://domain.com/myusername

I would prefer to buy myself a shorten domain and transfor that myusernameinto something smaller, but not generated using the database so it would be easy to manage and no need to have a special table for this.

kinda like Dropbox does, as my referral is: http://db.tt/jnYosez

is there a simple way to change between a string into a short string and vice versa ... or I can always use the User ID if I'm force to use numeric ... maybe converting to other number base ?

Any idea is mostly appreciated :)


You can't do it inhouse without using a database. It's because the names are short and therefore it's likely that a name have been used before.

You need to generate a name (using randomness or an algorithm), check if it exists and then store it in the db.

Update

Here is a solution:

    public class NumberConverter
    {
        public const string HEX = "0123456789abcdef";
        public const string Bigger = "0123456789abcdefghihjklmnopqrstuvqyz";
        private int _numericBase;
        private string _base;

        public NumberConverter(string numberBase)
        {
            _base = numberBase;
            _numericBase = numberBase.Length;
        }

        public string ToString(int number)
        {
            var remainder = number % _numericBase;
            var div = number / _numericBase;
            string tmp = "";
            while (div > 0)
            {
                tmp = _base[remainder] + tmp;

                remainder = div % _numericBase;
                div = div / _numericBase;
            }
            tmp = _base[remainder] + tmp;

            return tmp;
        }

        public int ToNumber(string numberString)
        {
            int index = numberString.Length - 1;
            int value = 0;
            int power = 0;
            while (index >= 0)
            {
                char currentChar = numberString[index];
                var currentValue = _base.IndexOf(currentChar);
                value += currentValue * (int)Math.Pow(_numericBase, power);
                power++;
                --index;
            }

            return value;
        }
    }



    public static void Main()
    {
        var converter = new NumberConverter(NumberConverter.Bigger);
        int userId = 755757;
        var numberString = converter.ToString(userId); // prints g759
        var value = converter.ToNumber(numberString);
    }

You can shorten the numbers even more by adding more that are allowed in uris.

Theres probably a more efficient way to do it. Bitwise operations isn't really my area.

Update 2

Characters that are allowed in a URI but do not have a reserved purpose are called unreserved. These include uppercase and lowercase letters, decimal digits, hyphen, period, underscore, and tilde.

  unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

Which means that you can use those to shorten the url (unless your web server is using them for something else)


You might want to look into bit.ly links, they provide an API.

0

精彩评论

暂无评论...
验证码 换一张
取 消