Need help using IndexOf()
-
I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing.
private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; }
This should be simple right? Robert -
I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing.
private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; }
This should be simple right? RobertRbledwards wrote: I don't know what I'm doing Neither do I, please explain a bit more (scenario etc), and paste less code. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
Rbledwards wrote: I don't know what I'm doing Neither do I, please explain a bit more (scenario etc), and paste less code. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
leppie wrote: and paste less code Oops, was asuming more was better. I just want to know if my string exists in the array. In the debugger I can see that it does exist. For example. I have a column named Amount billed. The strings in my array are {"Amount","Cost"}. I am using IndexOf to search the ColumnName for one of the strings in the array. It doesn't find "Amount" for some reason. Thanks Robert
-
I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing.
private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; }
This should be simple right? Robert -
It should work. maybe it something else your doing int iCOunt = -1; foreach ( string s in strCurrency ) { iCount++; if ( s.IndexOf(ColumnName) != -1 ) return iCount; }
No, I had it backwards. I was looking to see if s existed in ColumnName. so I needed to put ColumnName.IndexOf(s) instead of s.IndexOf(ColumnName). Such is life. Robert
-
I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing.
private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; }
This should be simple right? RobertI think what you're asking is to see if a particular string is in an array of strings. Use this:
public bool ExistsInArray(string s, string[] sList)
{
foreach (string item in sList)
{
if (item == s) return true;
}
return false;
}In other words, check all strings in the list to see if the one I want is there. If it is, exit immediately I find it, returning true. If I fall through the loop, having checked all strings in the list, return false. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
I think what you're asking is to see if a particular string is in an array of strings. Use this:
public bool ExistsInArray(string s, string[] sList)
{
foreach (string item in sList)
{
if (item == s) return true;
}
return false;
}In other words, check all strings in the list to see if the one I want is there. If it is, exit immediately I find it, returning true. If I fall through the loop, having checked all strings in the list, return false. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
but if you do "==" arent you just comparing the instances and not the value s.Equals(ColumnName)
No. The == operator for strings is overridden to compare the actual string contents. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
No. The == operator for strings is overridden to compare the actual string contents. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.