reagan123 Posted October 7, 2005 Posted October 7, 2005 (edited) need basic help splitting string... hello, I need help parsing a string in c#... I can't find an example online, just one of those days I guess. What i'm trying to do is to parse the following string at the '&'. I want to get the values so I can then put them into a database table. RESULT=126&PNREF=V63F0A7A001D&RESPMSG=Under review by Fraud Service&AUTHCODE=010101&AVSADDR=Y&AVSZIP=Y&IAVS=N&PREFPSMSG=Review CeilingAmount&POSTFPSMSG=Review Can anyone get me started or point me in the right direction? :o I've looked into the split method... seems like it may work but i'm not sure if it will do what i'm looking for. Thanks Edited October 7, 2005 by reagan123 Quote
*Experts* DiverDan Posted October 7, 2005 *Experts* Posted October 7, 2005 I think the split method is your best and easiest bet. I hope that you can easily convert this into C#. Dim values() As String = RESULT.Split("&") Dim i As Interger For i = 0 to values.GetUpperBound(0) MessageBox.Show(values(i)) Next Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
reagan123 Posted October 8, 2005 Author Posted October 8, 2005 hmm.. seems to work but now i have another problem that gives me an array with the values like this correct? RESULT=126 &PNREF=V63F0A7A001D &RESPMSG=Under review by Fraud Service &AUTHCODE=010101 &AVSADDR=Y &AVSZIP=Y &IAVS=N &PREFPSMSG=Review CeilingAmount &POSTFPSMSG=Review What i need to do know is get the value on the right hand of the '=' sign and assign that to a variable. Is that possible? I may be approaching this in the wrong way. Quote
Leaders snarfblam Posted October 8, 2005 Leaders Posted October 8, 2005 You can use String.IndexOf to get the character index of the equals sign and String.Substring to get a portion of the string. If you are using VS or #Develop, type in "String." and a list of all sorts of nifty functions and properties comes up. Quote [sIGPIC]e[/sIGPIC]
cyrilgupta Posted October 8, 2005 Posted October 8, 2005 Back in the days of VB6 i did it with Instr and mid. I think you can do the same thing still. I think marble_eater revealed a big secret when he advised you to study all the string functions, trust me learning helps. Quote
*Experts* DiverDan Posted October 8, 2005 *Experts* Posted October 8, 2005 I've been beaten to the post by the guys above. Your array will include the variable, an equal sign and a value. As marble eater mentioned, there are alot of different ways to separate the value from the array string: Using IndexOf - Replace, Remove, SubString Using Regular Expressions Using the split method again with a "=" delimiter And more that we haven't mentioned. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
pelikan Posted October 8, 2005 Posted October 8, 2005 (^|&)(?<fieldname>\W+)=(?<fieldvalue>[^&]+) Quote IN PARVUM MULTUM
reagan123 Posted October 10, 2005 Author Posted October 10, 2005 i'm lost again... here's what i've come up with so far.. any comments or suggestions appreciated. string test = "RESULT=126&PNREF=V64F0A7D0BC2&RESPMSG=Under review by Fraud Service&AUTHCODE=010101&AVSADDR=Y&AVSZIP=Y&IAVS=N&PREFPSMSG=Review CeilingAmount&POSTFPSMSG=Review"; string[] arInfo = new string[12]; // define which character is seperating fields char[] splitter = {'&'}; arInfo = test.Split(splitter); for(int x = 0; x < arInfo.Length; x++) { Response.Write(arInfo[x]); } } not sure where to go from here.... :( i'm in the process of looking at the string properties... I'm still not sure how to assign the values to a variable.... Quote
reagan123 Posted October 10, 2005 Author Posted October 10, 2005 this seems to get all of the values on the right hand side of the equals sign. I still can't figure out how to assign these values to variables? Sorry for being such a basic question. Thanks so much for the help so far!!! string test = "RESULT=126&PNREF=V64F0A7D0BC2&RESPMSG=Under review by Fraud Service&AUTHCODE=010101&AVSADDR=Y&AVSZIP=Y&IAVS=N&PREFPSMSG=Review CeilingAmount&POSTFPSMSG=Review"; string[] arInfo = new string[12]; // define which character is seperating fields char[] splitter = {'&'}; arInfo = test.Split(splitter); for(int x = 0; x < arInfo.Length; x++) { int y = arInfo[x].IndexOf("="); string z = arInfo[x].Substring(y+1); Response.Write(z); } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.