user1608982
user1608982

Reputation:

How to get the substring of a string that contains HTML tags using C# .net?

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..

Upvotes: 2

Views: 2259

Answers (2)

Jakob Hviid PhD
Jakob Hviid PhD

Reputation: 115

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!

Upvotes: 2

Prasad Jadhav
Prasad Jadhav

Reputation: 5208

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.

Upvotes: 2

Related Questions