2

I have a substring what contains HTML tag and I need to shorten it but display it with the same formatting as it appears on the string.

It doesn't have to be exactly X characters long, but it should be short enough to be displayed inside a panel with a certain width and height?

Is there any way I can achieve this using c#?

What about using CSS? I.e. displaying the panel with a fixed height regardless of its content?

Thanks..

Example: I have the following panel containing a label that contains text with html tags:

Example

I need to remove the scroll bar without making the panel longer but keeping this height & this width..

7
  • You mean turn "<span>Some Really Long String</span" into "<span> Some Rea...</span>? Commented Oct 4, 2012 at 12:54
  • Yes, this is one option, another is by displaying Some Really Bold String into Some Rea because that's the dimention of the Panel.(I want to avoid scrollbars and still keep the panel's dimentions by displaying only part of its content) Commented Oct 4, 2012 at 12:59
  • I edited the post adding the example. Commented Oct 4, 2012 at 13:05
  • 8
    use css: overflow:hidden. Commented Oct 4, 2012 at 13:06
  • Are you trying to strip HTML ? Commented Oct 4, 2012 at 13:07

2 Answers 2

2

If you have following html code:

<div class="div1"> Some Really Bold String </div>

You can provide css to hide the scroll bars,

.div { overflow:hidden; height:200px; width:200px;}

height and width values are just for example purpose.
overflow:hidden does not let the content of the div to expand out side the div. you will find more information on overflow here.

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

1 Comment

Great! Glad I was able to help.
2

You could use a regex to find the contents of the specific tag. Use a .substring to shorten the result afterwards.

A example could be:

<h1>head</h1>
<p>contents</p>

Regex could be:

<p\b[^>]*>(.*?)</p>

Result would be:

<p>contents</p>

Now just exclude the start and end tag. as its a fixed length.

I found more interesting reading about changing the content between HTML tags. Take a read here (regex ftw!):

http://www.thatsquality.com/articles/how-to-match-and-replace-content-between-two-html-tags-using-regular-expressions


Another solution that might not drive you as crazy if you want to solve it in c#:

HTML Agility Pack

Take a look at the examples part of the site. Great little tool!

3 Comments

HTML with Regex again?? Have a look at the masterpiece here: stackoverflow.com/a/1732454/1090657.
Really really cool post - love what he did. That being said, i have had a boss that made a living parsing html with regex. Regex finds patterns. Any string can have those patterns. While i agree the CSS solution might be the more sensible, regex is far from impossible. Everyone seemed to pursue the CSS solution, so i tried to think of a solution that could be plausible in c#.
Thanks @Wraithbone really interesting

Your Answer

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