I have a list of 9 characters which from the beginning is empty:
List<char> myList = new List<char>() { '-', '-', '-', '-', '-', '-', '-', '-', '-' };
Then a user will input a letter on a certain position, which is given. Then the list will then look like this:
{'-', 'a', '-', '-', '-', '-', '-', '-', '-'}
I now want to find the string that is created from each time a new letter is added. So if the list looks like this after a while:
{'-', 'a', '-', '-', 'b', '-', 'c', 'd', '-'}
And a user adds a letter on index 6 it will look like this:
{'-', 'a', '-', '-', 'b', 'e', 'c', 'd', '-'}
So I now want to output the string "becd".
One way I found is to loop through the list in each "direction" from where the letter is added and find when there is a '-' char and stop. Then the same in the other direction. Then I can merge the letters between the start and end index.
Is there an easier and cleaner way to do this?
One way I found is to loop through the list in each "direction" from where the letter is added and find when there is a '-' char and stop. Then the same in the other direction. Then I can merge the letters between the start and end index.
Seems reasonable to me. As a List<char> chars that might look like:
var putAt = 6;
string.Concat(chars.Skip(chars.LastIndexOf('-', putAt) + 1).TakeWhile(c => c != '-'));
LastIndexOf starts at the put char (or could be put char -1 for a tiny optimization) and seeks left looking for a '-'. LINQ then skips the chars up to/including that point and takes chars rightwards while it doesn't encounter a '-'. string.Concat makes the string
If chars was an array you could do it with a range:
new string(
chars[(Array.LastIndexOf(chars, '-', putAt) + 1)..Array.IndexOf(chars, '-', putAt)]
);
I'd expect it to be more performant, but I haven't checked - again LastIndexOf does a leftwards crawl and IndexOf does a rightwards one. This gives two indexes for the range operation that cuts a chunk of chars out of the array. String's constructor takes the new charray and makes it a string
Related
For example, I have a string:
"Nice to meet you"
, there are 13 letters when we count repeating letters, but I wanna create a char array of letters from this string without repeating letters, I mean for the string above it should create an array like
{'N', 'i', 'c', 'e', 't', 'o', 'y', 'u', 'm'}
I was looking for answers on google for 2 hours, but I found nothing, there were lots of answers about strings and char arrays, but were not answers for my situation. I thought that I can write code by checking every letter in the array by 2 for cycles but this time I got syntax errors, so I decided to ask.
You can do this:
var foo = "Nice to meet you";
var fooArr = s.ToCharArray();
HashSet<char> set = new();
set.UnionWith(fooArr);
//or if you want without whitespaces you could refactor this as below
set.UnionWith(fooArr.Where(c => c != ' '));
UPDATE:
You could even make an extension method:
public static IEnumerable<char> ToUniqueCharArray(this string source, char? ignoreChar)
{
var charArray = source.ToCharArray();
HashSet<char> set = new();
set.UnionWith(charArray.Where(c => c != ignoreChar));
return set;
}
And then you can use it as:
var foo = "Nice to meet you";
var uniqueChars = foo.ToUniqueCharArray(ignoreChar: ' ');
// if you want to keep the whitespace
var uniqueChars = foo.ToUniqueCharArray(ignoreChar: null);
this piece of code does the job:
var sentence = "Nice To meet you";
var arr = sentence.ToLower().Where(x => x !=' ' ).ToHashSet();
Console.WriteLine(string.Join(",", arr));
I have added ToLower() if you dont do differences between uppercase and lowercase, if case is sensitive you just put off this extension..
HashSet suppresses all duplicates letters
test: Fiddle
I tried this one and it works too
"Nice to meet you".Replace(" ", "").ToCharArray().Distinct();
A very short solution is to use .Except() on the input string:
string text = "Nice to meet you";
char[] letters = text.Except(" ").ToArray();
Here, .Except():
translates both the text string and the parameter string (" ") to char collections
filters out all the chars in the text char collection that are present in the parameter char collection
returns a collection of distinct chars from the filtered text char collection
Example fiddle here.
Visualizing the process
Let's use the blue banana as an example.
var input = "blue banana";
input.Except(" ") will be translated to:
{ 'b', 'l', 'u', 'e', ' ', 'b', 'a', 'n', 'a', 'n', 'a' }.Except({ ' ' })
Filtering out all ' ' occurrences in the text char array produces:
{ 'b', 'l', 'u', 'e', 'b', 'a', 'n', 'a', 'n', 'a' }
The distinct char collection will have all the duplicates of 'b', 'a' and 'n' removed, resulting in:
{ 'b', 'l', 'u', 'e', 'a', 'n' }
ToCharArray method of string is only thing you need.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
string str = "Nice to meet you";
char[] carr = str.ToCharArray();
for(int i = 0; i < carr.Length; i++)
Console.WriteLine (carr[i]);
}
}
String str = "Nice To meet you";
char[] letters = str.ToLower().Except(" ").ToArray();
A solution just using for-loops (no generics or Linq), with comments explaining things:
// get rid of the spaces
String str = "Nice to meet you".Replace(" ", "");
// a temporary array more than long enough (we will trim it later)
char[] temp = new char[str.Length];
// the index where to insert the next char into "temp". This is also the length of the filled-in part
var idx = 0;
// loop through the source string
for (int i=0; i<str.Length; i++)
{
// get the character at this position (NB "surrogate pairs" will fail here)
var c = str[i];
// did we find it in the following loop?
var found = false;
// loop through the collected characters to see if we already have it
for (int j=0; j<idx; j++)
{
if (temp[j] == c)
{
// yes, we already have it!
found = true;
break;
}
}
if (!found)
{
// we didn't already have it, so add
temp[idx] = c;
idx+=1;
}
}
// "temp" is too long, so create a new array of the correct size
var letters = new char[idx];
Array.Copy(temp, letters, idx);
// now "letters" contains the unique letters
That "surrogate pairs" remark basically means that emojis will fail.
How can I remove a specific list of chars from a string?
For example I have the string Multilanguage File07 and want to remove all vowels, spaces and numbers to get the string MltlnggFl.
Is there any shorter way than using a foreach loop?
string MyLongString = "Multilanguage File07";
string MyShortString = MyLongString;
char[] charlist = new char[17]
{ 'a', 'e', 'i', 'o', 'u',
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '0', ' ' };
foreach (char letter in charlist)
{
MyShortString = MyShortString.Replace(letter.ToString(), "");
}
Use this code to replace a list of chars within a string:
using System.Text.RegularExpressions;
string MyLongString = "Multilanguage File07";
string MyShortString = Regex.Replace(MyLongString, "[aeiou0-9 ]", "");
Result:
Multilanguage File07 => MltlnggFl
Text from which some chars should be removed 12345 => Txtfrmwhchsmchrsshldbrmvd
Explanation of how it works:
The Regex Expression I use here, is a list of independend chars defined by the brackets []
=> [aeiou0-9 ]
The Regex.Replace() iterates through the whole string and looks at each character, if it will match one of the characters within the Regular Expression.
Every matched letter will be replaced by an empty string ("").
How about this:
var charList = new HashSet<char>(“aeiou0123456789 “);
MyLongString = new string(MyLongString.Where(c => !charList.Contains(c)).ToArray());
Try this pattern: (?|([aeyuio0-9 ]+)). Replace it with empty string and you will get your desird result.
I used branch reset (?|...) so all characters are captured into one group for easier manipulation.
Demo.
public void removeVowels()
{
string str = "MultilanguAge File07";
var chr = str.Where(c => !"aeiouAEIOU0-9 ".Contains(c)).ToList();
Console.WriteLine(string.Join("", chr));
}
1st line: creating desire string variable.
2nd line: using linq ignore vowels words [captital case,lower case, 0-9 number & space] and convert into list.
3rd line: combine chr list into one line string with the help of string.join function.
result: MltlnggFl7
Note: removeVowels function not only small case, 1-9 number and empty space but also remove capital case word from string.
Is there simplest way to split the word/sentence into every single char and store into array?
Eg:
Me and you.
array = { 'M', 'e', ' ', 'a', 'n', 'd', ' ', 'y', 'o', 'u', '.' };
Well, this is pretty simple :
"Me and you".ToCharArray();
Using String.ToCharArray Method
https://msdn.microsoft.com/en-us/library/ezftk57x(v=vs.110).aspx
var chars = "Me and you.".ToCharArray();
This might be helpful
// Input string.
string value = "Me and you.";
// Use ToCharArray to convert string to array.
char[] array = value.ToCharArray();
This post might be more theory than code.
I was wondering if there is a (relatively) simple way to use a text table (basically an array of chars) and replace the chars in a string based on their value.
Let me elaborate.
Let's say we have this two line table:
table[0x0] = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'};
table[0x1] = new char[] {'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ']', ',', '/', '.', '~', '&'};
Each array has 16 members, 0-F in hex.
Say we have a string "hello" converted to hex (68 65 6C 6C 6F). I want to take these hex numbers, and map them to the new locations as defined in the table above.
So, "hello" would now look like this:
07 04 0B 0B 0E
I can easily convert the string into an array, but I am stuck on what to do next. I feel a foreach loop would do the trick, but it's exact contents I do not yet know.
Is there an easy way to do this? It seems like it shouldn't be too hard, but I'm not quite sure how to go about doing it.
Thank you very much for any help at all!
static readonly char[] TABLE = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ']', ',', '/', '.', '~', '&',
};
// Make a lookup dictionary of char => index in the table, for speed.
static readonly Dictionary<char, int> s_lookup = TABLE.ToDictionary(
c => c, // Key is the char itself.
c => Array.IndexOf(TABLE, c)); // Value is the index of that char.
static void Main(string[] args) {
// The test input string. Note it has no space.
string str = "hello,world.";
// For each character in the string, we lookup what its index in the
// original table was.
IEnumerable<int> indices = str.Select(c => s_lookup[c]);
// Print those numbers out, first converting them to two-digit hex values,
// and then joining them with commas in-between.
Console.WriteLine(String.Join(",", indices.Select(i => i.ToString("X02"))));
}
Output:
07,04,0B,0B,0E,1B,16,0E,11,0B,03,1D
Note that if you provide an input character that isn't in the lookup table, you're not going to notice it right away! Select returns an IEnumerable, that is lazily-evaluated only when you go to use it. At that point, if the input character is not found, the dictionary [] call will throw an exeception.
One way to make this more obvious is to call ToArray() after the Select, so you have an array of indices, and not an IEnumerable. This will force the evaluation to happen immediately:
int[] indices = str.Select(c => s_lookup[c]).ToArray();
Reference:
Array.IndexOf
Enumerable.ToDictionary
Enumerable.Select
String.Join
I want to convert part of a char array to a string. What is the best way to do that.
I know I can do the following for the whole array
char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);
but what about just elements 2 to 4 for example?
I also know I can loop through the array and extract them, but I wondered if there was a more succinct way of doing it.
Use the String constructor overload which takes a char array, an index and a length:
String text = new String(chars, 2, 3); // Index 2-4 inclusive
You may use LINQ
char[] chars = { 'a', ' ', 's', 't', 'r', 'i', 'n', 'g' };
string str = new string(chars.Skip(2).Take(2).ToArray());
But off course string overloaded constructor is the way to go