使用经典 asp 在特定位置从右侧插入空格

发布于 2025-01-03 20:46:19 字数 673 浏览 1 评论 0原文

我正在处理 SQL 2008 数据库中大约 60,000 个条目的一些英国邮政编码数据,并且需要操作包含邮政编码的字符串。

原始表单数据的收集未经验证,因此邮政编码以不同的格式保存,因此 CA12HW 也可以是 CA1 2HW(格式正确)。

英国邮政编码的长度和字母/数字混合各不相同,唯一的例外是所有代码均以空格数字字母结尾。

我只对查看代码的第一部分(即空格之前)感兴趣。因此,我正在考虑编写一段代码来执行以下操作:

1.检查右侧第四个空格。

2.如果没有空格,则向右插入四分之一。

3.在空格处分割字符串。

到目前为止,我有:

PostCode = "CA30GX"
SpaceLocate = InStr(PostCode, " ")
If SpaceLocate = 0 Then 'Postcode needs a space

如果唯一不变的是右边第四个应该有一个空格,我该如何插入一个?

插入空格后,我可以拆分代码以根据需要使用。

PostcodeArray = Split(Postcode, " ")

现在 PostcodeArray(0) 等于“CA3”,PostcodeArray(1) 等于“0GX”

任何帮助将不胜感激。

I'm working with some UK postcode data around 60,000 entries in a SQL 2008 database and need to manipulate a string containing a postcode.

The original form data was collected with no validation so the postcodes are held in different formats, so CA12HW can also be CA1 2HW (correctly formatted).

UK postcodes vary in length and letter / number mix with the only exception being all codes finish space number letter letter.

I am only interested in looking at the first part of the code ie before the space. Therefore I am looking at writing a piece of code that does the following:

1.Check for a space 4th from the right.

2.If there is no space insert one 4th right.

3.Split the string at the space.

So far I have:

PostCode = "CA30GX"
SpaceLocate = InStr(PostCode, " ")
If SpaceLocate = 0 Then 'Postcode needs a space

If the only constant is that there should be a space 4th right how do I insert one?

Once the space is inserted I can split the code to use as I need.

PostcodeArray = Split(Postcode, " ")

Now PostcodeArray(0) is equal to "CA3", PostcodeArray(1) is equal to "0GX"

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2025-01-10 20:46:19

您只需重新创建字符串即可:

PostCode = Left(PostCode, 3) & " " & Right(PostCode, 3)
PostcodeArray = Split(PostCode, " ")

编辑:

PostCode = Left(PostCode, Len(PostCode) - 3) & " " & Right(PostCode, 3)

You can just recreate the string:

PostCode = Left(PostCode, 3) & " " & Right(PostCode, 3)
PostcodeArray = Split(PostCode, " ")

Edit:

PostCode = Left(PostCode, Len(PostCode) - 3) & " " & Right(PostCode, 3)
夏の忆 2025-01-10 20:46:19

您可以使用 leftright 字符串函数来执行此操作:

newCode = left(Postcode,3) & " " & right(Postcode,len(Postcode)-3) 

You can use left and right string functions to do this:

newCode = left(Postcode,3) & " " & right(Postcode,len(Postcode)-3) 
暗恋未遂 2025-01-10 20:46:19

如果您只想要第一部分,则可以使用

PostCodeSector = Trim(Left(RTrim(PostCode), Len(RTrim(PostCode)) - 3))

修剪功能将忽略中间空格并删除任何其他空格

If you only want the first part you can just use

PostCodeSector = Trim(Left(RTrim(PostCode), Len(RTrim(PostCode)) - 3))

The trim functions will ignore the middle space and remove any other spaces

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文