首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBScript/ASP Classic -解析和替换字符串中的多个项

问VBScript/ASP Classic -解析和替换字符串中的多个项
EN

Stack Overflow用户
提问于 2020-09-06 04:50:04
回答 1查看 247关注 0票数 1

我们在组织中有一个部门,在遗留应用程序中为其提供了一个长度有限的描述字段。他们为不同的短语想出了他们自己的缩写和缩写,我们想把它们转换回英语短语。

我希望有一种比我目前正在做的更好的方式来解析这些描述。

示例项目:

代码语言:javascript
复制
Part #:         Description
-------------------------------------
10001           17OH 2P 2G TRK 2YR
10002           22OH 2P 3G TRK 1YR

在这些示例描述中,我需要解析它们以提取以下项:

代码语言:javascript
复制
17OH = 17 Ohm
22OH = 22 Ohm
2P   = 2 Pole
2G   = 2-Gang
3G   = 3-Gang
TRK  = Track
2YR  = 2-Year Warranty
1YR  = 1-Year Warranty

遗留应用程序是用VBScript/Classic ASP编写的,因此下面是将它们转换为英语的一个示例。

代码语言:javascript
复制
descriptionOUTPUT = ""
descriptionRAW = "17OH 2P 2G TRK 2YR"
if instr(descriptionRAW,"17OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "17 Ohm "
if instr(descriptionRAW,"22OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "22 Ohm "
if instr(descriptionRAW,"2P") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Pole "
if instr(descriptionRAW,"2G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Gang "
if instr(descriptionRAW,"3G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "3 Gang "
if instr(descriptionRAW,"TRK") > 0 then descriptionOUTPUT = descriptionOUTPUT & "Track "
if instr(descriptionRAW,"2YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2-Year Warranty "
if instr(descriptionRAW,"1YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "1-Year Warranty "
response.write(descriptionOUTPUT)

输出将为:

代码语言:javascript
复制
17 Ohm 2 Pole 2 Gang Track 2-Year Warranty

如果我们只有12个左右的项目需要解析,这是很好的,但我们可能有数百个这样的项目,重复"if instr“语句肯定是非常低效的,即使我们每天只运行该代码几百次。有没有更好的方法使用数组或正则表达式,或者比上面更高效的方法?

EN

回答 1

Stack Overflow用户

发布于 2020-09-07 00:45:29

一种非常基本的方法是在asp中使用字典对象。要么对它们进行硬编码,要么从数据库中读取它们并进行循环比较。

https://www.w3schools.com/asp/asp_ref_dictionary.asp

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63758381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档