Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
ex

Listbox

my name
her name
your name.

I want to remove only "."

my name
her name
your name

[Edit] Moved from non-solution
C#
string[] dataFromFile = lstQiftet.Items.Cast<string>().ToArray();

           ArrayList arr = new ArrayList();

           foreach (string s in dataFromFile)



I tried s.remove(".";).... s.TrimEnd(".")...... dont work
[/Edit]
Posted
Updated 23-Apr-12 11:43am
v2
Comments
Sergey Alexandrovich Kryukov 23-Apr-12 16:52pm    
Not a question. What have you done so far and what's the problem. Any code sample?
--SA
h7h7h7 23-Apr-12 17:07pm    
string[] dataFromFile = lstQiftet.Items.Cast<string>().ToArray();

ArrayList arr = new ArrayList();

foreach (string s in dataFromFile)

I tryied s.remove(".").... s.TrimEnd(".")...... dont work
Alan N 23-Apr-12 17:34pm    
Try posting a complete sample with the code that you say 'dont work' and then someone will be able to tell you what you are doing wrong. It is almost certain that you have just made a simple mistake bacause the Remove and TrimEnd methods do work.
Philippe Mori 23-Apr-12 18:29pm    
TrimEnd function does not modify the string but returns a modified string (as any other string member function by the way).

Also, you won't be able to modify the strings using a foreach loop as the returned items as s is a local variable and change made to it won't be reflected back in the original container.

The way to do it correctly assuming that you want to modify the original array would be:

C#
for (int i = 0, count = dataFromFile.Length; i != count; ++i)
{
    dataFromFile[i] = dataFromFile[i].TrimEnd('.');
} 
 
Share this answer
 
Comments
VJ Reddy 23-Apr-12 20:44pm    
Good TrimEnd method, . 5!
sravani.v 24-Apr-12 0:01am    
My 5!
The TrimEnd method and other methods to remove the . work. But the issue may be reassigning the modified list of items to the ListBox.

The Items property of ListBox is a ReadOnly property, hence it can be used to retrieve the existing list of items, but it cannot be assigned back. Hence, the DataSource property can be used to set the modified list.

The TrimEnd('.') works but if there are spaces at the end then it will not work. So, it is better to use TrimEnd(new char[]{'.',' '}) to ensure that the . is removed even if there are trailing spaces. Since, it is required to remove only the . at the end of the string, I think it is a good option to use TrimEnd method.

The following code can be used to test the above points.
C#
void Main()
{
    Form form1 = new Form();
    ListBox listBox1 = new ListBox();
    listBox1.Items.AddRange(new string[]{"my name","her name","your name.  "});
    form1.Controls.Add(listBox1);
    form1.ShowDialog();

    //Convert to ToList to set DataSource property of ListBox
    //Trim() is required to remove the extra space for the TrimEnd to work on .
    var items = listBox1.Items.Cast<string>().Select (s => s.TrimEnd(new char[]{'.',' '})).ToList();
   
    //This will not compile as Items  property is read only.
    //listBox1.Items = items;

    listBox1.DataSource=items;
    form1.ShowDialog();
}
 
Share this answer
 
v3
Comments
Abhinav S 23-Apr-12 23:18pm    
Good answer. 5.
VJ Reddy 23-Apr-12 23:23pm    
Thank you, Abhinav.
sravani.v 24-Apr-12 0:01am    
My 5!
VJ Reddy 24-Apr-12 0:07am    
Thank you, sravani.v
C#
s.Replace(".", "");
 
Share this answer
 
C#
String.Replace('.','');


Just replace the . with an empty string.
 
Share this answer
 
v2
Comments
h7h7h7 23-Apr-12 17:13pm    
I have tried that :S :S
ZurdoDev 23-Apr-12 18:24pm    
What happens?
[no name] 23-Apr-12 17:23pm    
s.Replace(".", ""); s.Replace('.', ''); won't compile in VS2008. (won't let me format this either.)
ZurdoDev 23-Apr-12 18:24pm    
Won't compile? What is the error?
[no name] 23-Apr-12 18:32pm    
Empty character literal.
At some point, you're adding items to the listbox:

ListBox lb;
String text;
String value;
.
.
.
lb.Items.Add(New ListItem(text, value);


To find and remove a period at the end of the text,

text = (text.Substring(text.length - 1, 1) == "."? text.substring(0, text.length - 1) : text);
 
Share this answer
 
v2
Comments
Philippe Mori 23-Apr-12 18:31pm    
Although it works, TrimEnd is much simpler to write.

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