查找字符串数组中String的索引 [英] Find index of a String in a String Array

查看:107
本文介绍了查找字符串数组中String的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从一个名为 inputstring 的字符串生成一个名为 inputstringarray 的字符串数组,方法是在出现字符'的时间间隔分割字符串。



I have generated a String Array named inputstringarray from a string named inputstring by splitting the string at intervals where the character "'" appears.

string[] inputstringarray = inputstring.Split('\'');





假设我使用的字符串如



Say i have used a string like

"'44'Is'GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType '1'"





所以,这将分成一个字符串数组,如





So, this will be parted to a string array like

inputstringarray[0]=""
inputstringarray[1]=44
inputstringarray[2]=Is
inputstringarray[3]=GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType 
inputstringarray[4]=1
inputstringarray[5]=""





如何在字符串数组中找到字符串为Is的Array元素的索引?



如果我使用不同的字符串,那么显然索引会改变。 String可用的 IndexOf 函数不适用于String Array。



如何在字符串数组中找到具有特定字符串值的Array元素的索引?



How do i find the index of the Array element having the string "Is" in the String Array ?

If i am using a different string, then obviously the index will change. The IndexOf function available to String is not available for String Array.

How can i find the index of the Array element having a particular string value in the String Array ?

推荐答案



试试这个。

Hi,
try this.
int TextIndex = Array.FindIndex(inputstringarray, m => m == "Is");//replace "Is" with your other desired text



希望它可以帮到你。


Hope it helps you.


好的,我们有数组静态成员,我们有 for 循环...我们缺少什么?对了,Linq的表情! :)



您可以尝试以下方法:



Ok, we have Array static member, we have for cycle... what are we missing? Right, Linq expressions! :)

You could try the following:

int[] indexes = inputstringarray.Select((str, index) => str.Equals("Is", StringComparison.InvariantCulture) ? index : -1).Where(iElement => iElement >= 0).ToArray();





这将为您提供包含所有Is字符串包含的索引的数组。



This gives you the array with indexes of all "Is" string inclusions.


I' d建议您使用String.Split方法的形式删除空条目:
I'd like to suggest you use the form of the String.Split method that removes "empty entries:"
// defining the char[] used to Split in advance 
// can save memory if you are using it repeatedly
private char[] splitCharAry = new char[] { '\'' };

string str = "'44'Is'GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType '1'";

string[] sAry = str.Split(splitCharAry, StringSplitOptions.RemoveEmptyEntries);

一旦你有了清理字符串[],.NET就会给你带来各种各样的找到一个项目的方法,其中一些你已经在你的问题的其他回答中看到过:IndexOf,Array.Find,Array.FindIndex等。在.NET的更高版本中,这些函数实际上正在转换为Linq打电话......你只是幸免必须写出类型。



如果我需要经常搜索,我可能会为了方便而编写这样的函数:

Once you have your "cleaned up" string[], .NET hands you a variety of ways to find an item, some of which you've already seen in the other responses to your question: IndexOf, Array.Find, Array.FindIndex, etc. In later versions of .NET those functions are actually being transformed for you into Linq calls ... you are just "spared" having to write out the Types.

If I needed to search frequently, I might write a function like this for convenience:

// This method is an O(n) operation, where n is the Length of array.
private int findArrayStringElement(string searchStr, bool UseContains, string[] strAry)
{
    return (UseContains) 
      ?
        Array.FindIndex(strAry, element => element.Contains(searchStr))
        :
        Array.FindIndex(strAry, element => element == searchStr);
}

因此,我可以搜索与我想要找到的字符串完全匹配的内容,或搜索包含我试图找到的字符串的项目。如果不匹配,所有上述方法都返回整数值#-1。



要记住的一件重要事情是搜索数组未排序项目总是(可能)需要迭代所有数组元素直到找到项目:使用Linq表达式不会比使用for更快-loop,或foreach,iterator!



在计算机科学术语中,在未排序的列表/数组中查找项目将是O(#n)操作,其中#n是数组/列表的长度。但是,如果数组/列表已排序,那么您可以(可能会取决于列表/数组的长度等因素)获得更快的性能。



在.NET中,在排序列表/数组上使用BinarySearch工具可以获得类似O(log #n)结果的内容,其中#n是数组/列表的长度。

So I could search either for something that absolutely matched the string I wanted to find, or for an item that "contained" the string I was trying to find. All the above methods return the integer value #-1 if there is not match.

An important thing to keep in mind is that searching an Array that is not sorted for an item is always going to (potentially) require iterating over all the Array elements until the item is found: using Linq expressions are not going to be any faster than using a for-loop, or foreach, iterator !

In computer-science terms, finding an item in an unsorted list/array is going to be an O(#n) operation, where #n is the Length of array/list. However, if the array/list is sorted, then you can, potentially (depending on factors like the length of the list/array), get much faster performance.

In .NET using the BinarySearch facility on a sorted list/array can get you something like O(log #n) results where #n is the length of the array/list.

Array.Sort(sAry);

// test
int sResult = Array.BinarySearch(sAry, "Is");

由于没有任何东西可以免费,因此考虑多少一种成本是合乎逻辑的。这里:[ ^ ] Microsoft描述了.NET BinarySearch工具如何工作,以及如何针对不同大小的搜索目标使用不同的搜索方法(有关详细信息,请参阅备注部分)。从技术上讲,这里实现的排序MS类型是一个Introsort [ ^ ] ,混合排序。



所以我认为你应该考虑的问题是:排序;还是不排序?



如果要搜索多次分割字符串的相同结果,那么,是的,我认为您可能会从排序中受益。如果您正在处理拆分其值经常变化的字符串,那么也许您将无法从排序中获益。



对于包含少量条目的数组:嗯,我认为你必须进行试验,看看有什么好处。

Since "nothing comes for free," it's logical to consider "how much" a sort "costs." Here: [^] Microsoft describes how the .NET BinarySearch facility works, and how, for different size search targets it uses different methods for the search (see the 'Remarks section for details). Technically, the type of Sort MS implemented here is an Introsort [^], a "hybrid sort."

So the question I think you should consider is: "to Sort; or not to Sort ?"

If you are going to search the same result of splitting a string many times, then, yes, I think you will probably benefit from sorting. If you are dealing with splitting a string whose value changes frequently, then perhaps you'll get no benefit from sorting.

For an array with a few entries: well, I think you'll have to experiment and see what the benefits are.


这篇关于查找字符串数组中String的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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