Tyler Adams

Tyler Adams

  • NA
  • 10
  • 0

How to extract certain data from a string

Feb 5 2009 1:42 PM

I'm working with an InfoPath form that has some C# code in it.  My question is how can i extract a chunk of data from a string value?  I've made it work using a .substring method and it returns the value that i'm looking for, however, i'm not 100% sure of the continuity of that field in Active Directory and i'm not sure that removing those characters would work for everyone.

Right now, the code retrieves the Address from Active Directory.  In active dirrectory (at least for the handful of users i checked) the address is "2575 Sand Hill Road - MS XX" (where XX can be a 2 or 3 digit number).

I want to extract just the MS XX and display that in the InfoPath field.  I know we have a lot of collaborators and people outside that have Active Directory accounts, which may or may not have an address listed and i'm not even 100% sure that our internal employees have the same entry (i'd imagine some of them might have 2575 Sand Hill Rd. - MS XX or something like that.  Is there a way i cen delimit it on the "MS" and take everything from that point to the right to account for all 2 and 3 digit numbers and allow for any possible 4 digit numbers i might not know about?

Here's the code i am using right now to remove the first 22 characters: (I'm a c# noob, so any help is appreciated!!)

string Address = employee.Properties["streetAddress"].Value.ToString();

string MailStop = Address.Substring(22);

//Displays the mailstop in the infopath form

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorMailstop", ns).SetValue(MailStop);


Answers (5)