[C#] 计算字符串在控制台上显示的宽度, 包含所有Char能表示的字符!

通过运算, 获取字符串在控制台上现实的宽度(单位为一个英文字母的宽度, 高度为控制台中一行的高度)在网上找了半天, 唯一一个正儿八经的, 就是通过GBK编码的字节数来推测所占宽度, 但我认为这个有点不大妥当, 例如某些特殊字符, 可能就不被GBK编码包含.所以, 我这里提供了一个可获取字符串显示宽度的可行方法.首先, 我通过循环C#中所有的字符, 并将其打印到控制台上, 运算单个字符所占宽度, 最终得出了一个List<int>, 通过这个列表, 只需要以字符强制转换为int的.

  • 通过运算, 获取字符串在控制台上现实的宽度(单位为一个英文字母的宽度, 高度为控制台中一行的高度)
  • 在网上找了半天, 唯一一个正儿八经的, 就是通过GBK编码的字节数来推测所占宽度, 但我认为这个有点不大妥当, 例如某些特殊字符, 可能就不被GBK编码包含.
  • 所以, 我这里提供了一个可获取字符串显示宽度的可行方法. Github 仓库地址: Null.ConsoleEx ==重点==

实现过程:

  1. 首先, 我通过循环C#中所有的字符, 并将其打印到控制台上, 运算单个字符所占宽度, 最终得出了一个List<int>, 通过这个列表, 只需要以字符强制转换为int的值作为索引, 访问对应的元素, 即可得出字符对应宽度
1
2
3
4
5
6
7
int GetCharLen(char c)
{
    Console.CursorLeft = Console.BufferWidth / 2 - 1;
    int start = Console.CursorLeft;
    Console.Write(c);
    return Console.CursorLeft - start;
}
  1. 之后, 再获取区间, 稍加处理, 即得到可获取char所有字符在控制台中显示的宽度.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
public static int GetCharDisplayLength(char c)
{
    if (index < 7)
        return 1;
    else if (index < 9)
        return 0;
    else if (index < 10)
        return 8;
    else if (index < 11)
        return 0;
    else if (index < 13)
        return 1;
    else if (index < 14)
        return 0;
    else if (index < 162)
        return 1;
    else if (index < 166)
        return 2;
    else if (index < 167)
        return 1;
    else if (index < 169)
        return 2;
    else if (index < 175)
        return 1;
    else if (index < 178)
        return 2;
    else if (index < 180)
        return 1;
    else if (index < 182)
        return 2;
    else if (index < 183)
        return 1;
    else if (index < 184)
        return 2;
    else if (index < 215)
        return 1;
    else if (index < 216)
        return 2;
    else if (index < 247)
        return 1;
    else if (index < 248)
        return 2;
    else if (index < 449)
        return 1;
    else if (index < 450)
        return 2;
    else if (index < 711)
        return 1;
    else if (index < 712)
        return 2;
    else if (index < 713)
        return 1;
    else if (index < 716)
        return 2;
    else if (index < 729)
        return 1;
    else if (index < 730)
        return 2;
    else if (index < 913)
        return 1;
    else if (index < 930)
        return 2;
    else if (index < 931)
        return 1;
    else if (index < 938)
        return 2;
    else if (index < 945)
        return 1;
    else if (index < 962)
        return 2;
    else if (index < 963)
        return 1;
    else if (index < 970)
        return 2;
    else if (index < 1025)
        return 1;
    else if (index < 1026)
        return 2;
    else if (index < 1040)
        return 1;
    else if (index < 1104)
        return 2;
    else if (index < 1105)
        return 1;
    else if (index < 1106)
        return 2;
    else if (index < 8208)
        return 1;
    else if (index < 8209)
        return 2;
    else if (index < 8211)
        return 1;
    else if (index < 8215)
        return 2;
    else if (index < 8216)
        return 1;
    else if (index < 8218)
        return 2;
    else if (index < 8220)
        return 1;
    else if (index < 8222)
        return 2;
    else if (index < 8229)
        return 1;
    else if (index < 8231)
        return 2;
    else if (index < 8240)
        return 1;
    else if (index < 8241)
        return 2;
    else if (index < 8242)
        return 1;
    else if (index < 8244)
        return 2;
    else if (index < 8245)
        return 1;
    else if (index < 8246)
        return 2;
    else if (index < 8251)
        return 1;
    else if (index < 8252)
        return 2;
    else if (index < 8254)
        return 1;
    else if (index < 8255)
        return 2;
    else if (index < 8364)
        return 1;
    else if (index < 8365)
        return 2;
    else if (index < 8451)
        return 1;
    else if (index < 8452)
        return 2;
    else if (index < 8453)
        return 1;
    else if (index < 8454)
        return 2;
    else if (index < 8457)
        return 1;
    else if (index < 8458)
        return 2;
    else if (index < 8470)
        return 1;
    else if (index < 8471)
        return 2;
    else if (index < 8481)
        return 1;
    else if (index < 8482)
        return 2;
    else if (index < 8544)
        return 1;
    else if (index < 8556)
        return 2;
    else if (index < 8560)
        return 1;
    else if (index < 8570)
        return 2;
    else if (index < 8592)
        return 1;
    else if (index < 8596)
        return 2;
    else if (index < 8598)
        return 1;
    else if (index < 8602)
        return 2;
    else if (index < 8712)
        return 1;
    else if (index < 8713)
        return 2;
    else if (index < 8719)
        return 1;
    else if (index < 8720)
        return 2;
    else if (index < 8721)
        return 1;
    else if (index < 8722)
        return 2;
    else if (index < 8725)
        return 1;
    else if (index < 8726)
        return 2;
    else if (index < 8728)
        return 1;
    else if (index < 8729)
        return 2;
    else if (index < 8730)
        return 1;
    else if (index < 8731)
        return 2;
    else if (index < 8733)
        return 1;
    else if (index < 8737)
        return 2;
    else if (index < 8739)
        return 1;
    else if (index < 8740)
        return 2;
    else if (index < 8741)
        return 1;
    else if (index < 8742)
        return 2;
    else if (index < 8743)
        return 1;
    else if (index < 8748)
        return 2;
    else if (index < 8750)
        return 1;
    else if (index < 8751)
        return 2;
    else if (index < 8756)
        return 1;
    else if (index < 8760)
        return 2;
    else if (index < 8764)
        return 1;
    else if (index < 8766)
        return 2;
    else if (index < 8776)
        return 1;
    else if (index < 8777)
        return 2;
    else if (index < 8780)
        return 1;
    else if (index < 8781)
        return 2;
    else if (index < 8786)
        return 1;
    else if (index < 8787)
        return 2;
    else if (index < 8800)
        return 1;
    else if (index < 8802)
        return 2;
    else if (index < 8804)
        return 1;
    else if (index < 8808)
        return 2;
    else if (index < 8814)
        return 1;
    else if (index < 8816)
        return 2;
    else if (index < 8853)
        return 1;
    else if (index < 8854)
        return 2;
    else if (index < 8857)
        return 1;
    else if (index < 8858)
        return 2;
    else if (index < 8869)
        return 1;
    else if (index < 8870)
        return 2;
    else if (index < 8895)
        return 1;
    else if (index < 8896)
        return 2;
    else if (index < 8978)
        return 1;
    else if (index < 8979)
        return 2;
    else if (index < 9312)
        return 1;
    else if (index < 9322)
        return 2;
    else if (index < 9332)
        return 1;
    else if (index < 9372)
        return 2;
    else if (index < 9632)
        return 1;
    else if (index < 9634)
        return 2;
    else if (index < 9650)
        return 1;
    else if (index < 9652)
        return 2;
    else if (index < 9660)
        return 1;
    else if (index < 9662)
        return 2;
    else if (index < 9670)
        return 1;
    else if (index < 9672)
        return 2;
    else if (index < 9675)
        return 1;
    else if (index < 9676)
        return 2;
    else if (index < 9678)
        return 1;
    else if (index < 9680)
        return 2;
    else if (index < 9698)
        return 1;
    else if (index < 9702)
        return 2;
    else if (index < 9733)
        return 1;
    else if (index < 9735)
        return 2;
    else if (index < 9737)
        return 1;
    else if (index < 9738)
        return 2;
    else if (index < 9792)
        return 1;
    else if (index < 9793)
        return 2;
    else if (index < 9794)
        return 1;
    else if (index < 9795)
        return 2;
    else if (index < 12288)
        return 1;
    else if (index < 12292)
        return 2;
    else if (index < 12293)
        return 1;
    else if (index < 12312)
        return 2;
    else if (index < 12317)
        return 1;
    else if (index < 12319)
        return 2;
    else if (index < 12321)
        return 1;
    else if (index < 12330)
        return 2;
    else if (index < 12353)
        return 1;
    else if (index < 12436)
        return 2;
    else if (index < 12443)
        return 1;
    else if (index < 12447)
        return 2;
    else if (index < 12449)
        return 1;
    else if (index < 12535)
        return 2;
    else if (index < 12540)
        return 1;
    else if (index < 12543)
        return 2;
    else if (index < 12549)
        return 1;
    else if (index < 12586)
        return 2;
    else if (index < 12690)
        return 1;
    else if (index < 12704)
        return 2;
    else if (index < 12832)
        return 1;
    else if (index < 12868)
        return 2;
    else if (index < 12928)
        return 1;
    else if (index < 12958)
        return 2;
    else if (index < 12959)
        return 1;
    else if (index < 12964)
        return 2;
    else if (index < 12969)
        return 1;
    else if (index < 12977)
        return 2;
    else if (index < 13198)
        return 1;
    else if (index < 13200)
        return 2;
    else if (index < 13212)
        return 1;
    else if (index < 13215)
        return 2;
    else if (index < 13217)
        return 1;
    else if (index < 13218)
        return 2;
    else if (index < 13252)
        return 1;
    else if (index < 13253)
        return 2;
    else if (index < 13262)
        return 1;
    else if (index < 13263)
        return 2;
    else if (index < 13265)
        return 1;
    else if (index < 13267)
        return 2;
    else if (index < 13269)
        return 1;
    else if (index < 13270)
        return 2;
    else if (index < 19968)
        return 1;
    else if (index < 40870)
        return 2;
    else if (index < 55296)
        return 1;
    else if (index < 55297)
        return 0;
    else if (index < 56320)
        return 1;
    else if (index < 56321)
        return 2;
    else if (index < 57344)
        return 1;
    else if (index < 59335)
        return 2;
    else if (index < 59337)
        return 1;
    else if (index < 59493)
        return 2;
    else if (index < 63733)
        return 1;
    else if (index < 63734)
        return 2;
    else if (index < 63744)
        return 1;
    else if (index < 64046)
        return 2;
    else if (index < 65072)
        return 1;
    else if (index < 65074)
        return 2;
    else if (index < 65075)
        return 1;
    else if (index < 65093)
        return 2;
    else if (index < 65097)
        return 1;
    else if (index < 65107)
        return 2;
    else if (index < 65108)
        return 1;
    else if (index < 65112)
        return 2;
    else if (index < 65113)
        return 1;
    else if (index < 65127)
        return 2;
    else if (index < 65128)
        return 1;
    else if (index < 65132)
        return 2;
    else if (index < 65281)
        return 1;
    else if (index < 65375)
        return 2;
    else if (index < 65504)
        return 1;
    else if (index < 65510)
        return 2;
    else if (index < 65536)
        return 1;
    else
        return 0;
}

再稍加处理, 使得每一个字符所需进行的判断次数差异减少. (类似于二叉树), 下面这个代码直接就能用哦~ 而且是包含Char能表示的所有字符的.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
public static int GetCharDisplayLength(char c)
{
    int index = c;
    if (index < 8800)
    {
        if (index < 8246)
        {
            if (index < 913)
            {
                if (index < 183)
                {
                    if (index < 166)
                    {
                        if (index < 11)
                        {
                            if (index < 9)
                            {
                                if (index < 7)
                                    return 1;
                                else
                                    return 0;
                            }
                            else
                            {
                                if (index < 10)
                                    return 8;
                                else
                                    return 0;
                            }
                        }
                        else
                        {
                            if (index < 14)
                            {
                                if (index < 13)
                                    return 1;
                                else
                                    return 0;
                            }
                            else
                            {
                                if (index < 162)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 178)
                        {
                            if (index < 169)
                            {
                                if (index < 167)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 175)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 182)
                            {
                                if (index < 180)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 450)
                    {
                        if (index < 247)
                        {
                            if (index < 215)
                            {
                                if (index < 184)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 216)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 449)
                            {
                                if (index < 248)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 716)
                        {
                            if (index < 712)
                            {
                                if (index < 711)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 713)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 730)
                            {
                                if (index < 729)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
            }
            else
            {
                if (index < 8209)
                {
                    if (index < 1025)
                    {
                        if (index < 945)
                        {
                            if (index < 931)
                            {
                                if (index < 930)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 938)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 963)
                            {
                                if (index < 962)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 970)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 1105)
                        {
                            if (index < 1040)
                            {
                                if (index < 1026)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 1104)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8208)
                            {
                                if (index < 1106)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 8229)
                    {
                        if (index < 8218)
                        {
                            if (index < 8215)
                            {
                                if (index < 8211)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8216)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8222)
                            {
                                if (index < 8220)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8242)
                        {
                            if (index < 8240)
                            {
                                if (index < 8231)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8241)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8245)
                            {
                                if (index < 8244)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
            }
        }
        else
        {
            if (index < 8721)
            {
                if (index < 8481)
                {
                    if (index < 8452)
                    {
                        if (index < 8255)
                        {
                            if (index < 8252)
                            {
                                if (index < 8251)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8254)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8365)
                            {
                                if (index < 8364)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8451)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8458)
                        {
                            if (index < 8454)
                            {
                                if (index < 8453)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8457)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8471)
                            {
                                if (index < 8470)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 8596)
                    {
                        if (index < 8560)
                        {
                            if (index < 8544)
                            {
                                if (index < 8482)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8556)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8592)
                            {
                                if (index < 8570)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8713)
                        {
                            if (index < 8602)
                            {
                                if (index < 8598)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8712)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8720)
                            {
                                if (index < 8719)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
            }
            else
            {
                if (index < 8743)
                {
                    if (index < 8731)
                    {
                        if (index < 8728)
                        {
                            if (index < 8725)
                            {
                                if (index < 8722)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8726)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8730)
                            {
                                if (index < 8729)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8740)
                        {
                            if (index < 8737)
                            {
                                if (index < 8733)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8739)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8742)
                            {
                                if (index < 8741)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 8766)
                    {
                        if (index < 8756)
                        {
                            if (index < 8750)
                            {
                                if (index < 8748)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8751)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8764)
                            {
                                if (index < 8760)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8781)
                        {
                            if (index < 8777)
                            {
                                if (index < 8776)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 8780)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 8787)
                            {
                                if (index < 8786)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
            }
        }
    }
    else
    {
        if (index < 12549)
        {
            if (index < 9676)
            {
                if (index < 8979)
                {
                    if (index < 8857)
                    {
                        if (index < 8814)
                        {
                            if (index < 8804)
                            {
                                if (index < 8802)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8808)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8853)
                            {
                                if (index < 8816)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8854)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 8895)
                        {
                            if (index < 8869)
                            {
                                if (index < 8858)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 8870)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 8978)
                            {
                                if (index < 8896)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 9650)
                    {
                        if (index < 9372)
                        {
                            if (index < 9322)
                            {
                                if (index < 9312)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 9332)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 9634)
                            {
                                if (index < 9632)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 9670)
                        {
                            if (index < 9660)
                            {
                                if (index < 9652)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 9662)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 9675)
                            {
                                if (index < 9672)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
            }
            else
            {
                if (index < 12293)
                {
                    if (index < 9738)
                    {
                        if (index < 9702)
                        {
                            if (index < 9680)
                            {
                                if (index < 9678)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 9698)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 9735)
                            {
                                if (index < 9733)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 9737)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 9795)
                        {
                            if (index < 9793)
                            {
                                if (index < 9792)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 9794)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 12292)
                            {
                                if (index < 12288)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 12436)
                    {
                        if (index < 12321)
                        {
                            if (index < 12317)
                            {
                                if (index < 12312)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 12319)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 12353)
                            {
                                if (index < 12330)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                    else
                    {
                        if (index < 12535)
                        {
                            if (index < 12447)
                            {
                                if (index < 12443)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 12449)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 12543)
                            {
                                if (index < 12540)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
            }
        }
        else
        {
            if (index < 55297)
            {
                if (index < 13215)
                {
                    if (index < 12959)
                    {
                        if (index < 12832)
                        {
                            if (index < 12690)
                            {
                                if (index < 12586)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 12704)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 12928)
                            {
                                if (index < 12868)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 12958)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 13198)
                        {
                            if (index < 12969)
                            {
                                if (index < 12964)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 12977)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 13212)
                            {
                                if (index < 13200)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 13265)
                    {
                        if (index < 13253)
                        {
                            if (index < 13218)
                            {
                                if (index < 13217)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 13252)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 13263)
                            {
                                if (index < 13262)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 19968)
                        {
                            if (index < 13269)
                            {
                                if (index < 13267)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 13270)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 55296)
                            {
                                if (index < 40870)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 0;
                            }
                        }
                    }
                }
            }
            else
            {
                if (index < 65093)
                {
                    if (index < 63733)
                    {
                        if (index < 59335)
                        {
                            if (index < 56321)
                            {
                                if (index < 56320)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 57344)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 59493)
                            {
                                if (index < 59337)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 65072)
                        {
                            if (index < 63744)
                            {
                                if (index < 63734)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 64046)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 65075)
                            {
                                if (index < 65074)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 2;
                            }
                        }
                    }
                }
                else
                {
                    if (index < 65128)
                    {
                        if (index < 65112)
                        {
                            if (index < 65107)
                            {
                                if (index < 65097)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                if (index < 65108)
                                    return 1;
                                else
                                    return 2;
                            }
                        }
                        else
                        {
                            if (index < 65127)
                            {
                                if (index < 65113)
                                    return 1;
                                else
                                    return 2;
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        if (index < 65504)
                        {
                            if (index < 65281)
                            {
                                if (index < 65132)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                if (index < 65375)
                                    return 2;
                                else
                                    return 1;
                            }
                        }
                        else
                        {
                            if (index < 65536)
                            {
                                if (index < 65510)
                                    return 2;
                                else
                                    return 1;
                            }
                            else
                            {
                                return 0;
                            }
                        }
                    }
                }
            }
        }
    }
}

或者, 我们将这个列表, 写入到byte[], 每一个字符占1bit, 如果对应bit是1, 则表示字符宽度为2, 如果对应位是0, 则表示字符宽度为1. 这样, 速度是更快的, 只不过要牺牲8kb的空间了.

因为控制台中, 字符宽度只有4中可能, 0, 1, 2, 8, 其中0是\r\n\0这类无宽度的, 显然它们也无法参与字符串宽度判断, 1就是普通半角字符, 2就是中文以及全角字符, 8显然是\t, 故, 我们只需要能够表示1和2即可, 也就是1bit的空间.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
int[] lens = new int[char.MaxValue + 1]
void WriteData()    // 将已经生成好的宽度数据写入到文件
{
    FileStream fs = File.OpenWrite(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Lengths.bin"));
    byte[] write = new byte[1];
    for (int i = 0; i < lens.Length;)
    {
        for (int j = 0; j < 8 && i < lens.Length; i++, j++)
        {
            if (lens[i] == 2)
                write[0] |= (byte)(1 << j);
        }
        fs.Write(write, 0, 1);
    }
    fs.Flush();
    fs.Close();
}

char.MaxValue / 8 = 8k, 8kb即可装下所有字符的宽度信息.

然后结合位运算, 就能得出一个简单的函数. 例如 Lengths 是我们的byte数组.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
static byte[] Lengths = Resources.Lengths;
public static bool IsWideDisplayCharEx(char c)
{
    int index = c;
    return (Lengths[index / 8] & (1 << (index % 8))) != 0;
}
public static int GetCharDisplayLengthEx(char c)
{
    return IsWideDisplayCharEx(c) ? 2 : 1;
}
public static int GetStringDisplayLengthEx(string str)
{
    int total = 0;
    foreach (char c in str)
        total += GetCharDisplayLengthEx(c);
    return total;
}

关于使用:

直接去clone开头所写明的代码仓库即可, 会不定时更新的.或者, 直接复制上面的代码(最长的那一段).

上面的代码大部分是自动生成的哦~偷懒 关于图中使用到的Null.TextEditor, 在这里:

  1. 博客链接: 使用IronPython来创建支持脚本编辑文本内容的文本编辑器
  2. Github 仓库地址: SlimeNull/Null.TextEditor










点个赞, 留个评论再走吧QAQ

Built with Hugo
主题 StackJimmy 设计