Removed 2 lines of a string.

  • Thread starter Thread starter Boki
  • Start date Start date
B

Boki

Hi All,

In order to remove 2 lines of a string, I write this:

/* BEGIN */
first_line_location = temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
first_line_location =
temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
textBox1.Text =temp_string;

/* END */

Even it can be put in a single function call, but I believe there is a
simpler way to do this, but I fail to implement it, could you advice ?

Thanks!

Best regards,
Boki.
 
Hi All,

In order to remove 2 lines of a string, I write this:

/* BEGIN */
first_line_location = temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
first_line_location =
temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
textBox1.Text =temp_string;

/* END */

Even it can be put in a single function call, but I believe there is a
simpler way to do this, but I fail to implement it, could you advice ?

Thanks!

Best regards,
Boki.

Hi Boki,
Maybe there is a more concise way to do it, but I think the SubString
approach is fine. I would probably have taken an approach more like
this:

int newlineIndex1 = temp_string.IndexOf(Environment.NewLine);
int newlineIndex2 = temp_string.IndexOf(Environment.NewLine,
newlineIndex1 + Environment.NewLine.Length);
temp_string = temp_string.Substring(newlineIndex2 +
Environment.NewLine.Length);
textBox1.Text =temp_string;

It avoids using a hard-coded "2" as the newline length, and it just
calls Substring once (and therefore creates a new string just once).

Hope that helps,
John
 
Hi,

Boki said:
Hi All,

In order to remove 2 lines of a string, I write this:

/* BEGIN */
first_line_location = temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
first_line_location =
temp_string.IndexOf(Environment.NewLine);
temp_string = temp_string.Substring(first_line_location +
2, temp_string.Length - first_line_location - 2);
textBox1.Text =temp_string;

/* END */

Even it can be put in a single function call, but I believe there is a
simpler way to do this, but I fail to implement it, could you advice ?

I do not know of a simpler solution, well maybe if you use a Split to divide
the string in lines, then you concatenate the resulting strings without
includind the ones you want out.
 
Hi,










I do not know of a simpler solution, well maybe if you use a Split to divide
the string in lines, then you concatenate the resulting strings without
includind the ones you want out.

Hi,

Is Split a control box or ? Sorry, I don't know this.

Boki.
 
Hi Boki,
Maybe there is a more concise way to do it, but I think the SubString
approach is fine. I would probably have taken an approach more like
this:

int newlineIndex1 = temp_string.IndexOf(Environment.NewLine);
int newlineIndex2 = temp_string.IndexOf(Environment.NewLine,
newlineIndex1 + Environment.NewLine.Length);
temp_string = temp_string.Substring(newlineIndex2 +
Environment.NewLine.Length);
textBox1.Text =temp_string;

It avoids using a hard-coded "2" as the newline length, and it just
calls Substring once (and therefore creates a new string just once).

Hope that helps,
John

Looks good. Thanks! :)

Boki.
 
Back
Top