制作字符串Shoreter [英] Make string Shoreter

查看:76
本文介绍了制作字符串Shoreter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单如果我有30个字符串就像这样。

字符串str1 =qwertyuiop12345asdfghjkl;

我怎么能像je5J那样小一些?

Simply If I have string of 30 Char Like This.
string str1="qwertyuiop12345asdfghjkl;"
how I can Make it smalle like "je5J" as Example?

推荐答案

为什么?唯一的方法是使用一些压缩方案,坦率地说,使用这么短的字符串,你不会得到那么大的好处,因为压缩方案使用的表将占用你在压缩中节省的空间。
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.


首先,你无法获得高效率的结果。



压缩算法几乎总是有某种形式的空间开销,这意味着它们仅在压缩数据时有效,数据足够大,开销小于节省的空间量。



压缩字符串是只有20个字符不太容易,并不总是可行。如果你有重复,霍夫曼编码或简单的游程编码可能会压缩,但可能不是很多。





我实际上没有使用压缩算法,实现不好但也许它给你一个想法。



你想要的字符范围介于(十进制)32和126,这意味着您可以将字符映射到7位而不是8位。你可以为每8个字符删除1个字节。



这种方法的缺点是你总是无法获得可打印的ASCII字符。





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.


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;
}


您可以创建一个简单的替换方法,该方法返回与原始字符串至少有某种关系的字符串。在你的字符串中你有10个字母,5个数字,然后是9个字母。你的方法可以检查字符串中的每个字符,看看是否是字母,数字,符号或空格,然后返回如下字符串:L10N5L9



我做了类似的东西,虽然我不能为我的生活记住为什么。这只是一个想法,在许多情况下,它当然会产生比原始更长的字符串!但是对于你的例子,它更短。
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.


这篇关于制作字符串Shoreter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆