c#中关于Substring()的问题

本文关键字:问题 Substring | 更新日期: 2023-09-27 18:03:30

我不是c#程序员& &;需要帮助。我有一些问题:

当我有字符串text='My car is nice'时,那么下面几行的输出是什么:

(1) text.Substring(1,1);
(2) text.Substring(6,1);
(3) text.Substring(1,4).Replace('c','a');
(4) text.Substring(1,10).Replace('a','b').Replace(' ','t');
我的结论是:
(1) 'y'
(2) ' is nice M'     <== here, I started from 6 until 1 (or do I need to swap 1&6?)
(3) 'y c'
(4) 'ytcbrtist'      <== here I replaced a with b & the space lines with t 

我希望有人能帮忙。

问好,

c#中关于Substring()的问题

如果你看一下String的文档。子字符串方法(Int32, Int32)它表示:

public string子字符串int startIndex,int长度)

:

(1) text.Substring(1,1);
(2) text.Substring(6,1);
(3) text.Substring(1,4).Replace('c','a');
(4) text.Substring(1,10).Replace('a','b').Replace(' ','t');
(1) 'y' // Indice 1 length 1
(2) ' ' // Indice 6 length 1  
(3) 'y aa' // Indice 1 length 4 and replacements 
(4) 'ytcbrtistn'// Indice 1 length 10 and replacements

看直播

1)'y'    OK
2)' ' The sixth character is 'r'. And the next one is space ' '.
3)'y aa' . You are taking 4 chars starting from first. It's 'y ca' . Later You replace c with a. 
4)'ytcbrtistn' . You take 10 chars starting from 2nd one. 'y car is n' . You replace a with b -> 'y cbr is n' . Later replace space with t.