如何将类似var numbers = "2016, 2017, 2018";
的字符串转换为List<int>
?
我已经尝试过了:
List<int> years = Int32.Parse(yearsString.Split(',')).ToList();
但是我收到以下错误消息:
无法从字符串[]转换为字符串。
答案 0 :(得分:7)
类似这样的东西
var results = yearsString.Split(',').Select(x => int.Parse(x.Trim()));
注意:这种方法没有容错功能,如果无法将其转换为int
,则会出现异常
有趣的是,这是一种使用指针unsafe
和fixed
的方法
fixed (char* pInput = Input)
{
var len = pInput + Input.Length;
var current = 0;
var results = new List<int>();
for (var p = pInput; p < len; p++)
{
if (*p >= 48 & *p <= 58)
current = current * 10 + *p - 48;
else if (*p == ',')
{
results.Add(current);
current = 0;
}
}
results.Add(current);
return results;
}
只是因为我很无聊
----------------------------------------------------------------------------
Mode : Release (64Bit)
Test Framework : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version : 10.0.17134
----------------------------------------------------------------------------
CPU Name : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads) : 4 (8) : Architecture : x64
Clock Speed : 3401 MHz : Bus Speed : 100 MHz
L2Cache : 1 MB : L3Cache : 8 MB
----------------------------------------------------------------------------
测试1
--- Standard input --------------------------------------------------------
| Value | Average | Fastest | Cycles | Garbage | Test | Gain |
--- Scale 100 ---------------------------------------------- Time 0.003 ---
| Unsafe | 2.304 µs | 2.101 µs | 9.298 K | 0.000 B | Pass | 90.28 % |
| Linq | 23.711 µs | 22.214 µs | 82.700 K | 0.000 B | Base | 0.00 % |
--- Scale 1,000 -------------------------------------------- Time 0.046 ---
| Unsafe | 21.697 µs | 21.013 µs | 75.490 K | 0.000 B | Pass | 90.41 % |
| Linq | 226.218 µs | 205.332 µs | 768.447 K | 0.000 B | Base | 0.00 % |
--- Scale 10,000 ------------------------------------------- Time 0.250 ---
| Unsafe | 214.526 µs | 200.829 µs | 733.557 K | 0.000 B | Pass | 89.93 % |
| Linq | 2.130 ms | 1.996 ms | 7.257 M | 0.000 B | Base | 0.00 % |
--- Scale 100,000 ------------------------------------------ Time 2.906 ---
| Unsafe | 2.303 ms | 2.063 ms | 7.680 M | 0.000 B | Pass | 90.99 % |
| Linq | 25.571 ms | 22.624 ms | 84.808 M | 0.000 B | Base | 0.00 % |
--- Scale 1,000,000 --------------------------------------- Time 36.594 ---
| Unsafe | 23.061 ms | 21.910 ms | 78.356 M | 0.000 B | Pass | 93.07 % |
| Linq | 332.639 ms | 274.595 ms | 1.055 B | 0.000 B | Base | 0.00 % |
---------------------------------------------------------------------------
答案 1 :(得分:0)
您需要那个;
2018-07-23 15:02:04.897 [SimpleAsyncTaskExecutor-23] DEBUG o.s.i.c.DirectChannel.send(289) - postSend (sent=true) on channel 'sdpJobInboundFlow.channel#0', message: GenericMessage [payload=byte[267], headers={amqp_receivedExchange=Oidc_Job, amqp_deliveryTag=9734, amqp_replyTo=Oidc_Sdp_Job_Report-OidcCore1, amqp_consumerQueue=Oidc_Sdp_Job_V6, amqp_redelivered=false, amqp_receivedRoutingKey=Oidc_Sdp_Job_V6, amqp_contentEncoding=UTF-8, json__TypeId__=com.kt.smcp.lib.rabbitmq.model.SdpJobCommand, amqp_deliveryMode=PERSISTENT, amqp_correlationId=[B@5270fb0b, id=396614b7-a89e-5059-517c-53b4194315a4, amqp_consumerTag=amq.ctag-KtcSpKywXf5PXY_558W9Wg, contentType=application/json, timestamp=1532325724896}]
2018-07-23 15:02:04.897 [apijobdispatcher-183] DEBUG r.s.c.c.ConsumerBeanAutoConfiguration.apply(334) - Invoking method[public void com.kt.smcp.core.apiworker.service.OidcSdpJobService.doOidcSdpJob3Sec(reactor.event.Event)] on class com.kt.smcp.core.apiworker.service.OidcSdpJobService using Event{id=null, headers=null, replyTo=null, key=oidc_sdp_job, data=com.kt.smcp.core.apiworker.model.OidcJobEvent@1a59754f}
2018-07-23 15:02:04.898 [apijobdispatcher-183] DEBUG o.a.w.d.h.WSHandler.doSenderAction(215) - Performing Action: 1
**2018-07-23 15:02:04.898** [apijobdispatcher-183] DEBUG o.a.w.d.m.WSSecUsernameToken.build(232) - Begin add username token...
**2018-07-23 15:03:05.891** [SimpleAsyncTaskExecutor-22] DEBUG o.s.a.r.c.RabbitTemplate.onMessage(1305) - Reply received for 9927bc46-15b5-4f33-9ee7-02fe57af6ad8
2018-07-23 15:03:05.891 [async-8410] DEBUG o.s.a.r.c.CachingConnectionFactory.physicalClose(689) - Closing cached Channel: AMQChannel(amqp://smap@172.31.55.53:5672/,1369)
2018-07-23 15:03:05.892 [SimpleAsyncTaskExecutor-21] DEBUG o.s.a.r.c.RabbitTemplate.onMessage(1305) - Reply received for 963ceada-6fcd-47b1-973e-22cf5b8ce609
2018-07-23 15:03:05.892 [async-8403] DEBUG o.s.a.r.c.CachingConnectionFactory.physicalClose(689) - Closing cached Channel: AMQChannel(amqp://smap@~~~.53:5672/,1353)
2018-07-23 15:03:05.892 [SimpleAsyncTaskExecutor-23] DEBUG o.s.a.r.c.RabbitTemplate.onMessage(1305) - Reply received for e2f93078-7b8b-44d8-aeff-603e6dc756ff
2018-07-23 15:03:05.892 [async-8377] DEBUG o.s.a.r.c.CachingConnectionFactory.physicalClose(689) - Closing cached Channel: AMQChannel(amqp://smap@~~:5672/,1366)
2018-07-23 15:03:05.892 [SimpleAsyncTaskExecutor-24] DEBUG o.s.a.r.c.RabbitTemplate.onMessage(1305) - Reply received for 8093f6f8-1d52-4983-a5b7-45b87729b0fe
2018-07-23 15:03:05.892 [async-8358] DEBUG o.s.a.r.c.CachingConnectionFactory.physicalClose(689) - Closing cached Channel: AMQChannel(amqp://smap@~~.53:5672/,1241)
2018-07-23 15:03:05.925 [async-8291] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.925 [async-8390] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.925 [async-8371] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.994 [async-8417] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.994 [async-8407] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.994 [async-8174] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.995 [async-8408] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.995 [async-8352] DEBUG o.s.d.r.c.RedisConnectionUtils.doGetConnection(125) - Opening RedisConnection
2018-07-23 15:03:05.995 [SimpleAsyncTaskExecutor-27] DEBUG o.s.i.c.DirectChannel.send(277) - preSend on channel 'sdpJobInboundFlow.channel#0', message: GenericMessage [payload=byte[267], headers={amqp_receivedExchange=Oidc_Job, amqp_deliveryTag=9734, amqp_replyTo=Oidc_Sdp_Job_Report-OidcCore2, amqp_consumerQueue=Oidc_Sdp_Job_V6, amqp_redelivered=false, amqp_receivedRoutingKey=Oidc_Sdp_Job_V6, amqp_contentEncoding=UTF-8, json__TypeId__=com.kt.smcp.lib.rabbitmq.model.SdpJobCommand, amqp_deliveryMode=PERSISTENT, amqp_correlationId=[B@655bb986, id=94f39be5-2d78-746c-8b0c-de6eaf532b2d, amqp_consumerTag=amq.ctag-kPDY7lHahXLq8kpOiiUOug, contentType=application/json, timestamp=1532325785994}]
2018-07-23 15:03:05.995 [SimpleAsyncTaskExecutor-24] DEBUG o.s.i.c.DirectChannel.send(277) - preSend on channel 'sdpJobInboundFlow.channel#0', message: GenericMessage [payload=byte[267], headers={amqp_receivedExchange=Oidc_Job, amqp_deliveryTag=9734, amqp_replyTo=Oidc_Sdp_Job_Report-OidcCore2, amqp_consumerQueue=Oidc_Sdp_Job_V6, amqp_redelivered=false, amqp_receivedRoutingKey=Oidc_Sdp_Job_V6, amqp_contentEncoding=UTF-8, json__TypeId__=com.kt.smcp.lib.rabbitmq.model.SdpJobCommand, amqp_deliveryMode=PERSISTENT, amqp_correlationId=[B@454ef987, id=a54c9126-5a2e-6059-50ed-a25375ff0f03, amqp_consumerTag=amq.ctag-o5WQ2o4bDiZSGV1pJp5itA, contentType=application/json, timestamp=1532325785995}]
2018-07-23 15:03:05.995 [SimpleAsyncTaskExecutor-25] DEBUG o.s.i.c.DirectChannel.send(277) - preSend on channel 'sdpJobInboundFlow.channel#0', message: GenericMessage [payload=byte[267], headers={amqp_receivedExchange=Oidc_Job, amqp_deliveryTag=9734, amqp_replyTo=Oidc_Sdp_Job_Report-OidcCore1, amqp_consumerQueue=Oidc_Sdp_Job_V6, amqp_redelivered=false, amqp_receivedRoutingKey=Oidc_Sdp_Job_V6, amqp_contentEncoding=UTF-8, json__TypeId__=com.kt.smcp.lib.rabbitmq.model.SdpJobCommand, amqp_deliveryMode=PERSISTENT, amqp_correlationId=[B@36921f77, id=d6b5e5d1-5d2d-e6ec-b6bc-77739a19ccc7, amqp_consumerTag=amq.ctag-rbgr9o7XB57kW11CQg1a0w, contentType=application/json, timestamp=1532325785995}]