I want to keep a string to another string strTotal from a richtextbox. then i want to split it into two SubString like firstPart and secondPart by a middle index say: midIndex.
Then I want to change some value in firsthalf and join with secondhalf. And finally show it to the richTextBox.
For this i used this code:
For my Last debug:
rtxtQueryPan.Text.Length = 53;
midIndex = 45;
//string Totalqueryy = rtxtQueryPan.Text;
string firstHalf = rtxtQueryPan.Text.Substring(0, midIndex-2);
string secondHalf = rtxtQueryPan.Text.Substring((midIndex- 1), (rtxtQueryPan.Text.Length - 1)); // THIS LINE SHOWS ERROR
string duplicateFirstHalf = firstHalf;
firstHalf += " " + clColumnNames.Text + ",";
rtxtQueryPan.Text = firstHalf+secondHalf;
In the 3rd Line of the code says:
Index and length must refer to a location within the string.
Parameter name: length
I have checked the length, midIndexvalue etc. But found no clue.
C#’s
.Substringfunction takes a length, not a position as its second argument.Thus, you probably wanted to write the following code:
Also, the length argument is optional, and not something you should provide in your case, as you want the rest of the string included. You can also therefore write: