0

Hi all i am having my data as follows

52201               1                   1         PPD1         111017111017   1111000020000003

Here i would like to replace the one with bold to 0000002 can any one help me. I used the following but i am unable to replace it

if (strBtchno1.StartsWith("5"))
{
    iBtchno = Convert.ToInt16(strBtchno1.Substring(87, 7));
    if (iBtchno > iBatchno)
    {
       iBtchno = iBtchno - 1;                      
       strBtchno1 = strBtchno1.Substring(0,87) + iBtchno.ToString() + strBtchno1.Substring(7,(strBtchno1.Length - 7));
     }                       
  }
7
  • You're starting your substring 87 spaces into the string. Shouldn't it be 37, or is the string actually bigger, than what you are showing? Commented Oct 17, 2011 at 12:38
  • I would like to replace the text from the character 87 to 94 my maximum length is 94 Commented Oct 17, 2011 at 12:38
  • If you mean replace the last 7 digits, xanatos answer is correct otherwise please explain better. Commented Oct 17, 2011 at 12:42
  • If you do the Convert.ToInt16 wont you loose all the leading zeros too? Commented Oct 17, 2011 at 12:43
  • @Nicolai formatting issue, I've edited with proper format now. Commented Oct 17, 2011 at 12:44

2 Answers 2

3
string strBtchno1 = "52201               1                   1         PPD1         111017111017   1111000020000003";
int iBtchno = Convert.ToInt32(strBtchno1.Substring(strBtchno1.Length - 7));
iBtchno++;
strBtchno1 = strBtchno1.Substring(0, strBtchno1.Length - 7) + iBtchno.ToString("d7");

7 digits is an int, not a short!!!

And to format the number back with the padding you can use iBtchno.ToString("d7").

Sign up to request clarification or add additional context in comments.

Comments

0
 string newS = System.Text.RegularExpressions.Regex.Replace(s, @"\*\*[0-9]+\*\*", "0000002");

but i dont know if you want regex solution

3 Comments

What you mean by this will it replace the text as per i needed?
@if your data contains '*' so yes. you didnt mentioned it
The * were there to try and make the text bold - can't be used inside code block though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.