当实例化的值是字符串时,如何创建新的 Vector4 对象?
How do I create new Vector4 objects when the values for instantiation are in strings?
我想创建一堆 Vector4 对象,实例化的值是从存储在字符串列表中的配置文件中提取的。
字符串的值如 "255, 0, 0 255" 或 "0, 0 255, 255" 等
假设我在一个 foreach 循环中并且我即将创建这些 Vector4 对象,我将如何解析这些字符串以便我可以提取出每个单独的整数并将其用于实例化,例如:
Vector4 v1 = new Vector4(255, 0, 0 255);
Vector4 v2 = new Vector4(0, 0, 255, 255);
请记住,我想让它成为一个自动过程,因此我有一个包含所有 Vector 值的配置文件。
拆分字符串,解析各个数字,并将它们传递给构造函数。
string input = "255, 0, 0, 255";
var nums = input.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries)
.Select(float.Parse)
.ToList();
Vector4 v1 = new Vector4(nums[0], nums[1], nums[2], nums[3]);
我喜欢 Grant Winney 提供的解决方案,但它无法处理 Steven Chen 要求的所有格式要求。分隔符也必须是 space 本身。
此外,我喜欢将这些东西包装到扩展方法中:)。
所以这里是稍微改进的版本,对错误的格式进行了一些测试,导致异常。
public static class StringVector4Extensions
{
public static Vector4 ToVector4(this string str, params string[] delimiters)
{
if (str == null) throw new ArgumentNullException("string is null");
if (string.IsNullOrEmpty(str)) throw new FormatException("string is empty");
if (string.IsNullOrWhiteSpace(str)) throw new FormatException("string is just white space");
if (delimiters == null) throw new ArgumentNullException("delimiters are null");
if (delimiters.Length <= 0) throw new InvalidOperationException("missing delimiters");
var rslt = str
.Split(delimiters, StringSplitOptions.RemoveEmptyEntries)
.Select(float.Parse)
.ToArray()
;
if (rslt.Length != 4)
throw new FormatException( "The input string does not follow" +
"the required format for the string." +
"There has to be four floats inside" +
"the string delimited by one of the" +
"requested delimiters. input string: " +
str );
return new Vector4(rslt[0], rslt[1], rslt[2], rslt[3]);
}
}
使用起来很简单:
var a = "255, 0, 0 255".ToVector4(",", " ");
var b = "0, 0 255, 255".ToVector4(",", " ");
我想创建一堆 Vector4 对象,实例化的值是从存储在字符串列表中的配置文件中提取的。
字符串的值如 "255, 0, 0 255" 或 "0, 0 255, 255" 等
假设我在一个 foreach 循环中并且我即将创建这些 Vector4 对象,我将如何解析这些字符串以便我可以提取出每个单独的整数并将其用于实例化,例如:
Vector4 v1 = new Vector4(255, 0, 0 255);
Vector4 v2 = new Vector4(0, 0, 255, 255);
请记住,我想让它成为一个自动过程,因此我有一个包含所有 Vector 值的配置文件。
拆分字符串,解析各个数字,并将它们传递给构造函数。
string input = "255, 0, 0, 255";
var nums = input.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries)
.Select(float.Parse)
.ToList();
Vector4 v1 = new Vector4(nums[0], nums[1], nums[2], nums[3]);
我喜欢 Grant Winney 提供的解决方案,但它无法处理 Steven Chen 要求的所有格式要求。分隔符也必须是 space 本身。
此外,我喜欢将这些东西包装到扩展方法中:)。 所以这里是稍微改进的版本,对错误的格式进行了一些测试,导致异常。
public static class StringVector4Extensions
{
public static Vector4 ToVector4(this string str, params string[] delimiters)
{
if (str == null) throw new ArgumentNullException("string is null");
if (string.IsNullOrEmpty(str)) throw new FormatException("string is empty");
if (string.IsNullOrWhiteSpace(str)) throw new FormatException("string is just white space");
if (delimiters == null) throw new ArgumentNullException("delimiters are null");
if (delimiters.Length <= 0) throw new InvalidOperationException("missing delimiters");
var rslt = str
.Split(delimiters, StringSplitOptions.RemoveEmptyEntries)
.Select(float.Parse)
.ToArray()
;
if (rslt.Length != 4)
throw new FormatException( "The input string does not follow" +
"the required format for the string." +
"There has to be four floats inside" +
"the string delimited by one of the" +
"requested delimiters. input string: " +
str );
return new Vector4(rslt[0], rslt[1], rslt[2], rslt[3]);
}
}
使用起来很简单:
var a = "255, 0, 0 255".ToVector4(",", " ");
var b = "0, 0 255, 255".ToVector4(",", " ");