What is the easiest way to clear an array of strings?
Have you tried Array.Clear?
string[] foo = ...;
Array.Clear(foo, 0, foo.Length);
Note that this won't change the size of the array - nothing will do that. Instead, it will set each element to null.
If you need something which can actually change size, use a List<string> instead:
List<string> names = new List<string> { "Jon", "Holly", "Tom" };
names.Clear(); // After this, names will be genuinely empty (Count==0)
Array.Clear(theArray, 0, theArray.Length);
It depends on circumstance (like: what is in the array) but the best method usually is to create a new one. Dropping all references to the old one.
MyType[] array = ...
....
array = new MyType[size];
I think you can also get away with this for example:
SearchTerm = new string[]{};
You can try this.
result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
string[] foo;
foo = string[""];
how about
string[] foo;
foo = null;
Related
I have list of string
Like list1 contain 'pinky','smita','Rashmi','Srivani'
And string is like
String str = "pinky, Nandini'
I want to check if neither of str present in list1,proceed further.
how to do that?
If I understood correctly, you want to return false in the example case, so you can use Any method: Check if none of the elements of the list is already in the str, here is a one liner:
if (!list.Any(x=>str.Contains(x))) ....
You can use combination of .Any() with .Contains() with !,
var list1 = new List<string>(){ "pinky", "smita", "Rashmi", "Srivani" };
string str = "pinky, Nandini";
var list2 = str.Split(",");
var nameExists = list2.Any(x => list1.Contains(x));
if(!nameExists)
{
//Your code goes here.
}
As #Fildor said, you can use Intersect(). Elegant approach,
//All credit goes to #Fildor
var nameExists = list1.Intersect(list2).Any();
workareaRefs is a string of random values splitted by comma i.e. 4,7,1,7 etc.
I am setting properties to TrackDataFilter and would like to set the Workareas
which is of type IList with the values in workareaRefs var.
So Workareas should contain the values in workareaRefs stored in the variable named r.
Can anyone help me achieve this?
var workareasRefs = workareaRefs.Split(',');
var r = new TrackDataFilter
{
DatePreset = preset,
Workareas = new List<TrackFilterGenericRef>
{
new TrackFilterGenericRef
{
Ref = 2, Type = Enums.ContentTypes.Workarea
}
},
};
Well, I am not sure If I understand your question correctly, so by guessing a bit, I would assume you want to do the following
WorkAreas = new List(workareasRefs);
I have got a simple question I am having a list:
List<string> test = new List<string> {"one", "two", "three", "four"}
Now I want to take for example value "three" and get all elements after it, so it would be looking like:
List<string> test = new List<string> {"three", "four"}
But we do not know where list end so it can be list of many elements and we can not define end as const.
Is it possible?
It sounds like you're looking for SkipWhile from LINQ:
test = test.SkipWhile(x => x != "three").ToList();
That will skip everything until (but not including) the "three" value, then include everything else. It then converts it to a list again.
Since you assign the filtered list back to initial one, then just remove first items up to "three" one:
int count = test.IndexOf("three");
test.RemoveRange(0, count < 0 ? test.Count : count);
This implementation doesn't create additional list, but modifies existing one.
This might do the trick for you
var list2 = test.Skip(2).Take(test.Count).ToList();
or better
var list3 = test.Skip(2).ToList();
Without LINQ it could be done something like this
List<string> outtest = new List<string>();
bool drty = false;
foreach(string st in test)
{
if(st == "three") //or whatever is the input.
drty = true;
if(drty)
outtest.Add(st);
}
i have a list of string
Emails = new List<string>() { "R.Dun#domain.co.nz", "S.Dun#domain.co.nz" }
now i want to pass string.empty to first value of list
something like
policy.Emails = new List<string>(){string.Empty};
how to put a loop for e.g. for each value of list do something.
you can directly set the first element as string.Empty:
policy.Emails[0]=string.Empty;
You can use indexof function for finding a string in the list as below,
List<string> strList = new List<string>() { "R.Dun#domain.co.nz", "S.Dun#domain.co.nz" };
int fIndex = strList.IndexOf("R.Dun#domain.co.nz");
if(fIndex != -1)
strList[fIndex] = string.Empty;
Or if you want to replace first item with string.Empty then as dasblinkenlight mentioned you can do using the index directly,
strList[0] = string.Empty
Hope it helps.
You can prepend string.Empty to an existing list with concat:
var emails = new List<string> {"R.Dun#domain.co.nz", "S.Dun#domain.co.nz"};
policy.Emails = new[] {string.Empty}.Concat(emails).ToList();
Now policy.Emails looks like this:
{"", "R.Dun#domain.co.nz", "S.Dun#domain.co.nz"}
If you would like to replace the first item, use Skip(1) before concatenating:
policy.Emails = new[] {string.Empty}.Concat(emails.Skip(1)).ToList();
To generalize, replacing the initial n values with empty strings would look like this:
policy.Emails = Enumerable.Repeat(string.Empty, 1).Concat(emails.Skip(n)).ToList();
Note: It goes without saying that if you do not mind modifying the list in place, the simplest solution is to do
emails[0] = string.Empty;
If you want to add an empty string at the beginning of a list you could do:
emails.Insert(0, string.Empty);
if i have an array. can i populate a generic list from that array:
Foo[] fooList . . . (assume populated array)
// This doesn't seem to work
List<Foo> newList = new List<Foo>(fooList);
You could convert the array to a List:
string[] strings = { "hello", "world" };
IList<string> stringList = strings.ToList();
You are looking for List(t).AddRange Method
As #korki said, AddRange will work, but the code you've posted should work fine. For example, this compiles:
var i = new int[10];
var list = new List<int>(i);
Could you show us more of your code?