Click here to Skip to main content
15,912,327 members
Home / Discussions / C#
   

C#

 
GeneralRe: Parse from string Pin
jsc4214-Dec-21 6:55
professionaljsc4214-Dec-21 6:55 
AnswerRe: Parse from string Pin
#realJSOP15-Dec-21 0:35
professional#realJSOP15-Dec-21 0:35 
You don't have to check for parens before deleting them. This will work:

C#
// remove parens, remove all spaces, and trim the ends. You should end up with a 
// pure string for parsing.
s = s.Replace("(", "").Replace(")", "").Replace(" ", "").Trim();

I would also make the parser work more generally:
C#
List<string> parts = new List<string>();
// restrict the number of parts to 2
parts.AddRange(s.Split(new char[]{',' }, 2));
// fill in missing "parts" to compensate for 1 or no values
// (string would be "1" or empty)
while (parts.Count < 2)
{
    parts.Add("0");
}
// make a valid value for parts that are present but not specified
// string would be "1,", or ",2"
for(int i = 0; i < parts.Count; i++)
{
    int value;
    if (Int32.TryParse(parts[i], out value))
    {
        parts[i] = value.ToString();
    }
    else
    {
        parts[i] = "0";
    }
}


At this point you now have a reasonably valid list of values, and you can use the .net Int32.TryParse() method.

C#
int x;
if (!Int32.TryParse(parts[0], out x))
{
    x = 0;
}
int y; 

if (!Int32.TryParse(parts[0], out y))
{
    y = 0;
}

// by the time you get here, x and y are valid integer values
Point myPoint = new Point(x,y);


EDIT ====================
Fixed some code typos
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013


modified 15-Dec-21 13:03pm.

GeneralRe: Parse from string Pin
jsc4215-Dec-21 0:49
professionaljsc4215-Dec-21 0:49 
GeneralRe: Parse from string Pin
#realJSOP15-Dec-21 6:54
professional#realJSOP15-Dec-21 6:54 
Questionc# Search in root/subtree active directory users Pin
jwradhe9-Dec-21 2:10
jwradhe9-Dec-21 2:10 
AnswerRe: c# Search in root/subtree active directory users Pin
Richard Andrew x6411-Dec-21 9:16
professionalRichard Andrew x6411-Dec-21 9:16 
QuestionAccess Exif Metadata Pin
Dave Dec20216-Dec-21 10:05
Dave Dec20216-Dec-21 10:05 
AnswerRe: Access Exif Metadata Pin
OriginalGriff6-Dec-21 10:19
mveOriginalGriff6-Dec-21 10:19 
GeneralRe: Access Exif Metadata Pin
Dave Dec20216-Dec-21 13:09
Dave Dec20216-Dec-21 13:09 
GeneralRe: Access Exif Metadata Pin
Luc Pattyn6-Dec-21 16:39
sitebuilderLuc Pattyn6-Dec-21 16:39 
GeneralRe: Access Exif Metadata Pin
Dave Dec20216-Dec-21 21:24
Dave Dec20216-Dec-21 21:24 
GeneralRe: Access Exif Metadata Pin
Dave Dec20217-Dec-21 17:02
Dave Dec20217-Dec-21 17:02 
GeneralRe: Access Exif Metadata Pin
Luc Pattyn7-Dec-21 17:20
sitebuilderLuc Pattyn7-Dec-21 17:20 
GeneralRe: Access Exif Metadata Pin
Dave Dec20217-Dec-21 20:51
Dave Dec20217-Dec-21 20:51 
GeneralRe: Access Exif Metadata Pin
Richard MacCutchan7-Dec-21 22:13
mveRichard MacCutchan7-Dec-21 22:13 
GeneralRe: Access Exif Metadata Pin
Richard Deeming7-Dec-21 22:17
mveRichard Deeming7-Dec-21 22:17 
QuestionCreated Access DB, now how to load a list into a single column in the newly created DB. Pin
Richard A Knox4-Dec-21 6:18
Richard A Knox4-Dec-21 6:18 
AnswerRe: Created Access DB, now how to load a list into a single column in the newly created DB. Pin
OriginalGriff4-Dec-21 6:48
mveOriginalGriff4-Dec-21 6:48 
GeneralRe: Created Access DB, now how to load a list into a single column in the newly created DB. Pin
jsc426-Dec-21 2:58
professionaljsc426-Dec-21 2:58 
QuestionPass field Pin
Luis M. Rojas3-Dec-21 2:25
Luis M. Rojas3-Dec-21 2:25 
AnswerRe: Pass field Pin
Richard Deeming3-Dec-21 2:56
mveRichard Deeming3-Dec-21 2:56 
Questionthread safety and performance Pin
Super Lloyd2-Dec-21 15:31
Super Lloyd2-Dec-21 15:31 
GeneralRe: thread safety and performance Pin
harold aptroot2-Dec-21 16:03
harold aptroot2-Dec-21 16:03 
GeneralRe: thread safety and performance Pin
Super Lloyd2-Dec-21 17:59
Super Lloyd2-Dec-21 17:59 
AnswerRe: thread safety and performance Pin
trønderen3-Dec-21 2:36
trønderen3-Dec-21 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.