如何将字符串拆分为两个separte变量
问题描述:
如果我有一个字符串值IMG10001,并希望将其拆分为两个单独的变量。
IMG10001 = IMG1 0001
原价值1值2
字符串
感谢您的任何输入。
If I have a string value of IMG10001, and like to split it into two separate variable.
IMG10001 = IMG1 0001
Original value1 value2
string
Thank you for any input.
答
我不是一个编码器或专家来问一个关于C#的好问题,我只是介绍我如何理解如何将此字符串IMG10001拆分为C#代码上的单独字符串或字段。 b $ b
I am not a coder or an expert to ask a good question about C# , I am just presenting how I understand on how to split this string IMG10001 to became separate string or field on C # code.
field1 = IMG1
field2 = 0001
啊!这更有意义。根据您正在处理的数据,有很多方法可以做到这一点。如果它是固定长度 - 总是3个字母后跟一堆numeris,那么使用String.Substring方法:
Ah! That makes a bit more sense. There are a large number of ways to do it, depending on the data you are dealing with. If it is fixed length - always 3 alphabetic followed by a bunch of numeris, then use the String.Substring method:
string inp = "IMG0001";
string prefix = inp.Substring(0, 3);
string suffix = inp.Substring(3);
或者,如果数据的长度不同,那么一个简单的Regex会这样做:
Alternatively, if the data can vary in length, then a simple Regex would do it:
Regex reg = new Regex(@"(?<prefix>[A-Za-z]+)(?<suffix>\d+)");
Match m = reg.Match(inp);
if (m.Success)
{
prefix = m.Groups["Prefix"].Value;
suffix = m.Groups["Suffix"].Value;
}</suffix></prefix>
这些应该有所帮助,
http://msdn.microsoft.com /en-us/library/system.string.substring(v=vs.71).aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/b873y76a .aspx [ ^ ]
有了这么少的信息,真的不能给你mu ch more,
These should help,
http://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.71).aspx[^]
http://msdn.microsoft.com/en-us/library/b873y76a.aspx[^]
With that little of information cant really give you much more,
您可以使用 http://msdn.microsoft .com / zh-CN / library / ms228388.aspx [ ^ ], http://msdn.microsoft.com/en-us /library/c1bs0eda.aspx [ ^ ]
或尝试
http://stackoverflow.com/questions/7378028/c-sharp-split-string-into-separate-variables [ ^ ]
http://forums.asp.net/t/1703097.aspx/1 [ ^ ]
You can use http://msdn.microsoft.com/en-us/library/ms228388.aspx[^], http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx[^]
or try
http://stackoverflow.com/questions/7378028/c-sharp-split-string-into-separate-variables[^]
http://forums.asp.net/t/1703097.aspx/1[^]