在 Excel 中查找某个字符第 n 次出现的位置 – 3 种快速方法
Finding the nth occurrence of a specific character within a text string in Excel can be particularly useful in data analysis, where you might need to parse strings or extract information based on certain delimiters or patterns. For instance, let's find the 2nd or 3rd occurrence of the character "-" in a text string. I'll demonstrate straightforward techniques to efficiently accomplish this task.
Find nth occurrence of a character in text string with formula
You can craft a formula to find the nth occurrence of a character. Please do as this:
1. Enter or copy the following formula into a cell where you want to get the result:
=FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))
- A2: The cell that contains the string.
- -: The character you are looking for.
- 2: The 2nd occurrence you want to find, you can change it to 3,4…as you need.
2. Then, drag the formula fill down to fill the formula to other cells, and the 2nd position of the character – are displayed at once, see screenshot:
- 替代 replaces the nth occurrence of the character with a non-printable character (CHAR(1)).
- FIND searches for this non-printable character, giving the position of the nth occurrence.
Find nth occurrence of a character in text string with Kutools for Excel
If you're not a fan of using formula or VBA, you might consider a convenient alternative – Kutools for Excel. Within its Formula groups, you'll find a useful utility – 查找字符在字符串中第N个出现的位置 which quickly identifies and returns the nth position of any character in a cell.
安装 Kutools for Excel 后,单击“ 库工具 > 公式助手 > 公式助手 打开 公式助手 dialog box. Click a cell where you want to put the result. Then, do as follows:
- 选择 查找 从下拉列表中选择 公式类型 部分;
- 查找字符在字符串中第N个出现的位置 in 选择一个公式 部分;
- Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the 参数输入 部分;
- 最后点击 OK 按钮获取结果。
Find nth occurrence of a character in text stringl with User Defined Function
In this section, we'll explore how to create and use a UDF to find the nth occurrence of a character in Excel, providing a step-by-step guide to help you optimize your data handling.
- 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。
- 点击 插页 > 模块,然后将以下宏粘贴到“模块窗口”中。
Function FindN(sFindWhat As String, _ sInputString As String, N As Integer) As Integer 'Updateby Extendoffice Dim J As Integer Application.Volatile FindN = 0 For J = 1 To N FindN = InStr(FindN + 1, sInputString, sFindWhat) If FindN = 0 Then Exit For Next End Function
- Then, close the vba window. Go back to the worksheet, enter the following formula into a cell, and then drag the fill handle down to fill the formula to other cells, see screenshot:
=FindN("-",A2,3)
- Count the number of occurrences of a word in a column
- 如果您有一个包含一些重复值的列数据,如下面的屏幕快照所示,那么现在要做的就是计算该列中特定单词出现的次数。 现在,通过本教程,我将介绍一些技巧以在Excel中快速解决该问题。
- Replace first n character or nth occurrence of character with another
- 在Excel中,我们通常使用“查找和替换”功能查找和替换字符串,但是如果您属于以下情况之一,则“查找和替换”功能将无法为您提供帮助。
- Extract a nth character from a string
- In general, you may want to extract string after a specific character, but in this case, I want to extract the nth character from a string as below screenshot shown.
- Extract first/last n characters from string
- 例如,在每个单元格中都有一个包含长字符串的列表,您只想从每个字符串中提取前n个字符,就像每个字符串的前3个字符一样,现在您可以使用以下方法在Excel中解决它。
相关文章:
最佳办公生产力工具
🤖 | Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行 | 生成代码 | 创建自定义公式 | 分析数据并生成图表 | 调用 Kutools 函数... |
热门特色: 查找、突出显示或识别重复项 | 删除空白行 | 合并列或单元格而不丢失数据 | 不使用公式进行四舍五入 ... | |
超级查询: 多条件VLookup | 多值VLookup | 跨多个工作表的 VLookup | 模糊查询 .... | |
高级下拉列表: 快速创建下拉列表 | 依赖下拉列表 | 多选下拉列表 .... | |
列管理器: 添加特定数量的列 | 移动列 | 切换隐藏列的可见性状态 | 比较范围和列 ... | |
特色功能: 网格焦点 | 设计图 | 大方程式酒吧 | 工作簿和工作表管理器 | 资源库 (自动文本) | 日期选择器 | 合并工作表 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊过滤器 (过滤粗体/斜体/删除线...)... | |
前 15 个工具集: 12 文本 工具 (添加文本, 删除字符,...) | 50+ 图表 类型 (甘特图,...) | 40+ 实用 公式 (根据生日计算年龄,...) | 19 插入 工具 (插入二维码, 从路径插入图片,...) | 12 转化 工具 (小写金额转大写, 货币兑换,...) | 7 合并与拆分 工具 (高级组合行, 分裂细胞,...) | ... 和更多 |
使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...
Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松
- 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
- 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
- 每天将您的工作效率提高50%,并减少数百次鼠标单击!