C#将字符串转换为大小
有没有办法将字符串变量转换为数据类型大小 例如,如果我有这个变量 字符串大小 我如何将其转换为数据类型大小以便于读取???可能:C#将字符串转换为大小,c#,C#,有没有办法将字符串变量转换为数据类型大小 例如,如果我有这个变量 字符串大小 我如何将其转换为数据类型大小以便于读取???可能: int size=Convert.ToInt32(tempSize); 已更新 必须在两个int中解析tempSize,然后调用: Size = New Drawing.Size(xSize, ySize) 你指的是什么尺寸 如果你是说,有一种方法 如果您的意思是,没有内置的解析方法,那么您必须自己解析字符串。大小是一个具有宽度和高度的结构 您需要两个整数来生成
int size=Convert.ToInt32(tempSize);
已更新
必须在两个int中解析tempSize,然后调用:
Size = New Drawing.Size(xSize, ySize)
你指的是什么尺寸 如果你是说,有一种方法
如果您的意思是,没有内置的解析方法,那么您必须自己解析字符串。大小是一个具有宽度和高度的结构 您需要两个整数来生成它,使用Size(width,height)构造函数
您应该找到一种使用Int32.parse或其他方法从字符串中提取整数的方法。使用提取的整数来构造大小。目前最好的可读和可解析格式是JSON 因此,您可以使用将Size对象从和转换为JSON。由于Size.ToString()的默认实现也提供了相当好的输出,因此您可以使用:
var json = new JavaScriptSerializer();
var s1 = new Size(5, 125);
var serialized = s1.ToString().Replace("=",":");
Console.WriteLine(serialized);
var s2 = json.Deserialize<Size>(serialized);
Console.WriteLine(s2);
我猜您有一个格式为“123;456”的字符串,因为这是Size struct使用的格式 要将其解析为,您可以执行以下操作:
string s = "123; 456";
int width, height;
string[] dims = s.Split(';');
int.TryParse(dims[0], out width);
int.TryParse(dims[1], out height);
System.Drawing.Size size = new System.Drawing.Size(width, height);
如果字符串没有
,则此操作将失败代码>,如果字符串不可解析,将返回数字0。我已成功调用类似于“1100700”格式的字符串上的System.Windows.Size.Parse。我们正在讨论Winforms中使用的System.Drawing.Size结构,是吗
如上所述,此结构没有Parse()函数。所以你得自己做。一种选择是格式化和解析您自己设计的字符串:
// format a custom string to encode your size
string mySizeString = string.Format("{0}:{1}", mySize.Width, mySize.Height);
// parse your custom string and make a new size
string[] sizeParts = mySizeString.Split(':');
int height = int.Parse(sizeParts[0]);
int width = int.Parse(sizeParts[1]);
Size mySize = new Size(height, width);
另一种选择是使用TypeConverter类为您进行格式化和解析:
// convert a Size to a string using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
string mySizeString = tc.ConvertToString(mySize);
// convert a string to a Size using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
Size mySize = (Size)tc.ConvertFromString(mySizeString);
也许最后一个解决方案正好适合您(har-har)。在任何情况下,我相信一些聪明人已经编写了扩展函数,以这种方式扩展Size结构。那太好了 现在,您可以使用扩展方法为string类创建解析方法:
public static Size Parse(this string str) ...
让我展示一些我必须编写的代码,因为大小应该通过WxH形式的URL参数传入,例如1024x768:
namespace MySize
{
public static class Extensions
{
public static Size Parse(this string str)
{
try {
var a = str.Split(new char[] { 'x' });
return new Size() {
Width = int.Parse(a[0]),
Height = int.Parse(a[1])
};
}
catch(Exception) { }
return Size.nosize;
}
}
public class Size
{
public int Width { get; set; }
public int Height { get; set; }
public override string ToString()
{
return Width.ToString() + "x" + Height.ToString();
}
public Size(System.Drawing.Size from)
{
Width = from.Width;
Height = from.Height;
}
public Size()
{
}
public static Size nosize = new Size(new System.Drawing.Size(-1, -1));
}
//[..]
}
这样,您就可以对类使用ToString()方法,对string(或string)使用相应的解析方法。下面是一个测试用例:
[TestMethod]
public void TestSizeParse()
{
var s1 = new Size(1024, 768);
var s1Str = s1.ToString();
Size s2 = s1Str.Parse();
Assert.AreEqual(s1.Width, s2.Width);
Assert.AreEqual(s1.Height, s2.Height);
Assert.AreEqual(s1, s2, "s1 and s2 are supposed to be equal");
}
什么尺寸?在哪个图书馆?你为什么需要这个?这毫无意义。什么数据类型是大小?从没听说过。。。你的意思是sizeof(string)
?根据MSDN,C#似乎缺少任何类型的Size
。你是说长度吗?@Jeremy B.-我想他指的是尺寸结构:在你发布更多信息后,我更新了我的帖子,你的问题不太清楚。。。为什么要对我们所有人进行向下投票?根据当前的文化,可能需要使用具有不变字符串的SizeConverter。在我的系统上,system.Windows.Size.Parse(Size.ToString())引发FormatException。
[TestMethod]
public void TestSizeParse()
{
var s1 = new Size(1024, 768);
var s1Str = s1.ToString();
Size s2 = s1Str.Parse();
Assert.AreEqual(s1.Width, s2.Width);
Assert.AreEqual(s1.Height, s2.Height);
Assert.AreEqual(s1, s2, "s1 and s2 are supposed to be equal");
}