Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am working on a automation project which requires a loop program that captures the value from one line and replaces it in another line.
 
I have a content(can be upto 2000 pages) within a textbox, which has some SGML tagging like below:
 
lsec prefix='Article' d='3' uid='VAT1011_A03'
 
lsbsec d='1.' loc='VAT1011_00_01' Goods produced 
lsbsec d='2.' loc='VAT1011_00_02' Goods used 
lsbsec d='3.' loc='VAT1011_00_03' Business entity 
lsbsec d='4.' loc='VAT1011_00_04' Consigns goods
 
lsec prefix='Article' d='4' uid='VAT1011_A04'
 
lsbsec d='1.' loc='VAT1011_00_01' Goods produced 
lsbsec d='2.' loc='VAT1011_00_02' Goods used 
lsbsec d='3.' loc='VAT1011_00_03' Business entity 
lsbsec d='4.' loc='VAT1011_00_04' Consigns goods
 
Through a button click highlighted part from lsec line needs to be captured and pasted in the highlighted part (00) in the following lsbsec lines till the next lsec starts.
 
The unique value that needs to be captured is the value between _and ('). Eg : _A03'.
 
Unique value that needs to be replaced is _00_ in the lsbsec lines.
Posted 17-Sep-12 0:10am
Edited 17-Sep-12 0:32am
v4
Comments
OriginalGriff at 17-Sep-12 6:09am
   
And what have you tried so far?
Where are you stuck?
vamshivarma at 17-Sep-12 6:12am
   
i have completed capturing the value from lsec line, and when i tried to replace _00_ in lsbsec, program replacing all the values.
 
Actually it has to replace only till next lsec starts. i wrote a condition And it is not a loop program.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"i have completed capturing the value from lsec line, and when i tried to replace _00_ in lsbsec, program replacing all the values.
 
Actually it has to replace only till next lsec starts. i wrote a condition And it is not a loop program."

 
So presumably you are using String.Replace?
 
That won't work too well - the replace method has no upper or lower bounds you can set to limit the replace operation to a selected portion of the text.
 
There are a couple of ways you can do this.
If you are trying to replace a single section only, then I would locate the start lsec line, located the end lsec line, then cut the input string into three parts before changing the single section and rebuilding it.
 
If you have multiple sections to do, then I would suggest one of two routes:
1) Use String.Split to break the text into sections starting with a lsec section, and use String.Replace to update each. Then use String.Join to rebuild the output.
2) Use a Regex - there is a version of the Regex.Replace method which uses a MatchEvaluator method to update each instance of the replace string which you could use to match your lsec and lsbsec lines. It would be a faff to get right I suspect, but it would be an easy to maintain version. I use this for string parameter replacement: Advanced Regex.Replace handling[^] - the link won't solve your problem, but it shows what I do with the match evaluator, and how easy it is to set up.
  Permalink  
Comments
vamshivarma at 17-Sep-12 8:50am
   
I Tried this
 
private void button1_Click(object sender, EventArgs e)
 
{ int i;
 
string value = string.Empty;
 
string finalValue = string.Empty;
 
for (i = 0; i < textBox1.Lines.Count(); i++)
 
{
 
string lineText = textBox1.Lines[i];
 
if(lineText.Contains("lsec"))
 
{
 
value = string.Empty;
 
int index = lineText.IndexOf(textBox2.Text, System.StringComparison.Ordinal);
 
int correctindex = index + textBox2.Text.Length + 1;
 

 
string text = lineText.Substring(correctindex, lineText.Length - correctindex);
 
for (int j = 0; j < text.Length; j++)
 
{
 
if (Char.IsLetterOrDigit(text[j]))
 
{
 
value += text[j];
 
}
 
}
 
} else
 
{
 
if (lineText.Contains("00"))
 
{
 
lineText = lineText.Replace("00", value);
 
}
 
}
 
finalValue += lineText + Environment.NewLine;
 
} textBox1.Text = finalValue;
 
}
 

But the Output is
 

lsec prefix='Article' d='3' uid=‘VAT1011_A03
lsbsec d='1.' loc='VAT1011_secprefixArticled3uidVAT1011A03_01' Goods produced
lsbsec d='2.' loc='VAT1011_secprefixArticled3uidVAT1011A03_02' Goods used
lsbsec d='3.' loc='VAT1011_secprefixArticled3uidVAT1011A03_03' Business entity
lsbsec d='4.' loc='VAT1011_secprefixArticled3uidVAT1011A03_04' Consigns goods
lsec prefix='Article' d='4' uid=‘VAT1011_A04
lsbsec d='1.' loc='VAT1011_secprefixArticled4uidVAT1011A04_01' Goods produced
lsbsec d='2.' loc='VAT1011_secprefixArticled4uidVAT1011A04_02' Goods used
lsbsec d='3.' loc='VAT1011_secprefixArticled4uidVAT1011A04_03' Business entity
lsbsec d='4.' loc='VAT1011_secprefixArticled4uidVAT1011A04_04' Consigns goods
 

it is replaceing _00_ with the entire line.
 

Could you please help me in solving this.
OriginalGriff at 17-Sep-12 9:53am
   
What have you put in TextBox2?
vamshivarma at 17-Sep-12 10:53am
   
Textbox1 : VAT1011
Textbox2 : Content
Now it is working fine, i have uploaded the solution.
OriginalGriff at 17-Sep-12 11:06am
   
:thumbsup:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

private void button1_Click(object sender, EventArgs e)
                 {
 
                          int i;
                          string value = string.Empty;
                          string finalValue = string.Empty;
 
                          for (i = 0; i &lt; textBox1.Lines.Count(); i++)
                          {
                                   string lineText = textBox1.Lines[i];
 
                                   if(lineText.Contains("lsec"))>
                                   {
                                            value = string.Empty;
                                            int index = lineText.IndexOf(textBox2.Text, System.StringComparison.Ordinal);
                                            int correctindex = index + textBox2.Text.Length + 1;
 

                                            string text = lineText.Substring(correctindex, lineText.Length - correctindex);
 
                                            for (int j = 0; j &lt; text.Length; j++)
                                           {
                                                    if (Char.IsLetterOrDigit(text[j]))
                                                    {
                                                             value += text[j];
                                                    }
                                            }
 
                                   }
                                   else
                                   {
                                            if (lineText.Contains("00"))
                                            {
                                                    lineText = lineText.Replace("00", value);
                                            }
                                   }
 
                                   finalValue += lineText + Environment.NewLine;
                          }
 
                          textBox1.Text = finalValue;
                 }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100