如何获取字符串的特定部分 [英] How to get a particular part of a String

查看:120
本文介绍了如何获取字符串的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Excel中编写一个宏,我需要从一个String中获取一个子字符串。
就像这样。

 〜/ tester / test / hai / bye 
〜/ stack / overflow / hai / bye

在上述情况下,我需要从第一个字符串测试器从第二个。我尝试使用 InStr ,但没有用。任何人都可以帮忙?

解决方案

您可以使用InStr和Mid函数来执行此操作。
使用InStr函数查找/,然后使用Mid获取您感兴趣的字符串的一部分。



尝试这样:

 函数ExtractFirstPartOfPath(path as String)as String 

Dim first,second as Integer

first = InStr(path,/)
second = InStr(first + 1,path,/)

ExtractFirstPartOfPath = Mid(path,first + 1 ,第二 - 第一 - 1)

结束函数

此函数将产生期望的结果。


I am writing a macro in Excel where I need to get a substring from a String. It's like this.

~/tester/test/hai/bye
~/stack/overflow/hai/bye

In the above cases I need to take the String tester from the first one and stack from the second one. I tried using InStr but it's not useful. Can anyone help?

解决方案

You can do this using the InStr and Mid functions. Use the InStr function to find the occurrences of the / and then use Mid to get the part of the string that you are interested in.

Try this:

Function ExtractFirstPartOfPath(path as String) as String

  Dim first, second as Integer

  first = InStr(path, "/")
  second = InStr(first + 1, path, "/")

  ExtractFirstPartOfPath = Mid(path, first + 1, second - first - 1)

End Function

This function will produce the desired results.

这篇关于如何获取字符串的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆