"Exploring LINQ Operations on Strings and String Arrays"

ยท

3 min read

Certainly! Below are examples of how you can use various LINQ methods with strings, string arrays, and sentences represented as strings:

1. String

string text = "Hello World!";

// Example: Filtering
var vowels = text.Where(c => "aeiou".Contains(char.ToLower(c)));
Console.WriteLine("Vowels in the string:");
PrintCollection(vowels);

// Example: Projection
var upperCaseLetters = text.Select(c => char.ToUpper(c));
Console.WriteLine("\nUppercase letters in the string:");
PrintCollection(upperCaseLetters);

// Example: Aggregation
var charCount = text.Length;
var distinctCharsCount = text.Distinct().Count();
Console.WriteLine($"\nCharacter Count: {charCount}, Distinct Character Count: {distinctCharsCount}");

// Example: Sorting
var sortedCharsAsc = text.OrderBy(c => c);
var sortedCharsDesc = text.OrderByDescending(c => c);
Console.WriteLine("\nSorted characters (ascending):");
PrintCollection(sortedCharsAsc);
Console.WriteLine("\nSorted characters (descending):");
PrintCollection(sortedCharsDesc);

// Example: Element Operations
var firstChar = text.First();
var lastChar = text.Last();
Console.WriteLine($"\nFirst character: {firstChar}, Last character: {lastChar}");

// Example: Quantifiers
var hasLetterH = text.Any(c => c == 'H');
var allLettersAreLowerCase = text.All(char.IsLower);
Console.WriteLine($"\nHas letter 'H': {hasLetterH}, All letters are lowercase: {allLettersAreLowerCase}");

// Example: Conversion
var charArray = text.ToArray();
var charList = text.ToList();
Console.WriteLine("\nConverted to character array:");
PrintCollection(charArray);
Console.WriteLine("\nConverted to character list:");
PrintCollection(charList);

2. String Array

string[] words = { "apple", "banana", "orange", "grape", "pineapple" };

// Example: Filtering
var longWords = words.Where(w => w.Length > 6);
Console.WriteLine("\nWords with more than 6 letters:");
PrintCollection(longWords);

// Example: Projection
var wordLengths = words.Select(w => w.Length);
Console.WriteLine("\nLength of words:");
PrintCollection(wordLengths);

// Example: Aggregation
var wordCount = words.Length;
Console.WriteLine($"\nWord Count: {wordCount}");

// Example: Sorting
var sortedWordsAsc = words.OrderBy(w => w);
var sortedWordsDesc = words.OrderByDescending(w => w);
Console.WriteLine("\nSorted words (ascending):");
PrintCollection(sortedWordsAsc);
Console.WriteLine("\nSorted words (descending):");
PrintCollection(sortedWordsDesc);

// Example: Element Operations
var firstWord = words.First();
var lastWord = words.Last();
Console.WriteLine($"\nFirst word: {firstWord}, Last word: {lastWord}");

// Example: Quantifiers
var hasWordApple = words.Any(w => w == "apple");
var allWordsContainLetterA = words.All(w => w.Contains('a'));
Console.WriteLine($"\nHas word 'apple': {hasWordApple}, All words contain letter 'a': {allWordsContainLetterA}");

// Example: Conversion
var wordsString = string.Join(", ", words);
Console.WriteLine($"\nConverted to string: {wordsString}");

3. Sentence as a String

string sentence = "The quick brown fox jumps over the lazy dog";

// Example: Filtering
var vowelsInSentence = sentence.Where(c => "aeiou".Contains(char.ToLower(c)));
Console.WriteLine("\nVowels in the sentence:");
PrintCollection(vowelsInSentence);

// Example: Projection
var wordsInSentence = sentence.Split(' ');
Console.WriteLine("\nWords in the sentence:");
PrintCollection(wordsInSentence);

// Example: Aggregation
var wordCountInSentence = wordsInSentence.Length;
Console.WriteLine($"\nWord Count in the sentence: {wordCountInSentence}");

// Example: Sorting
var sortedWordsInSentenceAsc = wordsInSentence.OrderBy(w => w);
Console.WriteLine("\nSorted words in the sentence (ascending):");
PrintCollection(sortedWordsInSentenceAsc);

// Example: Element Operations
var firstWordInSentence = wordsInSentence.First();
var lastWordInSentence = wordsInSentence.Last();
Console.WriteLine($"\nFirst word in the sentence: {firstWordInSentence}, Last word in the sentence: {lastWordInSentence}");

// Example: Quantifiers
var hasWordFox = wordsInSentence.Any(w => w == "fox");
var allWordsStartWithLetterT = wordsInSentence.All(w => w.StartsWith("T"));
Console.WriteLine($"\nHas word 'fox' in the sentence: {hasWordFox}, All words start with letter 'T': {allWordsStartWithLetterT}");

// Example: Conversion
var sentenceCharArray = sentence.ToCharArray();
Console.WriteLine("\nConverted to character array:");
PrintCollection(sentenceCharArray);

Helper Method

static void PrintCollection<T>(IEnumerable<T> collection)
{
    foreach (var item in collection)
    {
        Console.Write(item + " ");
    }
    Console.WriteLine();
}

These examples demonstrate various LINQ operations on strings, string arrays, and sentences represented as strings. Adjust and expand upon these examples as needed for your specific requirements!

ย