如何在 C# 中将字符串转换为整数数组
How to Convert a String into Integer Array in C#
我是 C# 新手。我正在尝试解决一个我必须找到非耦合整数的问题。我正在使用在线编译器。
给定输入
1, 2, 3, 1, 2
程序应该输出:
3
我创建了以下程序 -
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int[] num;
int accum=0;
int j=0;
string input = Console.ReadLine();
input = input.Replace(",","");
num = input.Select(int.Parse).ToArray();
for (int i = 0; i < num.length; i++)
accum ^= num[i];
Console.WriteLine(accum);
}
}
我在将字符串 1, 2, 3, 1, 2
转换为整数数组时遇到问题。
我有以下错误
error CS1061: Type `string' does not contain a definition for `Select' and no extension method `Select' of type `string' could be found. Are you missing `System.Reactive.Linq' or `System.Linq' using directive?
/usr/lib/mono/4.7.2-api/mscorlib.dll (Location of the symbol related to previous error)
Solution.cs(18,29): error CS1061: Type `int[]' does not contain a definition for `length' and no extension method `length' of type `int[]' could be found. Are you missing an assembly reference?
/usr/lib/mono/4.7.2-api/mscorlib.dll (Location of the symbol related to previous error)
当我添加System.linq
时,它给了我
error CS0234: The type or namespace name `linq' does not exist in the namespace `System'. Are you missing an assembly reference?
Select method 适用于可枚举类型,不适用于 string
类型。在尝试将每个元素转换为 int
.
之前,您需要将字符串输入转换为字符串数组
string input = Console.ReadLine();
string[] inputs = input.Split(',')
int[] num = inputs.Select(x => int.Parse(x.Trim())).ToArray();
试试这个,
static void Main(string[] args)
{
int[] num;
int accum = 0;
string input = Console.ReadLine();
num = Array.ConvertAll(input.Split(','), int.Parse);
for (int i = 0; i < num.Length; i++)
accum ^= num[i];
Console.WriteLine(accum);
}
或者使用Linq,如下替换即可,需要使用System.Linq
命名空间。
num = input.Split(',').Select(int.Parse).ToArray();
.NetFiddle : https://dotnetfiddle.net/bahpg9
下面提供了一个扩展方法来检查是否所有输入的值都可以是 int,如果可以,则创建一个 int 数组。
Working example and class project double、floats 等类似代码
代码比之前的回复多很多,但这里提供的代码不只是一个操作,放在一个class项目中,然后可以被其他项目使用。
public static class NumericArray
{
/// <summary>
/// Determine if each element in the string array can represent an int
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
public static bool AllInt(this string[] sender) =>
sender.SelectMany(item => item.ToCharArray()).All(char.IsNumber);
/// <summary>
/// Convert a string array to an int array
/// </summary>
/// <param name="sender">string array which can represent integers</param>
/// <returns>int array</returns>
public static int[] ToIntegerArray(this string[] sender)
{
var intArray = Array
.ConvertAll(sender,
(input) => new
{
IsInteger = int.TryParse(input, out var integerValue),
Value = integerValue
})
.Where(result => result.IsInteger)
.Select(result => result.Value)
.ToArray();
return intArray;
}
/// <summary>
/// Return a string array without spaces in elements
/// </summary>
/// <param name="sender"></param>
/// <param name="separator">deliminator</param>
/// <returns></returns>
public static string[] Strings(this string sender, char separator = ',')
=> sender.RemoveAllWhiteSpace().Split(separator);
/// <summary>
/// Remove all whitespace from a string
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
public static string RemoveAllWhiteSpace(this string sender)
=> sender
.ToCharArray().Where(character => !char.IsWhiteSpace(character))
.Select(c => c.ToString()).Aggregate((value1, value2) => value1 + value2);
}
在这种情况下模拟输入的示例使用。
namespace ConsoleNetCoreApp1
{
class Program
{
static void Main(string[] args)
{
/*
* This would be string userInput = Console.ReadLine();
*/
string userInput = "1, 2, 3, 1, 2";
var result = userInput.Strings();
if (result.AllInt())
{
var intArray = result.ToIntegerArray();
foreach (var intItem in intArray)
{
Console.WriteLine($"{intItem}");
}
}
else
{
Console.WriteLine("One or more values can not represent an int");
}
}
}
}
我是 C# 新手。我正在尝试解决一个我必须找到非耦合整数的问题。我正在使用在线编译器。
给定输入
1, 2, 3, 1, 2
程序应该输出:
3
我创建了以下程序 -
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int[] num;
int accum=0;
int j=0;
string input = Console.ReadLine();
input = input.Replace(",","");
num = input.Select(int.Parse).ToArray();
for (int i = 0; i < num.length; i++)
accum ^= num[i];
Console.WriteLine(accum);
}
}
我在将字符串 1, 2, 3, 1, 2
转换为整数数组时遇到问题。
我有以下错误
error CS1061: Type `string' does not contain a definition for `Select' and no extension method `Select' of type `string' could be found. Are you missing `System.Reactive.Linq' or `System.Linq' using directive?
/usr/lib/mono/4.7.2-api/mscorlib.dll (Location of the symbol related to previous error)
Solution.cs(18,29): error CS1061: Type `int[]' does not contain a definition for `length' and no extension method `length' of type `int[]' could be found. Are you missing an assembly reference?
/usr/lib/mono/4.7.2-api/mscorlib.dll (Location of the symbol related to previous error)
当我添加System.linq
时,它给了我
error CS0234: The type or namespace name `linq' does not exist in the namespace `System'. Are you missing an assembly reference?
Select method 适用于可枚举类型,不适用于 string
类型。在尝试将每个元素转换为 int
.
string input = Console.ReadLine();
string[] inputs = input.Split(',')
int[] num = inputs.Select(x => int.Parse(x.Trim())).ToArray();
试试这个,
static void Main(string[] args)
{
int[] num;
int accum = 0;
string input = Console.ReadLine();
num = Array.ConvertAll(input.Split(','), int.Parse);
for (int i = 0; i < num.Length; i++)
accum ^= num[i];
Console.WriteLine(accum);
}
或者使用Linq,如下替换即可,需要使用System.Linq
命名空间。
num = input.Split(',').Select(int.Parse).ToArray();
.NetFiddle : https://dotnetfiddle.net/bahpg9
下面提供了一个扩展方法来检查是否所有输入的值都可以是 int,如果可以,则创建一个 int 数组。
Working example and class project double、floats 等类似代码
代码比之前的回复多很多,但这里提供的代码不只是一个操作,放在一个class项目中,然后可以被其他项目使用。
public static class NumericArray
{
/// <summary>
/// Determine if each element in the string array can represent an int
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
public static bool AllInt(this string[] sender) =>
sender.SelectMany(item => item.ToCharArray()).All(char.IsNumber);
/// <summary>
/// Convert a string array to an int array
/// </summary>
/// <param name="sender">string array which can represent integers</param>
/// <returns>int array</returns>
public static int[] ToIntegerArray(this string[] sender)
{
var intArray = Array
.ConvertAll(sender,
(input) => new
{
IsInteger = int.TryParse(input, out var integerValue),
Value = integerValue
})
.Where(result => result.IsInteger)
.Select(result => result.Value)
.ToArray();
return intArray;
}
/// <summary>
/// Return a string array without spaces in elements
/// </summary>
/// <param name="sender"></param>
/// <param name="separator">deliminator</param>
/// <returns></returns>
public static string[] Strings(this string sender, char separator = ',')
=> sender.RemoveAllWhiteSpace().Split(separator);
/// <summary>
/// Remove all whitespace from a string
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
public static string RemoveAllWhiteSpace(this string sender)
=> sender
.ToCharArray().Where(character => !char.IsWhiteSpace(character))
.Select(c => c.ToString()).Aggregate((value1, value2) => value1 + value2);
}
在这种情况下模拟输入的示例使用。
namespace ConsoleNetCoreApp1
{
class Program
{
static void Main(string[] args)
{
/*
* This would be string userInput = Console.ReadLine();
*/
string userInput = "1, 2, 3, 1, 2";
var result = userInput.Strings();
if (result.AllInt())
{
var intArray = result.ToIntegerArray();
foreach (var intItem in intArray)
{
Console.WriteLine($"{intItem}");
}
}
else
{
Console.WriteLine("One or more values can not represent an int");
}
}
}
}