I have an array of strings like {"ABC_DEF_GHIJ", "XYZ_UVW_RST", ...} and want to search if my array contains a string partially matching "ABC_DEF". It should return either index (0) or the string itself ("ABC_DEF_GHIJ").
I tried:
int index = Array.IndexOf(saGroups, "ABC_DEF");
But it returns -1.
You can use extension methods and Lambda expressions to achieve this.
If you need the Index of the first element that contains the substring in the array elements, you can do this...
int index = Array.FindIndex(arrayStrings, s => s.StartsWith(lineVar, StringComparison.OrdinalIgnoreCase)) // Use 'Ordinal' if you want to use the Case Checking.
If you need the element's value that contains the substring, just use the array with the index you just got, like this...
string fullString = arrayStrings[index];
Note: The above code will find the first occurrence of the match. Similary, you
can use Array.FindLastIndex() method if you want the last element in the array
that contains the substring.
You will need to convert the array to a List<string> and then use the ForEach extension method along with Lambda expressions to get each element that contains the substring.
Try this:
string[] strArray = { "ABC_DEF_GHIJ", "XYZ_UVW_RST" };
string SearchThisString = "ABC_DEF";
int strNumber;
int i = 0;
for (strNumber = 0; strNumber < strArray.Length; strNumber++)
{
i = strArray[strNumber].IndexOf(SearchThisString);
if (i >= 0)
break;
}
Console.WriteLine("String number: {0}",strNumber);
You can also use Contains:
string [] arr = {"ABC_DEF_GHIJ", "XYZ_UVW_RST"};
for (int i = 0; i < arr.Length; i++)
if (arr[i].Contains("ABC_DEF"))
return i; // or return arr[i]
Simply loop over your string array and check every element with the IndexOf method if it contains the string you want to search for. If IndexOf returns a value other than -1 the string is found in the current element and you can return its index. If we can not find the string to search for in any element of your array return -1.
static int SearchPartial(string[] strings, string searchFor) {
for(int i=0; i<strings.Length; i++) {
if(strings[i].IndexOf(searchFor) != -1)
return i;
}
return -1;
}
See https://ideone.com/vwOERDfor a demo
Related
I have trouble with the value that arraylist returns.
I created a two-dimensional Arraylist that includes string arrays, when I try to get actual value of string arrays, I get System.String[] as output
instead of actual value of the arrays.
Why do I get System.String() as outuput?
Here is my code :
static void Main(string[] args)
{
string[] employee_1 = { "Employee1" };
string[] employee_2 = { "Employee2" };
ArrayList main_array = new ArrayList();
main_array.Add(employee_1);
main_array.Add(employee_2);
for (int i= 0; i < 2; i++)
{
Console.WriteLine(main_array[i]);
}
Console.ReadKey();
}
That is because when retrieving the items from an ArrayList what you are getting is a reference to an object instead of the actual type. Thus when printing it is calls the ToString of object (which prints the type's name) and not the string you want. In addition when printing a collection (like you are doing in your WriteLine command) you need to specify how to so do because it's default implementation is also as object's. You can use string.Join to print all items in the nested array.
To correct this first cast to string[] ( as string[]) and then print, or better still is to work with the list object instead: List<string[]>. To read more see:
ArrayList vs List<> in C#
What is the difference between an Array, ArrayList and a List?
So:
var mainCollection = new List<string[]> { new string[] { "Employee1" },
new string[] { "Employee2" }};
for (int i = 0; i < mainCollection.Count; i++)
{
Console.WriteLine(string.Join(", ", mainCollection[i]));
}
Console.ReadKey();
As a side note do not loop to 2 but instead by the number of items in the collection. See: What is a magic number, and why is it bad?
This is the default behaviour of ToString() function for an array.
To print the employees names, you need to iterate the array:
for (int i= 0; i < 2; i++)
{
foreach(string employee in main_array[i]) {
Console.WriteLine(employee);
}
}
The problem is here, ArrayList always take input as an object. So, when you add string array at an ArrayList, it take an object instead of string array.
So, you should convert this object to string array and print it.
like below:
for (int i = 0; i < 2; i++)
{
Console.WriteLine(string.Join(", ", main_array[i] as string[]));
}
or may use below(both are same):
for (int i = 0; i < 2; i++)
{
foreach (string employee in main_array[i] as string[])
{
Console.WriteLine(employee);
}
}
Because you have string[] type elements in outer ArrayList.
The best examples to enumerate are in answers above. If you realy need string content of string to be wtitten you should use
Console.WriteLine(((string[])main_array[i])[0]);
main_array is a two position ArrayList, each position contains a string[] this is, a string array.
If you use main_array[0], this is a reference to employee_1 which is a string array.
You should be able to reference the array strings by using main_array[0][0]
Try this:
foreach(var strArray in main_array)
{
// strArray is a string array
foreach(var stir in strArray)
{
// star is a string
Console.WriteLine(str);
}
}
I have an array of strings. I want to compare with the string which I am getting from JSON. Comparision should be like this.
For example:
If Account name in one string is Google and in other it is Google Inc, then since Google is part of the Google Inc company name, It should get matched. Otherwise not.
Code which I have written:
for (int i = 0; i < accountCount; i++)
{
//// account is found in the array
name[i] = account.Entities[i].Attributes["name"].ToString();
if (name[i] == message.Current.org_name)
{
flag = 1;
c.CreateOpportunity(message);
break;
}
}
//// account is not found in the array
if (flag == 0)
{
c.CreateAccount(message);
c.CreateOpportunity(message);
}
Try to use Contains function instead :
for (int i = 0; i < accountCount; i++)
{
//// account is found in the array
name[i] = account.Entities[i].Attributes["name"].ToString();
if (name[i].Contains(message.Current.org_name)
|| message.Current.org_name.Contains(name[i]))
{
flag = 1;
break;
}
}
//// account is not found in the array
if (flag == 0)
c.CreateAccount(message);
c.CreateOpportunity(message);
You can use Contains for a case sensitive search
or IndexOf to speficify more options with comparison criteria
However for the fun of it we can use Any on an array or list
Note : none of the above check for null
Contains
var org = message.Current.org_name;
var found = account.Entities.Any( // does any entity contain org ?
x => x.Attributes["name"] // Get Attribute
.ToString() // Convert it to string if needed
.Contains(org));
if (found)
{
c.CreateAccount(message);
}
c.CreateOpportunity(message);
if you would like a case insensative search you can use String.IndexOf
IndexOf
var found = account.Entities.Any( // does any entity contain org ?
x => x.Attributes["name"] // Get Attribute
.ToString() // Convert it to string if needed
.IndexOf(org, StringComparison.OrdinalIgnoreCase) >= 0);
References
String.Contains -
String.Contains Method (String)
String.IndexOf -
String.IndexOf Method (String, StringComparison)
Coparison type - StringComparison Enumeration
I didn't get the problem - I was trying to do a simple action:
for(i = x.Length-1, j = 0 ; i >= 0 ; i--, j++)
{
backx[j] = x[i];
}
Both are declared:
String x;
String backx;
What is the problem ? It says the error in the title...
If there is a problem - is there another way to do that?
The result (As the name 'backx' hints) is that backx will contain the string X backwards.
P.S. x is not empty - it contains a substring from another string.
Strings are immutable: you can retrieve the character at a certain position, but you cannot change the character to a new one directly.
Instead you'll have to build a new string with the change. There are several ways to do this, but StringBuilder does the job in a similar fashion to what you already have:
StringBuilder sb = new StringBuilder(backx);
sb[j] = x[i];
backx = sb.ToString();
EDIT: If you take a look at the string public facing API, you'll see this indexer:
public char this[int index] { get; }
This shows that you can "get" a value, but because no "set" is available, you cannot assign values to that indexer.
EDITx2: If you're looking for a way to reverse a string, there are a few different ways, but here's one example with an explanation as to how it works: http://www.dotnetperls.com/reverse-string
String is immutable in .NET - this is why you get the error.
You can get a reverse string with LINQ:
string x = "abcd";
string backx = new string(x.Reverse().ToArray());
Console.WriteLine(backx); // output: "dcba"
String are immuatable. You have convert to Char Array and then you would be able to modify.
Or you can use StringBuilder.
for example
char[] wordArray = word.ToCharArray();
In C# strings are immutable. You cannot "set" Xth character to whatever you want. If yo uwant to construct a new string, or be able to "edit" a string, use i.e. StringBuilder class.
Strings are immutable in C#. You can read more about it here: http://msdn.microsoft.com/en-us/library/362314fe.aspx
Both the variables you have are string while you are treating them as if they were arrays (well, they are). Of course it is a valid statement to access characters from a string through this mechanism, you cannot really assign it that way.
Since you are trying to reverse a string, do take a look at this post. It has lot of information.
public static string ReverseName( string theName)
{
string revName = string.Empty;
foreach (char a in theName)
{
revName = a + revName;
}
return revName;
}
This is simple and does not involve arrays directly.
The code below simply swaps the index of each char in the string which enables you to only have to iterate half way through the original string which is pretty efficient if you're dealing with a lot of characters. The result is the original string reversed. I tested this with a string consisting of 100 characters and it executed in 0.0000021 seconds.
private string ReverseString(string testString)
{
int j = testString.Length - 1;
char[] charArray = new char[testString.Length];
for (int i = 0; i <= j; i++)
{
if (i != j)
{
charArray[i] = testString[j];
charArray[j] = testString[i];
}
j--;
}
return new string(charArray);
}
In case you need to replace e.g. index 2 in string use this (it is ugly, but working and is easily maintainbable)
V1 - you know what you want to put their. Here you saying in pseudocode string[2] = 'R';
row3String.Replace(row3String[2], 'R');
V2 - you need to put their char R or char Y. Here string[2] = 'R' if was 'Y' or if was not stay 'Y' (this one line if needs some form of else)
row3String.Replace(row3String[2], row3String[2].Equals('Y') ? 'R' : 'Y');
When I try to copy arrays into a jagged array. My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.
static char[][] split_string(char[] str)
{
int length = count_words(str);
char[][] sentence = new char[length][];
int start = 0;
int end = 0;
int word = 0;
int i = -1;
while (i != str.Length)
{
i++;
if (str[i]==' ')
{
end = i-1;
char[] aux_array = substring(str, start, end);
//issue
aux_array.CopyTo(sentence[word], 0);
//alternative (not working either)
/*
for(int j=0; j<aux_array.Length;j++)
{
sentence[word][j] = aux_array[j];
}
*/
while (str[i]==' ')
{
i++;
}
word++;
start = i;
}
}
return sentence;
}
For information,
substring if of the form: substring(array, int, int) -> array
count_word is of the form: count_word(array) -> int
My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.
Then just put them into array
//...
sentence[word] = substring(str, start, end);
Note that the jagged array elements are null by default and you didn't allocate them, so you probably are getting null reference exception. If you really need to do a copy of the returned array, then the easiest way is to use Array.Clone method like this
sentence[word] = (char[])substring(str, start, end).Clone();
It is easier to work with strings and not raw char arrays but I assume it is with intention that you have decided to use char arrays.
One way to simplify your code is to build the char array as you go instead of preallocating it. .NET arrays have fixed size but List<T> allows you to grow a collection of items.
You can also change your function into an iterator block to simplify it further. When a word is complete you yield return it to the caller.
IEnumerable<char[]> SplitString(char[] str) {
var word = new List<char>();
foreach (var ch in str) {
if (ch == ' ') {
if (word.Count > 0) {
yield return word.ToArray();
word = new List<char>();
}
}
else
word.Add(ch);
}
if (word.Count > 0)
yield return word.ToArray();
}
This function will not return an array so if you want an array of arrays (jagged array) you need to use ToArray():
var str = "The quick brown fox jumps over the lazy dog".ToCharArray();
var result = SplitString(str).ToArray();
This code will correctly handle multiple spaces and spaces in the beginning and end of the source string.
I need to search a string in the string array. I dont want to use any for looping in it
string [] arr = {"One","Two","Three"};
string theString = "One"
I need to check whether theString variable is present in arr.
Well, something is going to have to look, and looping is more efficient than recursion (since tail-end recursion isn't fully implemented)... so if you just don't want to loop yourself, then either of:
bool has = arr.Contains(var); // .NET 3.5
or
bool has = Array.IndexOf(arr, var) >= 0;
For info: avoid names like var - this is a keyword in C# 3.0.
Every method, mentioned earlier does looping either internally or externally, so it is not really important how to implement it. Here another example of finding all references of target string
string [] arr = {"One","Two","Three"};
var target = "One";
var results = Array.FindAll(arr, s => s.Equals(target));
Does it have to be a string[] ? A List<String> would give you what you need.
List<String> testing = new List<String>();
testing.Add("One");
testing.Add("Two");
testing.Add("Three");
testing.Add("Mouse");
bool inList = testing.Contains("Mouse");
bool exists = arr.Contains("One");
I think it is better to use Array.Exists than Array.FindAll.
Its pretty simple. I always use this code to search string from a string array
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos > -1)
{
return true;
}
else
{
return false;
}
If the array is sorted, you can use BinarySearch. This is a O(log n) operation, so it is faster as looping. If you need to apply multiple searches and speed is a concern, you could sort it (or a copy) before using it.
Each class implementing IList has a method Contains(Object value). And so does System.Array.
Why the prohibition "I don't want to use any looping"? That's the most obvious solution. When given the chance to be obvious, take it!
Note that calls like arr.Contains(...) are still going to loop, it just won't be you who has written the loop.
Have you considered an alternate representation that's more amenable to searching?
A good Set implementation would perform well. (HashSet, TreeSet or the local equivalent).
If you can be sure that arr is sorted, you could use binary search (which would need to recurse or loop, but not as often as a straight linear search).
You can use Find method of Array type. From .NET 3.5 and higher.
public static T Find<T>(
T[] array,
Predicate<T> match
)
Here is some examples:
// we search an array of strings for a name containing the letter “a”:
static void Main()
{
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, ContainsA);
Console.WriteLine (match); // Jack
}
static bool ContainsA (string name) { return name.Contains ("a"); }
Here’s the same code shortened with an anonymous method:
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, delegate (string name)
{ return name.Contains ("a"); } ); // Jack
A lambda expression shortens it further:
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, n => n.Contains ("a")); // Jack
At first shot, I could come up with something like this (but it's pseudo code and assuming you cannot use any .NET built-in libaries). Might require a bit of tweaking and re-thinking, but should be good enough for a head-start, maybe?
int findString(String var, String[] stringArray, int currentIndex, int stringMaxIndex)
{
if currentIndex > stringMaxIndex
return (-stringMaxIndex-1);
else if var==arr[currentIndex] //or use any string comparison op or function
return 0;
else
return findString(var, stringArray, currentIndex++, stringMaxIndex) + 1 ;
}
//calling code
int index = findString(var, arr, 0, getMaxIndex(arr));
if index == -1 printOnScreen("Not found");
else printOnScreen("Found on index: " + index);
In C#, if you can use an ArrayList, you can use the Contains method, which returns a boolean:
if MyArrayList.Contains("One")
You can check the element existence by
arr.Any(x => x == "One")
it is old one ,but this is the way i do it ,
enter code herevar result = Array.Find(names, element => element == "One");
I'm surprised that no one suggested using Array.IndexOf Method.
Indeed, Array.IndexOf has two advantages :
It allows searching if an element is included into an array,
It gets at the same time the index into the array.
int stringIndex = Array.IndexOf(arr, theString);
if (stringIndex >= 0)
{
// theString has been found
}
Inline version :
if (Array.IndexOf(arr, theString) >= 0)
{
// theString has been found
}
Using Contains()
string [] SomeArray = {"One","Two","Three"};
bool IsExist = SomeArray.Contains("One");
Console.WriteLine("Is string exist: "+ IsExist);
Using Find()
string [] SomeArray = {"One","Two","Three"};
var result = Array.Find(SomeArray, element => element == "One");
Console.WriteLine("Required string is: "+ result);
Another simple & traditional way, very useful for beginners to build logic.
string [] SomeArray = {"One","Two","Three"};
foreach (string value in SomeArray) {
if (value == "One") {
Console.WriteLine("Required string is: "+ value);
}
}