JavaScript实现地址字符串中Ap后续字符全大写的处理方案
地址指定部分转大写的实现方案
你的代码问题在于只截取了Ap之后的内容,没有保留前面的部分,也没对目标部分做大写转换。正确的思路是找到Ap的位置,把该位置及之后的内容转大写,再和前面的内容拼接。
修正后的代码
NewAddress = toggleCaseText(GetRecordsLF_Address); // 原地址:3513 Jones Drive Apt #500a const apIndex = NewAddress.indexOf('Ap'); if (apIndex !== -1) { // 截取Ap之前的部分 + Ap及之后的部分转大写 FinalAddress = NewAddress.substring(0, apIndex) + NewAddress.substring(apIndex).toUpperCase(); } else { // 没找到Ap的情况,直接用原地址 FinalAddress = NewAddress; }
代码说明
- 先用
indexOf('Ap')找到目标片段的起始位置apIndex - 判断索引是否有效(避免地址中没有
Ap的异常情况) - 用
substring(0, apIndex)获取Ap之前的原始内容 - 用
substring(apIndex).toUpperCase()把Ap及之后的所有内容转大写 - 拼接两部分得到最终目标地址:
3513 Jones Drive APT #500A
内容的提问来源于stack exchange,提问作者Codestarwik
相关产品推荐
相关产品推荐

