Find string with most characters in array - c#

I have an array of string, and would like to find the index of the string with the most characters. I would like to do this without a for-loop.

You can just use the Select overload that gives you the index, order by descending length and get the first value;
string[] strings = new string[]{ "one", "three", "two" };
var value = strings.Select ((val, ix) => new {len=val.Length, ix})
.OrderByDescending (x => x.len).FirstOrDefault();
Console.WriteLine ("Index of longest string is: " +
(value != null ? value.ix : -1));

Use a recursive function that takes a current string, the array of strings, the index of the current string, index of the current string in the array, and the current max char count.

Related

Get string from array or set default value in a one liner

So we have ?? to parse its right-hand value for when the left hand is null.
What is the equivalent for a string[].
For example
string value = "One - Two"
string firstValue = value.Split('-')[0] ?? string.Empty;
string secondValue = value.Split('-')[1] ?? string.Empty;
Above example would still crash if we would try to get a third index or if string value = "One". Because it is not null but IndexOutOfRangeException is thrown.
https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception
So what is a one-line solution to tackle the above problem? I'd like to avoid the try-catch scenario because this gives ugly code.
I want to get value out of a string[] with a string.Empty as a backup value so my string is never null.
Well, you can try Linq:
using System.Linq;
...
string thirdValue = value.Split('-').ElementAtOrDefault(2) ?? string.Empty;
However, your code has a drawback: you constantly Split the same string. I suggest extracting value.Split('-'):
string value = "One - Two"
var items = value.Split('-');
string firstValue = items.ElementAtOrDefault(0) ?? string.Empty;
string secondValue = items.ElementAtOrDefault(1) ?? string.Empty;
I suggest you create a method for this. which will accept two inputs of type string(representing the input string) and an integer(represents the specified index), and should return the split value if the specified index is available, else it will return an empty string:
string GetSubstring(string input, int index)
{
string returnValue = String.Empty;
string[] substrings = input.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
returnValue = substrings.Length > index ? substrings[index] : returnValue;
return returnValue;
}
Here is a working example for your reference
One way to achieve this is to use MoreLinq's Lead and tuple destructuring:
string value = "One - Two";
var (first, second) = value.Split('-').Lead(1, string.Empty, (x, y) => (x, y)).First();
Or, if you want a more generic approach that works for all indices:
string value = "One - Two - Three - Fourth - Fifth";
var (first, second) = value.Split('-').Skip(6).Concat(string.Empty).Lead(7, string.Empty, (x, y) => (x, y)).First();
This code will get the seventh and fourteenth entries (neither of which are there, so string.Empty will be used for both).
Another option to consider is to assign two different variables on the same line of code:
string value = "One - Two";
var split = value.Split('-');
string first = split[0] ?? string.Empty, second = split.ElementAtOrDefault(1) ?? string.Empty;
This gives you three lines of code, good performance and reasonable level of clarity and readability.
Note there is no need to use ElementOrDefault(0) - better to use [0] since Split will never return an array with no elements in it.
Another option would be to use destructuring - but that is really only useful if you are interested in contiguous entries at the start of the array (although it could be tweaked to take index parameters reasonably simply):
public static void Destructure<T>(this T[] items, T defaultValue, out T t0, out T t1)
{
t0 = items.Length > 0 ? items[0] : defaultValue;
t1 = items.Length > 1 ? items[1] : defaultValue;
}
Maybe the following code will be useful
string value = "One - Two";
string firstValue = (value.Split('-').Length == 1 ? value.Split('-')[0] : null) ?? string.Empty;
string secondValue = (value.Split('-').Length == 2 ? value.Split('-')[1] : null) ?? string.Empty;

Find index of item from string array containing comma separated values

Below is my string array :
string[] arr = {
"region1.mp3,region1-sub.mp3,region1-sub1.mp3,region2-sub.mp3",
"region2.mp3,region2-Sub1.mp3",
"region3.mp3"
};
Below is my value which I am trying to search in above string array and get index:
string searchItem = "region1-sub1.mp3";
This is how I am trying to search but getting -1 (-1 indicates search not found I guess):
int index = Array.FindIndex(arr, t => t == searchItem); // -1
I understand that because my records in string array are comma separated that is why this search is failing.
So any other method which can help me find index without looping and generating new string array?
Expected Output : 0
You want to split every string by comma:
int index = Array.FindIndex(arr, t => t.Split(',').Contains(searchItem));
This works even if the string doesn't contain a comma.
This will you give you your desired output.
int index = Array.FindIndex(arr, t => t.Contains(searchItem));
int index = Array.FindIndex(arr, t => t.Contains(searchItem));
This returns 0.

Selecting max number from a list of string in LINQ?

trying to select the max digit from a list of strings:
int maxDigit = this.myList.Where(x=> x.Name.Any(Char.IsDigit))
.Select(x => int.Parse(x.Name)).DefaultIfEmpty(0).Max();
It is int.Parse(x.Name) which is causing an exception as this is returning the entire name string e.g. 'myValue99' which of course cannot be parsed to an int. I just want to return 99. I don't know where the digits will be in the string therefore cannot for example take the last two.
I need the DefaultIfEmpty for cases where the string does not contain a number.
Assuming you want the max number and not the max digit, all you need is a function to convert "stuff99" to 99. Then the Linq part becomes child's play:
int maxNumber = myList.Max(ExtractNumberFromText);
or, to be closer to your specs:
int maxNumber = myList
.Select(ExtractNumberFromText)
.DefaultIfEmpty(0)
.Max();
#Codecaster already pointed to a few applicable answers on this site for the second part. I adopted a simple one. No error checking.
// the specs: Only ever going to be my stuff1, stuff99
int ExtractNumberFromText(string text)
{
Match m = Regex.Match(text, #"\d*");
return int.Parse(m.Groups[0].Value); // exception for "abc"
// int.Parse("0" + m.Groups[0].Value); // use this for default to 0
}
you should only select and parse the Digit characters out of your string
int maxDigit = this.myList.Where(x => x.Name.Any(Char.IsDigit))
.Select(x => int.Parse(new string(x.Name.Where(Char.IsDigit).ToArray())))
.DefaultIfEmpty(0).Max();
Assuming the input can contain the following categories:
nulls
Empty strings
Strings with only alphabetical characters
Strings with mixed alphabetical and numerical characters
Strings with only numerical characters
You want to introduce a method that extracts the number, if any, or returns a meaningful value if not:
private static int? ParseStringContainingNumber(string input)
{
if (String.IsNullOrEmpty(input))
{
return null;
}
var numbersInInput = new String(input.Where(Char.IsDigit).ToArray());
if (String.IsNullOrEmpty(numbersInInput))
{
return null;
}
int output;
if (!Int32.TryParse(numbersInInput, out output))
{
return null;
}
return output;
}
Note that not all characters for which Char.IsDigit returns true can be parsed by Int32.Parse(), hence the TryParse.
Then you can feed your list to this method:
var parsedInts = testData.Select(ParseStringContainingNumber)
.Where(i => i != null)
.ToList();
And do whatever you want with the parsedInts list, like calling IEnumerable<T>.Max() on it.
With the following test data:
var testData = new List<string>
{
"۱‎", // Eastern Arabic one, of which Char.IsDigit returns true.
"123",
"abc456",
null,
"789xyz",
"foo",
"9bar9"
};
This returns:
123
456
789
99
Especially note the latest case.
To find the max digit (not number) in each string:
static void Main(string[] args)
{
List<string> strList = new List<string>() { "Value99", "46Text" };
List<int> resultList = new List<int>();
foreach (var str in strList)
{
char[] resultString = Regex.Match(str, #"\d+").Value.ToCharArray();
int maxInt = resultString.Select(s => Int32.Parse(s.ToString())).Max();
resultList.Add(maxInt);
}
}
It can be simple using Regex.
You stated 99, so you need to span more than one digit:
var maxNumber = myTestList.SelectMany(x => getAllNumnbersFromString(x.Name)).DefaultIfEmpty(0).Max();
static List<int> getAllNumnbersFromString(string str)
{
List<int> results = new List<int>();
var matchesCollection = Regex.Matches(str, "[0-9]+");
foreach (var numberMatch in matchesCollection)
{
results.Add(Convert.ToInt32(numberMatch.ToString()));
}
return results;
}
One digit only check:
int maxNumber = myTestList.SelectMany(x => x.Name.ToCharArray().ToList())
.Select(x => Char.IsDigit(x) ? (int)Char.GetNumericValue(x) : 0)
.DefaultIfEmpty(0).Max();
Probably there's a slicker way to do this but I would just do:
int tmp = 0;
int maxDigit = this.myList.Where(x=> x.Name.Any(Char.IsDigit))
.Select(x =>
(int.TryParse(x.Name,out tmp ) ? int.Parse(x.Name) : 0 ) ).Max();
You have to remember that Parse will error out if it can't parse the value but TryParse will just give you false.

C# LINQ: How is string("[1, 2, 3]") parsed as an array?

I am trying to parse a string into array and find a very concise approach.
string line = "[1, 2, 3]";
string[] input = line.Substring(1, line.Length - 2).Split();
int[] num = input.Skip(2)
.Select(y => int.Parse(y))
.ToArray();
I tried remove Skip(2) and I cannot get the array because of non-int string. My question is that what is the execution order of those LINQ function. How many times is Skip called here?
Thanks in advance.
The order is the order that you specify. So input.Skip(2) skips the first two strings in the array, so only the last remains which is 3. That can be parsed to an int. If you remove the Skip(2) you are trying to parse all of them. That doesn't work because the commas are still there. You have splitted by white-spaces but not removed the commas.
You could use line.Trim('[', ']').Split(','); and int.TryParse:
string line = "[1, 2, 3]";
string[] input = line.Trim('[', ']').Split(',');
int i = 0;
int[] num = input.Where(s => int.TryParse(s, out i)) // you could use s.Trim but the spaces don't hurt
.Select(s => i)
.ToArray();
Just to clarify, i have used int.TryParse only to make sure that you don't get an exception if the input contains invalid data. It doesn't fix anything. It would also work with int.Parse.
Update: as has been proved by Eric Lippert in the comment section using int.TryParse in a LINQ query can be harmful. So it's better to use a helper method that encapsulates int.TryParse and returns a Nullable<int>. So an extension like this:
public static int? TryGetInt32(this string item)
{
int i;
bool success = int.TryParse(item, out i);
return success ? (int?)i : (int?)null;
}
Now you can use it in a LINQ query in this way:
string line = "[1, 2, 3]";
string[] input = line.Trim('[', ']').Split(',');
int[] num = input.Select(s => s.TryGetInt32())
.Where(n => n.HasValue)
.Select(n=> n.Value)
.ToArray();
The reason it does not work unless you skip the first two lines is that these lines have commas after ints. Your input looks like this:
"1," "2," "3"
Only the last entry can be parsed as an int; the initial two will produce an exception.
Passing comma and space as separators to Split will fix the problem:
string[] input = line
.Substring(1, line.Length - 2)
.Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
Note the use of StringSplitOptions.RemoveEmptyEntries to remove empty strings caused by both comma and space being used between entries.
I think it would be better you do it this way:
JsonConvert.DeserializeObject(line, typeof(List<int>));
you might try
string line = "[1,2,3]";
IEnumerable<int> intValues = from i in line.Split(',')
select Convert.ToInt32(i.Trim('[', ' ', ']'));

String.Empty in strings

2 days ago, there was a question related to string.LastIndexOf(String.Empty) returning the last index of string:
Do C# strings end with empty string?
So I thought that; a string can always contain string.empty between characters like:
"testing" == "t" + String.Empty + "e" + String.Empty +"sting" + String.Empty;
After this, I wanted to test if String.IndexOf(String.Empty) was returning 0 because since String.Empty can be between any char in a string, that would be what I expect it to return and I wasn't wrong.
string testString = "testing";
int index = testString.LastIndexOf(string.Empty); // index is 6
index = testString.IndexOf(string.Empty); // index is 0
It actually returned 0. I started to think that if I could split a string with String.Empty, I would get at least 2 string and those would be String.Empty and rest of the string since String.IndexOf(String.Empty) returned 0 and String.LastIndexOf(String.Empty) returned length of the string.. Here is what I coded:
string emptyString = string.Empty;
char[] emptyStringCharArr = emptyString.ToCharArray();
string myDummyString = "abcdefg";
string[] result = myDummyString.Split(emptyStringCharArr);
The problem here is, I can't obviously convert String.Empty to char[] and result in an empty string[]. I would really love to see the result of this operation and the reason behind this. So my questions are:
Is there any way to split a string with String.Empty?
If it is not possible but in an absolute world which it would be possible, would it return an array full of chars like [0] = "t" [1] = "e" [2] = "s" and so on or would it just return the complete string? Which would make more sense and why?
Yes, you can split any string with string .Empty
string[] strArr = s.Split(string.Empty.ToCharArray());
You will always get an Index of 0 when you look for String.Empty in any String, because it's the definition of String.IndexOf(String.Empty) you should have a look at the MSDN, where it says:
"The zero-based index position of
value if that string is found, or -1
if it is not. If value is
String.Empty, the return value is 0."
Directed to your second Question:
I think you can Split a String with an Empty String by doing something like this in your code:
String test = "fwewfeoj";
test.Split(new String[] { String.Empty }, StringSplitOptions.None);
By the way: Possible Clone of this answer
Why does "abcd".StartsWith("") return true?
Do you really need to split the string, or are you just trying to get all the individual characters?
If so, then a string is also a IEnumerable<char>, and you also have an indexer.
So, what are you actually trying to do?
And no, you can't call the split methods with string.Empty or similar constructs.
string emptyString = string.Empty;
char[] emptyStringCharArr = emptyString.ToCharArray();
This will give you an empty array of chars.
This is because String is already an array of chars in memory, and String.Empty has no value.
To break it down further, consider an implementation of .ToCharArray()
private Char[] toCharArray(String value)
{
var stringLength = value.Length;
var returningArray = new char[stringLength];
for(var i = 0; i < stringLength; i++)
{
returningArray[i] = value[i];
}
return returningArray;
}
Length of course will be zero, and you will return an empty char array. Of course this isn't the exact implementation, but you can see how and why it's returning nothing (and therefore isn't splitting on the string as you're expecting it to)
It's not an array with a single element String.Empty, because that doesn't really make sense. When you try to split on an empty array, it doesn't know how or what to split on, so you're given back the original string.
As for why it returns 0 by default, consider:
private int IndexOf(String value, String searchFor)
{
for(var i = 0; i < value.Length; i++)
{
if(value.Substring(i, searchFor.Length) == searchFor)
{
return i;
}
}
return -1;
}
private int LastIndexOf(String value, String searchFor)
{
var searchLength = searchFor.Length;
for(var i = value.Length - searchFor.Length; i >= 0; i--)
{
if(value.Substring(i, searchLength) == searchFor)
return i;
}
return -1;
}
String.SubString(x, 0) will ALWAYS return String.Empty, regardless of what's passed in (even String.Empty). For this reason it's much faster to add a check and return 0 regardless (as it would even if it ran the loop).
Since String.Empty is just an empty string, so if you do:
var s = "part1" + string.Empty + "part2";
this will result in exactly the same string as:
var s = "part1" + "part2";
the first syntax will not insert a magic empty string between the two parts.
That IndexOf returns 0, is by definition, not because there is some magic empty string between characters.
I cannot think of a logic way to split a string, by an empty string. What should it return? When using an empty string as an argument to the string.Split method, it will be ignored. If it was the only separator to use, the string will be returned unsplit.
you could also say
"testing" == string.Empty + string.Empty + string.Empty + ... + "t" + string.Empty + string.empty + "esting";
So actually you could place an endless array of string.empty between each character.
So I think
1 not possible
2 none, it just doens't make sense...

Categories