C# coding

eli411
eli411 used Ask the Experts™
on
I have coded a c# function and stuck on this particular code.  Hope someone know now to help me on this one.

my code got the data from a 3rd party web service and return following data

PRE-QUAL AUTO FOR $45K SCORE:936 BW:88

Here is what I want to do is write a c# function that only taking the number between $ and K, so 45 will be what I am looking for in this case.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You just need to use a regular expression "(\$([0-9]+)[K])", http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

string input = "PRE-QUAL AUTO FOR $45K SCORE:936 BW:88";
            string pattern = "(\\$([0-9]+)[K])";
            if(System.Text.RegularExpressions.Regex.IsMatch(input, pattern))
            {
                string match = System.Text.RegularExpressions.Regex.Match(input, pattern).Value;
                int value = Convert.ToInt32(match.Substring(1, match.Length - 2));
            }
            else
            {
                // error, no match
            }

Open in new window

Senior Developer
Commented:
This is non-regular expression solution (easier to maintain).
You may need to add error handling

            string s = @"PRE-QUAL AUTO FOR $45K SCORE:936 BW:88 ";

            int dollarSignIndex = s.IndexOf("$");
            int kIndex = s.IndexOf("K", dollarSignIndex);
            string result = s.Substring(dollarSignIndex + 1, kIndex - dollarSignIndex - 1);
            MessageBox.Show(result);

Open in new window

Top Expert 2014
Commented:
Added to anarki_jimbel comments, please find my code which I have used in my project, which has worked for me:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FetchSubstringConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string webResponse = @"PRE-QUAL AUTO FOR $45K SCORE:936 BW:88";

                /* Input string from where substring has to be fetched */
                Console.WriteLine("Response output: " + webResponse);

                Console.WriteLine("Enter Start character from where Substring has to be filtered...");

                string startCharacter = Console.ReadLine();

                Console.WriteLine("Enter End character till where Substring has to be filtered...");

                string endCharacter = Console.ReadLine();

                int amount = Convert.ToInt32(GetStringBetweenCharacters(webResponse, Convert.ToChar(startCharacter), Convert.ToChar(endCharacter)));

                Console.WriteLine("Resultant Substring fetched is : " + amount);
                
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception has occured with current user inputs: " + ex.Message);
            }
        }

        /// <summary>
        /// This method is used to fetch the substring present in between two characters from the string
        /// </summary>
        /// <param name="content">Input parameter which holds the complete string</param>
        /// <param name="startCharacter">Input parameter which holds character value from where substring has to be filtered</param>
        /// <param name="endCharacter">Input parameter which holds character value till where substring has to be filtered</param>
        /// <returns>Substring value after filter</returns>
        public static string GetStringBetweenCharacters(string content, char startCharacter, char endCharacter)
        {
            try
            {
                string value = string.Empty;

                if (content.IndexOf(startCharacter) >= 0)
                {
                    /* Looping from index start of Filter Name till end character is identified */
                    for (int i = content.IndexOf(startCharacter) + 1; i < content.Length; i++)
                    {
                        if (content.ElementAt(i) != endCharacter)
                            value = value + content.ElementAt(i);
                        else
                            break;
                    }
                }

                return value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Open in new window

Shah
Different solutions for one issue :)

Author

Commented:
I used the second solution.  I tested all the solutions and they are working good.  The second solution is simpler and working better in my situation.  Thank you all!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial