Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Simply If I have string of 30 Char Like This.
string str1="qwertyuiop12345asdfghjkl;"
how I can Make it smalle like "je5J" as Example?
Posted
Comments
Emre Ataseven 17-Apr-14 14:24pm    
and u want to convert it back to original string?

Why? The only way to do that is some compression scheme and, frankly, with such a short string, you're not going to get that much of a benefit as the tables used by the compression scheme will take up the space you save in the compression.
 
Share this answer
 
First of all, you can't get a high efficiency result.

Compression algorithms almost always have some form of space overhead, which means that they are only effective when compressing data which is sufficiently large that the overhead is smaller than the amount of saved space.

Compressing a string which is only 20 characters long is not too easy, and it is not always possible. If you have repetition, Huffman Coding or simple run-length encoding might be able to compress, but probably not by very much.


I did it without using compression algorithm actually, implementation is not good but maybe it gives you an idea.

The character range you want is between (as decimal)32 and 126, it means you can map your characters to 7 bits instead of 8 bits. You can get rid of 1 byte for each 8 chars.

Disadvantage of this approach is you can't get printable ASCII chars always.


C#
public string CompressString(string str)
{
    var sb = new StringBuilder();
    foreach (var chr in str)
    {
        sb.Append(Convert.ToString(chr, 2).PadLeft(7,'0'));
    }

    int width = (int)(Math.Ceiling((double)sb.Length / 8)) * 8;
    string bits = sb.ToString().PadLeft(width, '0');

    var list = Enumerable
           .Range(0, bits.Length / 8)
           .Select(i => bits.Substring(i * 8, 8))
           .ToList();

    var ascii = new string(list.Select(p => (char)Convert.ToByte(p, 2)).ToArray());
    return ascii;
}

public string DecompressString(string str)
{
    var sb = new StringBuilder();
    foreach (var chr in str)
    {
        sb.Append(Convert.ToString(chr, 2).PadLeft(8,'0'));
    }

    string bits = sb.ToString();
    bits = bits.Remove(0, bits.Length % 7);

    var list = Enumerable
          .Range(0, bits.Length / 7)
          .Select(i => bits.Substring(i * 7, 7).PadLeft(8,'0'))
          .ToList();

    var ascii = new string(list.Select(p => (char)Convert.ToByte(p, 2)).ToArray());
    return ascii;
}
 
Share this answer
 
You could create a simple substitution method which returns a string with at least some relation to the original. In your string you have 10 letters, 5 numbers, then 9 more letters. Your method could check each character in the string to see if is a letter, number, symbol or space and then return a string like this: "L10N5L9"

I did something like that once although I can't for the life of me remember why. This is just an idea, ans in many cases it would of course produce a longer string than the original! But for your example it is shorter.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900