vb.net VB InStr 等效于 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31920525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:23:22  来源:igfitidea点击:

VB InStr Equivalent in c#

c#vb.net

提问by Deke

I am not sure why I get the result of 0, which is the correct value of a

我不知道为什么我得到的结果是0,这是正确的值a

I have in VB

我在 VB

Dim searched As String = "<results>" & vbCrLf & "<field name=\""FID\""/>" & vbCrLf & "<field name=\""StartFID\""/>" & vbCrLf & "<field name=\""Vertex1\""/>" & vbCrLf & "<field name=\""Vertex2\""/>" & vbCrLf & "<field name=\""Slope\""/>" & vbCrLf & ""

Dim sought As String = "<rs FID=\""87\"" StartFID=\""87\"" Vertex1=\""29\"" Vertex2=\""30\"" Slope=\""-1\""/>"

Dim a As Integer = InStr(searched, sought)

What I would like to do, is get the same result of a == 0when converted to c#.

我想做的是获得与a == 0转换为 c# 时相同的结果。

I have tried

我试过了

int a = String.Compare(searched, sought);
int a = String.IndexOf(searched, sought);
int a = String.Equals(searched, sought);

回答by CodeCaster

Strings in C# are zero-indexed. If a.IndexOf(b)returns 0, then string bis present in string aat position 0.

C# 中的字符串是零索引的。如果a.IndexOf(b)返回0,则字符串b出现在字符串a中的位置0

If the sought string is not in the input, IndexOf()returns -1.

如果搜索的字符串不在输入中,则IndexOf()返回-1

回答by dasblinkenlight

One difference between InStrand IndexOf's behavior is that InStrreturns zero when the string is not found, while IndexOfreturns -1. Using IndexOfis the idiomatic way of searching for substrings in C#, and the correct method to use in your situation.

InStrIndexOf的行为之间的一个区别是InStr当找不到字符串时返回零,而IndexOf返回 -1。UsingIndexOf是在 C# 中搜索子字符串的惯用方法,也是在您的情况下使用的正确方法。

If you would like to use InStrdirectly, you could do it by referencing Microsoft.VisualBasicassembly, and then calling InStras a static method of the Stringsclass:

如果想InStr直接使用,可以通过引用Microsoft.VisualBasic程序集,然后InStr作为类的静态方法调用Strings

int a = Strings.InStr(searched, sought);

回答by Deke

Works... Thanks Carsten

作品...谢谢卡斯滕

int b = searched.IndexOf(sought);

b = 0

乙 = 0

I see the problem I had now... when I move the string to a variable that calculates the string I get the error. I wasn't sure whether I had the correct use of InStr.

我看到了我现在遇到的问题......当我将字符串移动到计算字符串的变量时,我得到了错误。我不确定我是否正确使用了 InStr。

回答by taha mosaad

Simply You can use IndexOf()+1 to return 0 instead of -1

只需您可以使用 IndexOf()+1 返回 0 而不是 -1

int a = searched.IndexOf(sought)+1;

it work for me hop it help

它对我有用,希望有帮助

回答by CodeTherapist

The three methods are all for different purposes and do not the same. That is why they have different names..

这三种方法都是用于不同的目的,并不相同。这就是为什么他们有不同的名字..

int a = String.IndexOf(searched, sought);

This is the method that you should use and is exactly for that purpose. Alternatively you could use string.Contains().

这是您应该使用的方法,并且正是为此目的。或者,您可以使用string.Contains().

int a = String.Compare(searched, sought);

Compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.

比较两个指定的 String 对象,忽略或尊重它们的大小写,并返回一个整数,指示它们在排序顺序中的相对位置。

bool a = String.Equals(searched, sought);

Determindes equality of two strings.

确定两个字符串是否相等。

回答by tichra

String.IndexOfshould work, but if you truly want to use InStr, you can still do it. Just add a reference to Microsoft.VisualBasic.dll, and a "using Microsoft.VisualBasic;" at the top of your file, and you can use Strings.InStr: Like Strings.InStr(searched, sought)

String.IndexOf应该可以工作,但如果你真的想使用 InStr,你仍然可以这样做。只需添加对 Microsoft.VisualBasic.dll 的引用和“使用 Microsoft.VisualBasic;” 在文件的顶部,您可以使用Strings.InStr:像Strings.InStr(searched, sought)

回答by loowool

You can use the searched.Contains(sought)

您可以使用 searched.Contains(sought)