Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using this to remove ',' and its working fine and i am using str as global variable but i get error if string is empty so how to handle error when i cal global variable in aspx page.


string _str = text.Remove(text.LastIndexOf(','));
Posted
Updated 1-Feb-12 21:07pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Feb-12 3:06am    
What global variable are you talking about? There are no global variables in C#.
--SA
Sergey Alexandrovich Kryukov 2-Feb-12 3:06am    
What is "call a variable"? There is no such thing.
--SA

There could be a number of ways to do this.

You could just do this
<br />
if text.length > 0 && text.Contains(',')<br />
string _str = text.Remove(text.LastIndexOf(',')); 


Another approach could be to club the if statement in the same line
string _str = text.Length>0 && text.Contains(',')?text.Remove(text.LastIndexOf(',')):String.Empty;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Feb-12 3:23am    
Same thing in more detail, could be useful for the OP (who is apparently too weak), so my 5.
--SA
Abhinav S 2-Feb-12 3:26am    
Thank you.
First, you do not "call" any variables, and you don't have any global variables.

If you have a problem with null string, you can always check if (text == null) … or if (string.IsNullOrEmpty(text)) …

—SA
 
Share this answer
 
Comments
Abhinav S 2-Feb-12 3:12am    
String.Empty will throw an error as well so IsNullOrEmpty is the best option. My 5.
I've checked for , as well - otherwise OP will still get an error.
Sergey Alexandrovich Kryukov 2-Feb-12 3:22am    
Great. Thank you for your confirmation and your vote.
--SA
b = dtsubstancename.Rows[i]["Subs_name"].ToString() + "," + b;

if (b != "")
{
int backSlashIndex = b.IndexOf(",");
weaklypositive = (backSlashIndex >= 0) ? b.Remove(b.LastIndexOf(',')) : b;

lblwp.Visible = true;
lblrwp.Visible = true;

}
 
Share this answer
 

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