Home > OS >  Find between 2 strings but Last Index should be first found C#
Find between 2 strings but Last Index should be first found C#

Time:12-12

Hello guys Im looking for solution to find text in string between 2 phrases, but for example not last one in string but first found. where:

mainString = <Message>1</Message><Message>2</Message><Messages>3</Message>
leftBracket = "<Message>"
rightBracket = "</Message>"

because right now it return me 1<Message><Message>2<Message><Message>3 but I want to return value 1

 private string between2strings(string mainString, string leftBracket, string rightBracket)
        {
            int pFrom = mainString.IndexOf(leftBracket)   leftBracket.Length;
            int pTo = mainString.LastIndexOf(rightBracket);

            string result = mainString.Substring(pFrom, pTo - pFrom);

            return result;
        }

I want to show all code after remove my contact info, or maybe I should change the method of reading data:

Here full xml in string Im trying to find data

<?xml version="1.0" encoding="utf-8"?>
<response>
    <Count>2</Count>
    <Messages>
        <Message>
            <Smstat>0</Smstat>
            <Index>20001</Index>
            <Phone> 4857782</Phone>
            <Content>Test4</Content>
            <Date>2021-12-11 14:24:23</Date>
            <Sca> 4879096</Sca>
            <SaveType>4</SaveType>
            <Priority>0</Priority>
            <SmsType>1</SmsType>
        </Message>
        <Message>
            <Smstat>0</Smstat>
            <Index>20000</Index>
            <Phone> 4857782</Phone>
            <Content>Test3</Content>
            <Date>2021-12-11 14:02:48</Date>
            <Sca> 4879096</Sca>
            <SaveType>4</SaveType>
            <Priority>0</Priority>
            <SmsType>1</SmsType>
        </Message>
    </Messages>
</response>

CodePudding user response:

I suggest you parse the XML document using the XmlDocument object and navigate its content using an XPath expression. Here is an example

using System;
using System.Xml;

namespace Test
{
    class Program
    {
        static void Main()
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("Test.xml");
            XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/response/Messages/Message");
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                Console.WriteLine(xmlNode.InnerXml);
            }
        }

    }
}

Fabio

CodePudding user response:

Use Split(String[], StringSplitOptions):

var mainString = "<Messages>1</Message><Messages>2</Message><Messages>3</Message>";
string[] parts = mainString.Split(new [] { "<Messages>", "</Messages>" }, 
                                  StringSplitOptions.RemoveEmptyEntries);

This yields the array: { "1", "2", "3" } and makes it easy to pick the required entry:

string result = parts[0];

But you may consider using XML processing routines from the System.Xml.Linq Namespace or the Html Agility Pack (also available as NuGet package).

CodePudding user response:

An answer using regular expression:

var mainString = "<Message>1</Message><Message>2</Message><Message>3</Message>";

string p  = "<[^>]*>([^<]*)<[^>]*>";
Regex r = new Regex(p);
foreach (Match item in r.Matches(mainString))
{
   Console.WriteLine(item.Groups[1]);
}
            }

This will output:

1
2
3

The regular expression can be read as:

  • <[^>]*>

-- find a <

-- followed by any character not being a > 0 or more times [^>]*

-- followed by a >

  • ([^<]*)

-- find any character, not a < 0 or more times

-- The () make it possible to reference this found piece.

  • <[^>]*>

-- same as first part, to find the closing tag.

P.S. Explaining regular expressions is not my most popular subject ...

  • Related