Click here to Skip to main content
14,879,397 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
This is my code snippet.
C#
for(int i=0; i<=textbox.text.length;i++)
{ 
textbox.text.tostring[i].tostring; 
int index=textbox.text.tostring.lastindexof(",");
if(i.equals(index)) 
 { 
  textbox.text=textbox.text.remove(index, 1);
  } 
}


I happen to have a number of email addresses in a textbox in this format me@yahoo.com, you@yahoo.com, but i need to delete the last character(,) only. The problem is it deletes both characters.
Posted
Updated 12-Apr-11 0:08am
v2

I would suggest you try this

textbox.Text.Remove(textbox.Text.LastIndexOf(','),1);


its easier way and you can use it in whatever the event you would like to use it in

I hope I helped
:-)
   
Comments
capdevillia 12-Apr-11 7:52am
   
Thank u bro. Actually my solution did work too but problem was that it was within a while loop. Thanks..
The problem is within your loop condition, there should be no need for that.

Replace your for loop code with this one :


textbox.Text = textbox.Text.Remove(Textbox.Text.LastIndexOf(','), 1);
   
Comments
capdevillia 12-Apr-11 7:35am
   
It still removes all the characters
Sergey Alexandrovich Kryukov 12-Apr-11 12:59pm
   
Do it does not, it removed only one character, or none.
--SA
Pong D. Panda 12-Apr-11 22:23pm
   
Dont use the loop, replace your whole loop statement with that line of code
This should work :

C#
String orgText = textbox.text;
int i = orgText.LastIndexOf(",");
if(i != -1)
{
textbox.text = orgtext.Remove(i);
}
   
v2
Comments
Tarun.K.S 12-Apr-11 6:47am
   
Edit : Added an if condition.
try this
TextBox1.Text = "aa,bb,cc,dd,ee;
string myString = TextBox1.Text;
               TextBox1.Text = myString.Replace(",", "");

//or if you want space between values 

TextBox1.Text = myString.Replace(","," ");
   
v2
This is the code sample, try this;
string value = "this is a test string.,";
value = value.Trim();
////verify before removing any character, to avoid the index exception
if (value.Length > 1 && value.EndsWith(","))
{
    textbox.text = value.Substring(0, value.Length - 1);
}
   
v5
Comments
Johnny J. 12-Apr-11 6:43am
   
You don't need to check Length > 1 - EndsWith(",") will return false if the string is empty...
avigodse 12-Apr-11 6:49am
   
what if user enters only ',' or ', ' in textbox?
Johnny J. 12-Apr-11 6:53am
   
Then surely it should be removed. There's no logic in having JUST a comma in the box. Your code will leave it there.
avigodse 12-Apr-11 14:53pm
   
Thank a lot SA for down-vote. You are very unprofessional, read that you are not the only expert. Some people prove to be just hopeless.
zingoraa 13-Apr-11 3:27am
   
do not use this site for blame game.
You try this
string[] a = TextBox.Text.ToString().Split(',');


now all item email id retrieve through loop.
   
Comments
avigodse 12-Apr-11 6:23am
   
i did not down-vode???
actually this is a closest answer, it he wants to get only email-ids from it.
Johnny J. 12-Apr-11 6:34am
   
But that's not what the OP wanted...
Just try this.

If you want to remove all commas try this.
textbox.text.tostring.Replace(",", "");

If you want to remove only last comma try this.
textbox.text.tostring.Remove(textbox.text.tostring.Length - 1, 1);
   
v2
Comments
avigodse 12-Apr-11 6:16am
   
No, first will remove all the commas (",") from the string.
and second will remove anything thats at last place.
i did not down-vote. :)
Toniyo Jackson 12-Apr-11 6:28am
   
Check the answer now.

Somebody down voting every answer.
Johnny J. 12-Apr-11 6:56am
   
>Somebody down voting every answer.

Me - and I didn't downvote EVERY answer - only the wrong answers and the poor practise ones. That is what the voting system is there for!
Toniyo Jackson 12-Apr-11 6:58am
   
Okay. Is this answer is wrong?
Johnny J. 12-Apr-11 7:02am
   
Half of it anyway. The OP clearly states that he does NOT want to remove ALL commas. So why post the code for that as the first you do?
Try something like:

C#
string myText = textbox.text.Trim();

                if (myText.EndsWith(","))
                    myText = myText.Substring(0, myText.Length - 1);


BTW: There's no need to call ToString on the TextBox's Text property - it's already a string.

Good luck!
   
v2
Comments
E.F. Nijboer 12-Apr-11 6:47am
   
What's the reason you are down voting me (and probably everyone else)? The overhead of my answer would prevent extra commas and would work with or without the extra comma at the end. I think it would be more up to the one asking the question to judge it and choose what solution would work best for him. Voting 1 without any public comment on a perfectly good answer is somewhat weak don't you think?
Johnny J. 12-Apr-11 6:50am
   
Your answer is not the answer to the OP's question. Why should I upvote somebody who answers "Thursday" to the question "What colour is an orange?"
Johnny J. 12-Apr-11 6:52am
   
Besides: I added a comment, but I later deleted it because I found it irrelevant in this case, but I'm sure you must have gotten an email of it anyway. The contents was: Using Join and Split to remove a comma from the end of a string is timeconsuming overkill. It's simply not a good solution. THAT'S why I downvoted your solution. I'm sure that I'm allowed to have an opinion as well!
Johnny J. 12-Apr-11 6:58am
   
BTW: If you downvoted my answer here just because you're pissed that I downvoted your irrelevant post, then you're wrong - my answer was useable and relevant to the question unlike yours. Try to keep people and coding apart... You've completely misunderstood the voting system.
capdevillia 12-Apr-11 7:20am
   
I had seen your solution johnny on another thread n it jammed. It deleted all commas. Thank u n way.
Johnny J. 12-Apr-11 7:23am
   
BS - then it's because you put it inside your for loop - you shouldn't do that - why do you have that at all?

[EDIT] Wait just a second - what do you mean "my solution on another thread" - THIS thread is my solution. If I have posted comments to somebody elses code then it is still not my solution. What preceisely do you mean? [/EDIT]
Tarun.K.S 12-Apr-11 7:28am
   
Please vote and accept the answer(s) that helped you.
capdevillia 12-Apr-11 8:11am
   
Oops sorry damn me! My solution did work too but was within while loop.
Johnny J. 12-Apr-11 8:13am
   
:-)
Simply split the string on each comma, removing empty entries and joining them comma separated. This way the last comma (or other extra unwanted commas) are deleted.
MIDL
String value = textbox.text;
char[] charSeparators = new char[] {','};
String result = String.Join(",", value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries));


Good luck!
   
v2

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