C# 从未知字符串长度保存大小未知的数组
我想将未知C# 从未知字符串长度保存大小未知的数组,c#,arrays,C#,Arrays,我想将未知字符串的输入保存到数组中。我如何使其产生如下结果: string a = "ABCA"; char[] array; array = a.ToCharArray(0, a.length); foreach (char c in array) { switch(c) { case 'A': into2ndarray = 1; break; case 'B': into2ndarray = 2;
字符串的输入保存到数组中。我如何使其产生如下结果:
string a = "ABCA";
char[] array;
array = a.ToCharArray(0, a.length);
foreach (char c in array) {
switch(c) {
case 'A':
into2ndarray = 1;
break;
case 'B':
into2ndarray = 2;
break;
case 'C':
into2ndarray = 3;
break;
}
}
int[] into2ndarray //the result will be used for another calculation
因此,预期的结果是{1,2,3,1}
你知道如何实现这一目标吗?谢谢。您可以尝试Linq进行查询,例如
using System.Linq;
...
int[] into2ndarray = a
.Select(item => // for each character in a string
item == 'A' ? 1 // map A to 1
: item == 'B' ? 2 // -/- B to 2
: item == 'C' ? 3 // -/- C to 3
: -1) // map other characters to -1
.Where(number => number >= 0) // filter out -1s
.ToArray(); // materialize as an array
您可以尝试使用Linq进行查询,例如
using System.Linq;
...
int[] into2ndarray = a
.Select(item => // for each character in a string
item == 'A' ? 1 // map A to 1
: item == 'B' ? 2 // -/- B to 2
: item == 'C' ? 3 // -/- C to 3
: -1) // map other characters to -1
.Where(number => number >= 0) // filter out -1s
.ToArray(); // materialize as an array
以下是我将如何做到这一点:
class Program
{
static void Main(string[] args)
{
int[] array = ConvertStringToArray("ABCA");
}
public static int[] ConvertStringToArray(string str)
{
int[] array = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case 'A':
array[i] = 1;
break;
case 'B':
array[i] = 2;
break;
case 'C':
array[i] = 3;
break;
default:
throw new NotImplementedException($"Char '{str[i]}' is not managed");
break;
}
}
return array;
}
}
类程序
{
静态void Main(字符串[]参数)
{
int[]数组=ConvertStringToArray(“ABCA”);
}
公共静态int[]ConvertStringToArray(字符串str)
{
int[]数组=新的int[str.Length];
对于(int i=0;i
以下是我将如何做到这一点:
class Program
{
static void Main(string[] args)
{
int[] array = ConvertStringToArray("ABCA");
}
public static int[] ConvertStringToArray(string str)
{
int[] array = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case 'A':
array[i] = 1;
break;
case 'B':
array[i] = 2;
break;
case 'C':
array[i] = 3;
break;
default:
throw new NotImplementedException($"Char '{str[i]}' is not managed");
break;
}
}
return array;
}
}
类程序
{
静态void Main(字符串[]参数)
{
int[]数组=ConvertStringToArray(“ABCA”);
}
公共静态int[]ConvertStringToArray(字符串str)
{
int[]数组=新的int[str.Length];
对于(int i=0;i
您可以这样做以实现您想要的结果。在代码中查看我的注释以了解它在做什么
string a = "ABCA";
// this code is unnecessary because the characters of a string can already be accessed by index
// char[] array;
// array = a.ToCharArray(0,a.length);
int[] into2ndarray = new int[a.Length]; // instantiate the new array to the same length as the string
for (int i = 0; i < a.Length; ++i) // don't use a foreach since we need to know the index. a for loop is better here
{
switch(a[i]){ // access the char from the string directly by index
case 'A':
into2ndarray[i] = 1; // assign result to the new array
break;
case 'B':
into2ndarray[i] = 2;
break;
case 'C':
into2ndarray[i] = 3;
break;
}
}
string a=“ABCA”;
//此代码是不必要的,因为索引已经可以访问字符串的字符
//字符[]数组;
//数组=a.ToCharArray(0,a.length);
int[]into2ndarray=新int[a.Length];//将新数组实例化为与字符串相同的长度
for(inti=0;i
您可以这样做以实现您想要的结果。在代码中查看我的注释以了解它在做什么
string a = "ABCA";
// this code is unnecessary because the characters of a string can already be accessed by index
// char[] array;
// array = a.ToCharArray(0,a.length);
int[] into2ndarray = new int[a.Length]; // instantiate the new array to the same length as the string
for (int i = 0; i < a.Length; ++i) // don't use a foreach since we need to know the index. a for loop is better here
{
switch(a[i]){ // access the char from the string directly by index
case 'A':
into2ndarray[i] = 1; // assign result to the new array
break;
case 'B':
into2ndarray[i] = 2;
break;
case 'C':
into2ndarray[i] = 3;
break;
}
}
string a=“ABCA”;
//此代码是不必要的,因为索引已经可以访问字符串的字符
//字符[]数组;
//数组=a.ToCharArray(0,a.length);
int[]into2ndarray=新int[a.Length];//将新数组实例化为与字符串相同的长度
for(inti=0;i
可以使用字典
:
Dictionary keySizes=新字典{
{'A',2},
{'B',2},
{'C',2}
};
例如:
public static int[] ConvertStringToArray()
{
string str = "ABCA";
char[] array;
array = str.ToCharArray(0, str.Count());
int[] into2ndarray = new int[] { };
Dictionary<char, int> keySizes = new Dictionary<char, int> {
{ 'A', 2 },
{ 'B', 2 },
{ 'C', 2 }
};
int arraySize;
foreach (char c in array)
{
if (keySizes.TryGetValue(c, out arraySize))
{
into2ndarray = new int[arraySize];
break;
}
}
return into2ndarray;
}
publicstatic int[]ConvertStringToArray()
{
字符串str=“ABCA”;
字符[]数组;
array=str.ToCharArray(0,str.Count());
int[]into2ndarray=新int[]{};
Dictionary keySizes=新字典{
{'A',2},
{'B',2},
{'C',2}
};
内部阵列化;
foreach(数组中的字符c)
{
if(keySizes.TryGetValue(c,out arraySize))
{
into2ndarray=新整数[arraySize];
打破
}
}
返回到第2道光线;
}
可以使用字典
:
Dictionary keySizes=新字典{
{'A',2},
{'B',2},
{'C',2}
};
例如:
public static int[] ConvertStringToArray()
{
string str = "ABCA";
char[] array;
array = str.ToCharArray(0, str.Count());
int[] into2ndarray = new int[] { };
Dictionary<char, int> keySizes = new Dictionary<char, int> {
{ 'A', 2 },
{ 'B', 2 },
{ 'C', 2 }
};
int arraySize;
foreach (char c in array)
{
if (keySizes.TryGetValue(c, out arraySize))
{
into2ndarray = new int[arraySize];
break;
}
}
return into2ndarray;
}
publicstatic int[]ConvertStringToArray()
{
字符串str=“ABCA”;
字符[]数组;
array=str.ToCharArray(0,str.Count());
int[]into2ndarray=新int[]{};
Dictionary keySizes=新字典{
{'A',2},
{'B',2},
{'C',2}
};
内部阵列化;
foreach(数组中的字符c)
{
if(keySizes.TryGetValue(c,out arraySize))
{
into2ndarray=新整数[arraySize];
打破
}
}
返回到第2道光线;
}
走自己的路
string a=“ABCA”;
字符[]数组;
数组=a.ToCharArray(0,a.length);
int[]into2ndarray=新的int[array.Length]//结果将用于另一次计算;由原始str的长度初始化。
int i=0;//计数器以保存元素
foreach(数组中的字符c){
开关(c){
案例“A”:
into2ndarray[i]=1;//按索引访问当前元素;
打破
案例“B”:
into2ndarray[i]=2;
打破
案例“C”:
into2ndarray[i]=3;
打破
}
i++;//增加计数器
}
但就我个人而言,我倾向于:
into2ndarray=array.Where(c=>c(int)(c-'A')+1.ToArray();
走自己的路
string a=“ABCA”;
字符[]数组;
数组=a.ToCharArray(0,a.length);
int[]into2ndarray=新的int[array.Length]//结果将用于另一次计算;由原点的长度初始化