如何使用StringBuilder从ArrayList中删除值? [英] How to make such a form to remove values from ArrayList with StringBuilder?

查看:103
本文介绍了如何使用StringBuilder从ArrayList中删除值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用代码从arraylist中删除值,但是如何使用StringBuilder调用函数来删除值。



我有两个arraylist,一个用于句子和其他arraylist的arraylists标签。每个句子都有很多标签。我想根据索引显示每个句子及其建议的标签。但是用户可以删除他不想要的标签。并且用户在完成时将它们提交到数据库,我想将每个单独提交作为下面的结果或者一个按钮一起提交。





//希望结果

句子1

标记一个[删除],标记两个[删除],标记三个[删除] ...



提交按钮



句子2

标签一[删除],标签二[删除],标签三[删除] ...



提交按钮

I know how to remove value from arraylist using code, but how can I call function to delete the value using StringBuilder.

I have two arraylist, one for sentences and other arraylist of arraylists for tags. each sentence has many tags. I want to display each sentence and its suggested tags accrording to the index. But user can delete the tag he don't want. And the user will submit them to the database when he finish, I want to submit each one individually as the wished result below or altogether by one button.


//wished result
sentence 1
tag one [delete], tag two[delete], tag three[delete] ...

submit button

sentence 2
tag one[delete], tag two[delete], tag three[delete] ...

submit button

推荐答案

问题是StringBuilder与ArrayList不同。在ArrayList中,字符串值作为单独的字符串维护,可通过索引访问。你可以遍历它们。



StringBuilder是一个很大的字符串。你正在为字符串添加部分,但是一旦添加它们,它们就不再被认为是谨慎的字符串,它们是较大字符串的一部分。



所以如果你建立一个像这样的字符串:



StringBuilder sb = new StringBuilder();

sb.Append(How);

sb.Append(now,);

sb.Append(brown);

sb.Append(cow?);



...你不再有四个字符串,你有一个字符串:



现在怎么样,褐色牛?



即使您添加了AppendLine,也就是说文本中插入了换行符。



从理论上讲,你可以保持字典中添加的行的位置:



StringBuilder sb = new StringBuilder();

Dictionary< string,int> dictSB = new Dictionary< string,int>();



dictSB.Add(brown,sb.Length);

sb .Append(棕色)





如果你想删除棕色,你可以这样做。



int brownLocation = dictSb [brown];

sb.Remove(brownLocation,brown.Length);



然后你必须遍历字典条目,并将所有那些值大于(前)褐色位置的值减去褐色长度。



但是你为什么要这么做?如果你需要保持离散的字符串,请将它们保存在某种类型的集合中。你想要完成什么?
The problem is that StringBuilder is not the same as ArrayList. In ArrayList, the string values are maintained as separate strings, accessible by index. You can loop through them.

StringBuilder is a big string. You are adding parts to the string but once they are added, they are no longer considered discreet strings, they are part of the larger string.

So if you build a string like this:

StringBuilder sb = new StringBuilder();
sb.Append("How");
sb.Append(" now,");
sb.Append(" brown");
sb.Append(" cow?");

...you no longer have four strings, you have one string:

"How now, brown cow?"

Even if you added with AppendLine that just means there is a line break inserted into the text.

Theoretically, you could maintain the locations of the lines added in a dictionary:

StringBuilder sb = new StringBuilder();
Dictionary<string,int> dictSB = new Dictionary<string,int>();

dictSB.Add("brown", sb.Length);
sb.Append("brown")


If you wanted to remove "brown" you could do it like this.

int brownLocation = dictSb["brown];
sb.Remove(brownLocation, "brown".Length);

Then you'd have to iterate through the dictionary entries and decrement all those that have larger values than the (former) location of "brown" by the length of brown.

But why would you want to do all that? If you need the strings to stay discrete, keep them in a collection of some sort. What are you trying to accomplish?


这篇关于如何使用StringBuilder从ArrayList中删除值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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