substring()来剪切字符串? [英] substring () to cut string?

查看:93
本文介绍了substring()来剪切字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个问题如下:在用户界面中,我必须显示图像,名称,价格和摘要每个产品。



总结我使用substring()来剪切字符串,因为我只想显示1-2行的摘要。同时在管理员中,摘要可以更长。



但是如果我使用substring()来剪切字符串,则会发生错误,它会破坏框架,因为虽然管理员总结我使用FCKEditor。作为总结,我希望他们能够换行或更改font-family,font-size,...不应该使用textarea。



总结他们经常从一个网站复制到另一个网站,因此它经常会出现更多标签。因此,如果该标签剪切了字符串,它将丢失,导致用户界面框架中的破裂。



所以你有办法帮我解决这个问题吗?



谢谢。

Hi,

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?

Thanks.

推荐答案

首先,你应该明白你从来没有剪掉一个字符串。字符串是不可变的。初始化后,根本无法修改它们。当您在某个变量上使用 System.Substring 时,实际上创建了一个具有适当常量的全新字符串,并将引用分配给新创建的字符串。如果将其分配给同一个变量,则其参照标识始终会丢失,因为引用将替换为第一个参考引用仍在使用时创建的第二个引用引用。这是清楚的吗?



现在,我不知道为什么你有破坏框架的问题,因为你没有告诉我们你是什么样的UI正在使用。所以,请自己处理或提出另一个问题。它完全与切割字符串无关。无论UI是什么,摘要往往占据不同的空间,具体取决于字符串长度,行分隔符和其他因素。无论如何你需要解决这个问题。



关于摘要,我强烈建议你稍微改变一下数据模型。原因如下:我看起来你的想法是使用简短的详细描述作为总结。我认为这是一个坏主意。这样的缩写字符串很少得到充分的总结。因此,您应将描述和摘要保持为两个不同的实体。而且,尽管需要输入单独的文本进行摘要,您的用户会更高兴。看起来您想要自动进行汇总。它会限制用户的自由;大多数用户都不会喜欢这样。



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

—SA


这篇关于substring()来剪切字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆