Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string schoollist ='102','11','23','34'
string[] SchoolListArr = schoollist.Split(',');

for (int i = 0; i < SchoolListArr.Length; i++)
{
    //Console.WriteLine("The school List:" + SchoolListArr[i]);

 //   String s = null;

    String[] schools = new String[100];

    schools[i] = SchoolListArr[i];

    Console.WriteLine("The school List:" + schools[i]);

}
Console.ReadLine();

// now my question is how do I strip ( ' ') from the  string so that I have 

102
11
// instead of 
'102'
'11'

// thank you
Posted

For starters, the code you have put in your question will not compile.
string schoollist ='102','11','23','34' would need to be string schoollist ="'102','11','23','34'";

In order to replace values in a string, you could very simply use the string.Replace() method.
schoollist = schoollist.Replace("\'", string.Empty);

For more info on string.Replace: MSDN documentation[^]
 
Share this answer
 
Comments
Thomas Daniels 22-Aug-13 12:10pm    
You beat it to me! +5!
fjdiewornncalwe 22-Aug-13 12:12pm    
Cheers and Thanks.
rudolph098 22-Aug-13 12:32pm    
Thank you
Sergey Alexandrovich Kryukov 23-Aug-13 0:51am    
Sure, a 5.
—SA
Try:
C#
string schoollist = "'102','11','23','34'";
schoollist = schoollist.Replace("'", ""); // <== add this line
string[] SchoolListArr = schoollist.Split(',');

You also need to surround your schoollist string with quotes and add a semicolon at the end of the line, otherwise it won't compile.

Hope this helps.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 22-Aug-13 12:12pm    
5
Thomas Daniels 22-Aug-13 12:19pm    
Thank you!
rudolph098 22-Aug-13 12:33pm    
Thank you
Thomas Daniels 22-Aug-13 12:36pm    
You're welcome!
This is one way you could do it, using string.Trim(params char[])

Console.WriteLine("The school List:" + schools[i].Trim(char.Parse("'")));
 
Share this answer
 
It seems that your real question is, "How do I convert a string value into a numeric value?"

Inside your loop, the only line you need is
C#
schools[i] = Convert.ToInt32(SchoolListArr[i]);

You can use a cast instead, if you want; personally, I try to use .Net methods rather than language-specific syntax.

Move the declaration of schools to before the loop -- there is no need to keep recreating it -- make it an array of int and initialize it to have SchoolListArr.Length elements. I believe the writeline should be after the loop.
 
Share this answer
 
For stripping spaces (' ') you Trim the text.

http://msdn.microsoft.com/en-us/library/system.string.trim.aspx[^]
 
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