0

I want to get all of the values which is matches in a some string with using substring?

I gave the some code which finds the only one result. I need all of matches..

İs it possible?

int pFrom = icrasorgula_cevap.IndexOf("<dosyaId>") + "<dosyaId>".Length; 
int pTo = icrasorgula_cevap.LastIndexOf("</dosyaId>"); 

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

<root>
<object-array>
    <object-array>
        <DVOList>
            <liste>
                <AvukatDosyaKisiDVO>
                    <birimId>105344286</birimId>
                    <birimAdi>Merkezi Takip Sistemi</birimAdi>
                    <dosyaNo>2019/15333</dosyaNo>
                    <dosyaTurKod>294</dosyaTurKod>
                    <dosyaAcilisTarihi>2019-06-19 03:50:05.0</dosyaAcilisTarihi>
                    <dosyaDurumu>0</dosyaDurumu>
                    <birimTuru1>11</birimTuru1>
                    <birimTuru2>1101</birimTuru2>
                    <birimTuru3>1199</birimTuru3>
                    <dosyaId>523127202</dosyaId>
                    <dosyaKisiSize>1</dosyaKisiSize>
                    <dosyaTurAciklama>MTS Dosyası</dosyaTurAciklama>
                </AvukatDosyaKisiDVO>
            </liste>
            <liste>
                <AvukatDosyaKisiDVO>
                    <birimId>105428346</birimId>
                    <birimAdi>Merkezi Takip Sistemi</birimAdi>
                    <dosyaNo>2019/15333</dosyaNo>
                    <dosyaTurKod>294</dosyaTurKod>
                    <dosyaAcilisTarihi>2019-06-19 03:50:05.0</dosyaAcilisTarihi>
                    <dosyaDurumu>0</dosyaDurumu>
                    <birimTuru1>11</birimTuru1>
                    <birimTuru2>1101</birimTuru2>
                    <birimTuru3>1199</birimTuru3>
                    <dosyaId>523123427202</dosyaId>
                    <dosyaKisiSize>1</dosyaKisiSize>
                    <dosyaTurAciklama>MTS Dosyası</dosyaTurAciklama>
                </AvukatDosyaKisiDVO>
            </liste>
            <actualCount>0</actualCount>
            <currentPage>0</currentPage>
            <pageSize>0</pageSize>
            <cacheID>0</cacheID>
            <recordCount>0</recordCount>
        </DVOList>
    </object-array>
</object-array>

Get these with using Substring..

<dosyaId>523123427202</dosyaId>
<dosyaId>5231272023</dosyaId>
6
  • 7
    If you are trying to read XML you should use an XML parser, this is what they are designed for. Commented Jun 21, 2019 at 11:37
  • 1
    Alex K's valid point aside, there are overloads to IndexOf that take a start index, so you can write a loop that continues following the last match. Commented Jun 21, 2019 at 11:38
  • Not just for a xml code. With regular expresion is possible, but I am asking for substring. Can you give me a sample code? Commented Jun 21, 2019 at 11:38
  • If you use RegEx, you'll get an extra problem. Commented Jun 21, 2019 at 11:43
  • Yes you are right? So what can I do?? Commented Jun 21, 2019 at 11:46

1 Answer 1

2

An xml parser is obviously the correct option, but using your code one option is to loop (code assumes there are always pairs) - e.g.

 var icrasorgula_cevap = @"<dosyaId>first</dosyaId><dosyaId>second</dosyaId><dosyaId>third</dosyaId>";

 int index = 0;
 int pTo = 0;
 while ((index = icrasorgula_cevap.IndexOf("<dosyaId>", pTo)) >= 0)
 {
   int pFrom = index + "<dosyaId>".Length;
   pTo = icrasorgula_cevap.IndexOf("</dosyaId>", pFrom);

   string result = icrasorgula_cevap.Substring(pFrom, pTo - pFrom);
   Console.WriteLine(result); // first, second, third

   }
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate for your answer. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.