Click here to Skip to main content
15,939,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 20-May-14 23:03pm
[no name] 20-May-14 16:35pm    
If you read what you wrote here, wouldn't you be confused? Delete what value? "add remove link or button beside each word", means what exactly? Why are you asking about deleting and adding when you are doing neither? What does your code snippet have to do with anything?

1 solution

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(" now,");
sb.Append(" brown");
sb.Append(" cow?"); 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);

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?
Share this answer
Member 10672813 21-May-14 4:29am    
I am trying to create tag display, where user could dynamically remove suggested tags stored in ArrayList, but I still don't have solution, I use stringbuilder to display result as search result for sentences I want to tag.
Member 10672813 21-May-14 4:54am    
I will put my code definition and the result I want in the question
Francine DeGrood Taylor 21-May-14 12:50pm    
I'm sorry, but your added description does not make it clearer what you want to do. So you have an arraylist of sentences, and an arraylist of...arraylists of tags? Or is the second an arraylist of tags? What does the second arraylist represent? I assume it has something to do with the sentences? And what controls are you using to display the sentences and secondary arraylist to the usewr?
Francine DeGrood Taylor 21-May-14 12:58pm    
Give me a concrete example of what the user might be seeing on the form, what the information that he is seeing represents, and what he would want to do with that information.

For example, you might say "The user has a list of sentences. When he selects a sentence, a listbox beside the sentences is populated with all the tags for that sentence. The user may want to delete one of the tags by selecting the tag and clicking the Delete button. After all deletions have been done, user clicks button to send information to the database. How do I convey to the database what tags have been deleted?"

This is the level of detail you should give so that we can help you.
Member 10672813 21-May-14 16:12pm    
I will tell you the idea I want, I will appreciate your help or any help, with ArrayList or any other way, I am upload many files and gives them tags, I have two pages the first upload files to the server, and decide their tags, this all was done good, but now I am trying to create second page that will receive the file and his tags. In this page I want to display the file title and his tags, where user can remove the suggested tags for each file, before submit the final form to the database. Hope this was clear, if not told me to discuss more.

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