[英]How to create char array of letters from a string array ? (C#)
例如,我有一個string
:
“很高興認識你”
,當我們計算重復字母時有 13 個字母,但我想從這個字符串中創建一個不重復字母的char
array
,我的意思是對於上面的字符串,它應該創建一個類似的array
{'N', 'i', 'c', 'e', 't', 'o', 'y', 'u', 'm'}
我在谷歌上找了兩個小時的答案,但我什么也沒找到,有很多關於字符串和字符 arrays 的答案,但不是我的情況的答案。 我以為我可以通過循環檢查數組中的每個字母 2 for
編寫代碼,但是這次我遇到了語法錯誤,所以我決定問一下。
你可以這樣做:
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 != ' '));
更新:你甚至可以做一個擴展方法:
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;
}
然后您可以將其用作:
var foo = "Nice to meet you";
var uniqueChars = foo.ToUniqueCharArray(ignoreChar: ' ');
// if you want to keep the whitespace
var uniqueChars = foo.ToUniqueCharArray(ignoreChar: null);
這段代碼完成了這項工作:
var sentence = "Nice To meet you";
var arr = sentence.ToLower().Where(x => x !=' ' ).ToHashSet();
Console.WriteLine(string.Join(",", arr));
如果您不區分大寫和小寫,我添加了ToLower()
,如果區分大小寫,您只需推遲此擴展。HashSet 會抑制所有重復的字母
測試:小提琴
我試過這個,它也有效
"Nice to meet you".Replace(" ", "").ToCharArray().Distinct();
一個非常簡短的解決方案是在輸入字符串上使用.Except()
:
string text = "Nice to meet you";
char[] letters = text.Except(" ").ToArray();
在這里, .Except()
:
" "
) 轉換為 char collections示例小提琴在這里。
讓我們以藍色香蕉為例。
var input = "blue banana";
input.Except(" ")
將被翻譯成:{ 'b', 'l', 'u', 'e', ' ', 'b', 'a', 'n', 'a', 'n', 'a' }.Except({ ' ' })
' '
會產生:{ 'b', 'l', 'u', 'e', 'b', 'a', 'n', 'a', 'n', 'a' }
'b'
、 'a'
和'n'
的所有重復項,從而導致:{ 'b', 'l', 'u', 'e', 'a', 'n' }
字符串的 ToCharArray 方法是您唯一需要的。
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();
一個只使用 for 循環的解決方案(沒有 generics 或 Linq),注釋解釋了一些事情:
// 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
“代理對”這句話基本上意味着表情符號會失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.