Click here to Skip to main content
15,609,332 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I'm having one problem as follows: In the user interface, I have to display the image, name, price and a summary of each product.

In summary I use substring () to cut string, because the summary I just want to display 1-2 lines. Meanwhile in the admin, the summary can be longer.

But if i use substring() to cut string, the error happens, it can shatter the frame, because while administrators in summary I use FCKEditor. As a summary I want them to be able to line break or change the font-family, font-size, ... should not be used textarea.

In summary they often copied from one site to another, so it's often born more tags. So if that tags cut the string, it will be missing, causing a rupture in the user interface framework.

So you have a way to help me handle the problem?

MalwareTrojan 9-Mar-13 2:37am    
Show me what have you tried?

1 solution

First of all, you should understand that you never "cut" a string. Strings are immutable. Once initialized, they cannot be modified at all. When you use System.Substring on some variable, you actually create a brand-new string with appropriate constant and assign the reference to the newly created string. If you assign it to the same variable, its referential identity is always lost, as the reference is replaced with the second reference reference created when the first one was still in use. Is that clear?

Now, I have no idea why do you have a problem with "shattering the frame", because you did not tell us what kind of UI you are using. So please, deal with it by yourself or ask another question. It has nothing to do with "cutting" the string at all. Whatever the UI is, the summary tends to occupy different space, depending on string length, those line separators and other factors. You will need to solve this problem anyway.

As to the summary, I will strongly advice you to change the data model a bit. Here is why: I looks like you idea is to use abbreviated detailed description as a summary. I think this is a bad idea. Such abbreviated strings rarely make adequate summary. Therefore, you should keep description and summary as two different entities. And your user, despite the need of entering separate text for summary, would be more happy. It looks like you wanted to make summary "automatically". It would limit the user's freedom; most users will hardly like that.

Share this answer
thanhbinh00 9-Mar-13 3:17am    
The first, I'm using Looks like you misunderstood my intention.

I mean, if in the summary of product 1 is too long (using FCK), in the user interface to show them all so very bad.

This is why I use substring () to limit the summary is displayed (1 or 2 lines).

Agree with you, can not cut the string, it is "separates" instead of "cut". I often say na "cut string" should you misunderstood.

I tried to use Regex.Replace, it clears away all the html tags so true is not broken frame.

But if I want a summary of one bold, or italic, will handle it?

Furthermore when I use Regex. Replace before substring is often error-prone "Index and length must refer to a location within the string."

I've been trying to fix this, but not because I want to cut the string so that when you cut the length shall be limited to the final text of which is still original.

Eg: I have one string: "Hello everyone. Nice to meet you". Now I use substring to cut the string, display length limit is 25. As usual, it will cut the letters "m", but I do not want to show that, instead, that it will display the first full text closest ("to").

The help you handle the problems that.

Sergey Alexandrovich Kryukov 9-Mar-13 6:17am    
No, I did not misunderstand you. You did not mention ASP.NET yourself, your fault (always tag what UI library or application type), but of course I took into account that it could be ASP.NET. How come you ended up with HTML tag in data in first place?
Anyway, your approach with length limit leads nowhere, because fonts are not fixed-width...
thanhbinh00 11-Mar-13 22:30pm    
I use Regex.Replace to remove all html tags then using substring.

Ok, let's not limit the length of the non-fixed width fonts. So if I fix the fonts 12px how?
Sergey Alexandrovich Kryukov 11-Mar-13 22:46pm    
Well, then you apparently can calculate exact text width. But it will look ugly anyway. Better pay attention for my advice. Don't rely on the possibility of knowing exact text metrics. You can allow for text scrolling. Why not? Don't try to make things more difficult than they have to be.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900