本文是小编为大家收集整理的关于如何用多个逗号替换字符串中的最后一个逗号“and”或“&”?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
大家好
谢谢大家帮助别人.
我有一个非常简单的问题,我们如何才能将字符串中的最后一个逗号替换为 ''and'',它有多个逗号?
假设我们有一个句子"我喜欢水果中的香蕉、橙子、葡萄、芒果".我已经尝试过 Replace() 函数,但是它用 ''and'' 代替了每个逗号,如果只有两个项目,这很好.但我希望这句话必须转换为"我喜欢水果中的香蕉、橙子、葡萄和芒果",当有两个以上的项目时只替换最后一个逗号,如果只有两个项目,则替换唯一的逗号项目.
希望我的问题可以理解.
推荐答案
"我喜欢水果中的香蕉、橙子、葡萄和芒果"
对
"我喜欢水果中的香蕉、橙子、葡萄、和芒果"
对于大多数人来说,最后一个命令应该保留在最后一个和/或/连接词之前的列表中.
----------
COMMAS----------逗号的标准用法是分隔一系列项目:"猫、狗和沙鼠".关于"and"之前是否需要最后一个逗号,权威人士存在分歧.当你必须取悦他们时,遵循老师、编辑或老板推荐的风格;但如果你是一个人,我建议你使用最后的逗号.它通常会消除歧义.
因此,除非您有不喜欢最后逗号的老师或编辑,否则我不会这样做.
这很像一个家庭作业问题.
因此,我将描述逻辑,而不是简单地发布我用来解决问题的代码.
至于找句末逗号:
使用 instr() 函数和 len() 函数.
做循环
instr() 查找第一次出现
如果返回非零,则将返回的位置加一,如果 >len 然后返回的位置是您的最后一个逗号,否则使用新的增量作为您的 instring 的起点.
如果返回的位置为零并且最后返回的小于字符串的 len() 则最后返回的位置是最后一个逗号
在最后一个字符串中使用可选的起始位置来使用替换功能
替换函数
请发布您提出的代码...届时我将很乐意帮助优化它.
我错过了什么吗?我认为您可以使用 instrRev() 函数来定位最后一个逗号的位置?
不,可以使用 instrRev()... 我忘了那个,因为它对现场来说是相当新的(无论如何对我来说……猜想它首先是与 ACC2000 一起提供的;但是直到最近我才开始真正考虑它,因为我公司的所有旧 ACC97 数据库最终都被迫使用转到 ACC2010(邪恶的笑声).)
问题描述
Hello everybody
Thank you all for helping others.
I have a very simple question that how can we replace only last comma with ''and'' in string, which has got multiple commas?
Suppose we have a sentence "I like Banana, Orange, Grapes, Mango in fruits". I have tried Replace() function but that put ''and'' in place of every comma, which is fine if there are only two items. But I want that this sentence must be converted to "I like Banana, Orange, Grapes and Mango in fruits", replacing only the last comma, when there are more than two items and replace the only comma in case when there are only two items.
Hope I have made my question understandable.
推荐答案
"I like Banana, Orange, Grapes and Mango in fruits"
vs
"I like Banana, Orange, Grapes, and Mango in fruits"
For most people, the last comman should remain in the list prior to the final and/or/conjuctive.
----------
COMMAS----------A standard use for commas is to separate the items in a series: “cats, dogs, and gerbils.” Authorities differ as to whether that final comma before the “and” is required. Follow the style recommended by your teacher, editor, or boss when you have to please them; but if you are on your own, I suggest you use the final comma. It often removes ambiguities.
So unless you have a teacher or editor that doesn''t like the final comma, I would not be doing this.
This is very much like a homework problem.
So instead of simply posting the code I used to solve something along the same lines, I''ll describe the logic.
As for finding the final comma in a sentence:
use the instr() function and the len() function.
do Loop
Instr() Find the first occurance
If the returned is non-zero then add one to the position returned, if > len then the position returned is your last comma else use the new increment as the starting point for your instring.
If the position returned is zero and the last returned was less than the len() of the string then the last position returned was the final comma
use the replace function using the optional start postion at the last instring
Replace Function
Please post the code you have come up with... at that point I''ll be happy to help optimize it.
Am I missing something? I would think you could use the instrRev() function to locate the position of the last comma?
nope, instrRev() could be used... I forget about that one as it''s fairly new to the scene (well for me anyway... guess it was first available with ACC2000; however I didn''t start really considering it until just recently as all of the old ACC97 databases at my company have finally been forced to move to ACC2010 (evil laugh).)