English 中文(简体)
将数据领域纳入案文的最佳方式?
原标题:Best way to embed data fields in text?
  • 时间:2010-02-18 23:27:32
  •  标签:
  • text
  • field

什么是将数据字段嵌入文本以便轻松检索,同时不影响可读性(太多)的最佳方法?

For instance, I could do something like "Some instructions here, followed by @firstname:John @lastname:Smith ", and then write code to parse out the fields. Then I could begin to work out the problems, like embedded @-signs.

但是,这已经做了几千次,我希望有人能向我指出一种已解决的模式。

问题回答

Gonna雇用了一位基于你的问题的学者,让我知道我是否误解了。

你需要选择一些容易被正则表达式和/或变量替换捕捉的东西。它也应该是你在文本中不经常使用的字符。

像是“你好 ${firstName},你今天怎么样?”,或者更简单地说“你好 {0},最近怎么样?”

有些语言已经内建了对此的支持,因此取决于您的环境。

正如Mike所说,它取决于环境。在.NET中,你有"Hello {0}",几种语言会使用"Hello %s",使用MooTools的JavaScript会使用"Hello {name}"等等。"{0}"和"%s"都取决于参数的顺序(这可能是一个缺点)。

如果你有兴趣学习如何自己实现它,这里是一个使用JavaScript正则表达式的例子:

// Variables should be an object at the current form:
// {name:  John , age: 13}
function substitute(string, variables)
{
    var result = string.replace(/\?{([^{}]+)}/g, function(match, name) {
        if(match.charAt(0) ==  \ ) return match.slice(1); //If there was a backslash before the first {, just return it as it was.
        return (variables[name] != undefined) ? variables[name] :   ;
    });
    return result;
}

将其翻译为中文:将其移植到支持正则表达式的其他语言中不应该太难。

或许是一只黑板,但如果这只是一个简单的要求,要求为电文或国际化(etc)形成某种文本,你可能会滥用Sting.format(java,大概是这些窗户语言有类似之处),这要么是空洞或阵列参数,如:

String text = "Hello %s, do you still live at %s?";
String[] replacements = {
    user.name(),
    user.address()
};

String formatted = String.format(text, replacements);

当然,唯一的问题是你不能指定参数的顺序,只能指定它们出现的位置。这可能是一个问题,也可能不是一个问题,但如果不是的话,这可能是最快的解决方案。





相关问题
Which non-bold UTF-8 iPhone SDK font can I use here?

Problem: I create a UILabel with this font: label.font = [UIFont systemFontOfSize:15.0f]; Now this system font is bold, but I want to display pretty small text, and the text may also contain very ...

Limiting file upload type

Simple question. Is there a way to only allow txt files upon uploading? I ve looked around and all I find is text/php, which allows PHP. $uploaded_type=="text/php

Extract everything from PDF [closed]

Looking for solution to extract content from a PDF file (using console tool or a library). It will be used on server to produce on-line e-books from uploaded PDF files. Need to extract following ...

Flash & external form fields

Does anybody know if this is possible? I am trying to create a flash movie that will show / preview what I am typing into a field in a normal HTML form. The call to update the flash movie would most ...

Avoiding escape sequence processing in ActionScript?

I need to mimic C# functionality of the @ symbol when it precedes a string. @"C:AFilePath" for example What is the best way to do this? Also there are some sites that will escape larger ...

iPhone: Draw rotated text?

I want to draw some text in a view, rotated 90°. I m pretty new to iPhone development, and poking around the web reveals a number of different solutions. I ve tried a few and usually end up with my ...

Numbering in jQuery

How could I change the text below so that the text within it has a number appended to it. <div class="right">This is some text</div> <div class="right">This is some text</div>...

热门标签