欢迎献计献策:中文字符转换为英文字符

itti 2002-06-20 08:38:11
假如有这样一串字符:
"SELECT 姓名, 年龄, 性别 FROM students"

还有这样一个参考表
--------------------
姓名 name
年龄 age
性别 sex
.
.
.
-------------------

我想根据这个参考表把前面提到的字符串改写为:
"SELECT name, age, sex FROM students"

各位有什么办法或思路,花点时间把它贴出来.
...全文
177 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeeMaRS 2002-06-20
  • 打赏
  • 举报
回复
你是脚本语言+数据库吗?
如果是的话,问题就很简单了.以PHP举例:
1.从数据库中读出对照表,存入两个数组中.一个存要替换的文字,一个存要替换成的内容
2.用str_replace,按对照表中的要求进行替换.

下面是str_replace的资料,你看看吧,他支持search和replace都是数组的调用方式.可以完成你的要求.

str_replace
(PHP 3>= 3.0.6, PHP 4 )

str_replace -- Replace all occurrences of the search string with the replacement string
Description
mixed str_replace ( mixed search, mixed replace, mixed subject)

This function returns a string or an array with all occurences of search in subject replaced with the given replace value. If you don't need fancy replacing rules, you should always use this function instead of ereg_replace() or preg_replace().

In PHP 4.0.5 and later, every parameter to str_replace() can be an array.

If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string; then this replacement string is used for every value of search.

Example 1. str_replace() example

$bodytag = str_replace("%body%", "black", "<body text=%body%>");

This function is binary safe.

Note: str_replace() was added in PHP 3.0.6, but was buggy up until PHP 3.0.8.

See also ereg_replace(), preg_replace(), and strtr().
mountainfrank 2002-06-20
  • 打赏
  • 举报
回复
VB:大概这样,改一改就能用
dim original(3),target(3)
original(1)="姓名"
target(1)="name"
original(1)="年龄"
target(1)="age"
original(1)="性别"
target(1)="sex"
sql="SELECT 姓名, 年龄, 性别 FROM students"
for i=1 to 3
sql=Replace(sql,original(i),target(i))
next


itti 2002-06-20
  • 打赏
  • 举报
回复
我们应该考虑通用一点。

如果是这样的字符串呢?
"SELECT765 姓ok名, 年龄t, my性别FROMstudents"

改为
"SELECT764 姓ok名, aget, mysexFROMstudents"

我想我们应该判断找到的某一个字符是否为中文字符。。。

请继续!

ghost_lsw 2002-06-20
  • 打赏
  • 举报
回复
同意楼上的
oujinliang 2002-06-20
  • 打赏
  • 举报
回复
要写得简单些的话,把从字符串中提出的每个单词和参考表对照,没有对应的,就复制下来,否则替换。从字符串中提出单词应该好写吧?
bjay 2002-06-20
  • 打赏
  • 举报
回复
如果要完全的做,需要写一个函数。

1、找到"select"
2、进入一个循环 找“,”
3、每找到一个“,”做4、否则做8、
4、去掉各类分隔符记录在一个变量如A中
5、根据A查找参考表找到相应的项记录在变量B中
6、用变量B内容替换母串中的内容
7、返回2、
8、找到“FROM”串
9、将最后一个“,”与“FROM”之间内容再做一次4-6。
10、在“FROM”前加一个空格。
11、你可以使用改写好的母串了。
eion 2002-06-20
  • 打赏
  • 举报
回复
专门做一个替代函数,将对应的汉语名称替换为英文名称,不过要注意最好是全字匹配,否则容易出错
zhoukun666 2002-06-20
  • 打赏
  • 举报
回复
up
itti 2002-06-20
  • 打赏
  • 举报
回复
还有没有其他方法?

因为,如果对照表很庞大,达100000条记录,而我这个字符串中要替换的地方不到10处,按上面2位的方法是否浪费太多时间了。

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧