用户基于字符串的模板 [英] User based string template

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

问题描述

我有用户选择,我需要的模板,并在填写相关信息字符串的模板。

I have a string template that the user chooses, I need to take that template and fill in the requested information.

string templateString = "%author% - %title% (%year%)";



我能做到。载有()和.Replace(),但模板可能有大量投入或少量如:

I could do .Contains() and .Replace(), however the template could have a large amount of inputs or a small amount such as:

string templateString = "%author% - %publisher% - %isbn% - %asin% - %title% (%year%)";



所以要对每个选项做。载有()和.Replace(),用户可以选择似乎低效的,我所希望做的是找到与填充所需的信息的templateString一个更好的方式。任何帮助将不胜感激。这对数千种商品被使用。

So to do .Contains() and .Replace() for every option that the user can choose seems inefficient, what I am hoping to do is find a better way of populating the templateString with the requested information. Any help would be greatly appreciated. This would be used on thousands of items.

该计划本身需要然而,许多文件的用户输入(电子书,我们中的一些有成千上万的人),以及它的每一个重命名电子书基于该个人用户的模板,填充从电子书刮元数据的信息。

The program itself takes however many files the user inputs (ebooks, some of us have thousands of them), and it renames every ebook based off the individual user's template, filling the information with metadata scraped from the ebook.

推荐答案

我已经在这样做了过去。而不是使用。载我只需拨打 .Replace(图案,更换)的每可用选项。在一个典型的面向用户(如数据显示/输入)应用程序,用户将不会注意到任何放缓。

I've done this in the past. Instead of using .Contains I simply call .Replace(pattern, replacement) for every available option. In a typical user-oriented (eg. data display/entry) app, the user will NEVER notice any slowdown.

您真的应该veryify你试图优化性能之前有一个性能问题。你是最有可能浪费你的时间。

You really should veryify that you have a performance issue before trying to optimize for performance. You're most likely wasting your time.

的测试数据:
使用测试代码下面你可以看到,更换10个值25000书需要在2秒,其中大部分是通过写控制台(运行时降低到160毫秒下,如果你删除Console.WriteLine命令(),只是本本添加到列表与LT所致;字符串>())。 。似乎很接受我

TEST DATA: Using the test code below you can see that replacing 10 values in 25,000 "books" takes under 2 seconds, most of which is caused by writing to the console (runtime drops to under 160ms if you remove the Console.WriteLine() and just add the books to a List<String>()). Seems pretty acceptable to me.

String template = @"%index%.  %title% - %author% [%isbn%] - %year%";
            Dictionary<String, String> values = new Dictionary<String, String> { { "title", "A day in the life of..." }, { "author", "Joe S. Schmoe" }, { "year", "1945" }, { "isbn", "987-987-987-987-987" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "five", "5" }, { "six", "6" }, { "seven", "7" } };
            String output = string.Empty;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (Int32 index = 0; index < 25000; index++) {
                output = template;
                foreach (String key in values.Keys) {
                    output = output.Replace("%" + key + "%", values[key]);
                }
                output = output.Replace("%index%", index.ToString());
                Console.WriteLine(output);
            }
            watch.Stop();

            Console.WriteLine("Elapsed time (ms): " + watch.ElapsedMilliseconds.ToString());

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

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