C#相当于InStrRev [英] C# equivalent to InStrRev

查看:530
本文介绍了C#相当于InStrRev的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个多小时的寻找,我不能为我的生活弄清楚如何搜索右边一个字符串变量开始。我想要做的是让一个路径(右前的文件名)的最后一个文件夹,在VB6中我会做这样的事情:

 昏暗参考译文]字符串

S =C:\Windows\System32\Foo\Bar\

Debug.Print中秋节(S ,InStrRev(左(S,莱恩(S) - 1),\)+ 1)

下面是我试过到目前为止:

 字符串s =C:\\Windows\System32\ \Foo\\Bar\\; 

S = agencyName.Substring(s.LastIndexOf(\\)+ 1)


< DIV CLASS =h2_lin>解决方案

想必你想忽略最后 \ 中的字符串,因为你的VB代码检索所有,但的最后一个字符。你的C#代码是不工作,因为它是搜索整个字符串,找到 \ 作为字符串中的最后一个字符,导致您的子字符串返回什么。你必须告诉 LastIndexOf 在,就像你在VB中做了最后一个之前的字符开始。



我认为等同于您的VB代码如下:

  S = s.Substring(s.LastIndexOf(\\ ,s.Length  -  2)+ 1)


I have been searching for over an hour and I can not for the life of me figure out how to search a string variable starting on the right. What I want to do is to get the last folder of a path (right before the file name), In VB6 I would do something like this:

Dim s As String

s = "C:\Windows\System32\Foo\Bar\"

Debug.Print Mid(s, InStrRev(Left(s, Len(s) - 1), "\") + 1)

Here is what I tried so far:

string s = "C:\\Windows\System32\\Foo\\Bar\\";

s = agencyName.Substring(s.LastIndexOf("\\") + 1) 

解决方案

Presumably you want to ignore the last \ in the string, because your VB code is searching all but the last character. Your C# code isn't working because it's searching the whole string, finding the \ as the last character in the string, causing your substring to return nothing. You have to tell LastIndexOf to start at the character before the last one just as you did in VB.

I think the equivalent to your VB code would be:

s = s.Substring(s.LastIndexOf("\\", s.Length - 2) + 1)  

这篇关于C#相当于InStrRev的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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