locked
Help with extracting middle of string RRS feed

  • Question

  • Hi all,

    I've got many image strings in the form of for example "TestImage.jpg?width=200&height=400?n=4899" and what I want to do is is remove the width and height (if they exist and of course they will have different values, the file extension could be a png or a gif).

    What is the easiest way of getting the middle part of that string providing they exist?

    Thanks

    Wednesday, July 11, 2012 3:48 PM

Answers

  • You can extract string from first position(0 th) index and last position (n th) index from the following code. Do not forget to add some defensive logic as well.

    string YourValue = "TestImage.jpg?width=200&height=400?n=4899";
                string[] fileName = YourValue.Split('?');
                //fileName[0] will be TestImage.jpg
                //fileName[1] will be width=200&height=400
                //fileName[2] will be n=4899


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    • Proposed as answer by Nair S Wednesday, July 11, 2012 6:50 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:27 AM
    Wednesday, July 11, 2012 3:59 PM
  • string url = "TestImage.jpg?width=200&height=400&n=4899";
    string[] parts = url.Split('?');
    string filename = parts[0];
    string query = parts[1];
    string[] queryTerms = query.Split('&');
    foreach(string term in queryTerms)
    {
       string[] keyAndValue = term.Split('=');
       string key = keyAndValue[0];
       string value = keyAndValue[1];
    }

    There are more sophisticated ways of doing this (with regular expressions, for example) and there are also classes in the System.Web namespace that can help you parse URLs and extract the key-value pairs out of the query parameters, but this is a very simple example which illustrates a simple approach.

    This breaks the string down into its various parts.  If you are trying to split the filename from its extension, use a Split('.') on filename and use index 0 as the name and index 1 as the extension.

    Evan


    • Edited by Evan Machusak Wednesday, July 11, 2012 3:54 PM
    • Proposed as answer by Kris444 Wednesday, July 11, 2012 3:54 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:26 AM
    Wednesday, July 11, 2012 3:52 PM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String image = "TestImage.jpg?width=200&height=400?n=4899";
                image = Regex.Replace(image, @"\??width=\d+\&height=\d+.+", "");
            }
        }
    }
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

    • Proposed as answer by Nair S Wednesday, July 11, 2012 6:50 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:27 AM
    Wednesday, July 11, 2012 3:59 PM
  • How about:

            static string GetWidthAndHeight(string input)
            {
                string[] parsedParameterSet = input.Split('?');
    
                foreach (string parameter in parsedParameterSet)
                {
                    if (parameter.StartsWith("width"))
                    {
                        return parameter;
                    }
                }
    
                return string.Empty;
            }
    
            static void Main(string[] args)
            {
                string testString = "TestImage.jpg?width=200&height=400?n=4899";
    
                if (testString.Contains("width"))
                {
                    string fileName = testString.Replace("?" + GetWidthAndHeight(testString), string.Empty);
                }
            }
    Looping through the string searching for the boundary of the "width" and "height" parameters to find the indexes can be written to be more efficient, but using the canned APIs is much more efficient to develop.

    David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.




    Wednesday, July 11, 2012 4:00 PM
  • if you need some kind of code:

    if (ImageString.Contains("height")){     // Do Something //  }

    inside, you may need to split the whole string with '?', then replace that part of the ImageString with blank string('').

    string [] HeightString = ImageString.Split('?');

    then loop through this array HeightString to check what value contains height.

    then, ImageString =ImageString.Replace(HeightString[count], "");

    i dont have vs now, hence cant write the whole piece of code. hope it is helpful.

    regards

    joon

     


    Wednesday, July 11, 2012 4:01 PM

All replies

  • string url = "TestImage.jpg?width=200&height=400&n=4899";
    string[] parts = url.Split('?');
    string filename = parts[0];
    string query = parts[1];
    string[] queryTerms = query.Split('&');
    foreach(string term in queryTerms)
    {
       string[] keyAndValue = term.Split('=');
       string key = keyAndValue[0];
       string value = keyAndValue[1];
    }

    There are more sophisticated ways of doing this (with regular expressions, for example) and there are also classes in the System.Web namespace that can help you parse URLs and extract the key-value pairs out of the query parameters, but this is a very simple example which illustrates a simple approach.

    This breaks the string down into its various parts.  If you are trying to split the filename from its extension, use a Split('.') on filename and use index 0 as the name and index 1 as the extension.

    Evan


    • Edited by Evan Machusak Wednesday, July 11, 2012 3:54 PM
    • Proposed as answer by Kris444 Wednesday, July 11, 2012 3:54 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:26 AM
    Wednesday, July 11, 2012 3:52 PM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String image = "TestImage.jpg?width=200&height=400?n=4899";
                image = Regex.Replace(image, @"\??width=\d+\&height=\d+.+", "");
            }
        }
    }
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

    • Proposed as answer by Nair S Wednesday, July 11, 2012 6:50 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:27 AM
    Wednesday, July 11, 2012 3:59 PM
  • You can extract string from first position(0 th) index and last position (n th) index from the following code. Do not forget to add some defensive logic as well.

    string YourValue = "TestImage.jpg?width=200&height=400?n=4899";
                string[] fileName = YourValue.Split('?');
                //fileName[0] will be TestImage.jpg
                //fileName[1] will be width=200&height=400
                //fileName[2] will be n=4899


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    • Proposed as answer by Nair S Wednesday, July 11, 2012 6:50 PM
    • Marked as answer by Jason Dot Wang Thursday, July 19, 2012 8:27 AM
    Wednesday, July 11, 2012 3:59 PM
  • How about:

            static string GetWidthAndHeight(string input)
            {
                string[] parsedParameterSet = input.Split('?');
    
                foreach (string parameter in parsedParameterSet)
                {
                    if (parameter.StartsWith("width"))
                    {
                        return parameter;
                    }
                }
    
                return string.Empty;
            }
    
            static void Main(string[] args)
            {
                string testString = "TestImage.jpg?width=200&height=400?n=4899";
    
                if (testString.Contains("width"))
                {
                    string fileName = testString.Replace("?" + GetWidthAndHeight(testString), string.Empty);
                }
            }
    Looping through the string searching for the boundary of the "width" and "height" parameters to find the indexes can be written to be more efficient, but using the canned APIs is much more efficient to develop.

    David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.




    Wednesday, July 11, 2012 4:00 PM
  • if you need some kind of code:

    if (ImageString.Contains("height")){     // Do Something //  }

    inside, you may need to split the whole string with '?', then replace that part of the ImageString with blank string('').

    string [] HeightString = ImageString.Split('?');

    then loop through this array HeightString to check what value contains height.

    then, ImageString =ImageString.Replace(HeightString[count], "");

    i dont have vs now, hence cant write the whole piece of code. hope it is helpful.

    regards

    joon

     


    Wednesday, July 11, 2012 4:01 PM