且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将字符串拆分为两个separte变量

更新时间:2023-01-19 21:32:20

我不是一个编码器或专家来问一个关于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://***.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://***.com/questions/7378028/c-sharp-split-string-into-separate-variables[^]
http://forums.asp.net/t/1703097.aspx/1[^]