Angry Coder

Hello,

Let's say I have the following variables:

string var1 = "5567 87865 4432 54535 6678 4435";

string var2 = "667 332 32 555677 4465 44444444";

string part_of_var1 = "4432 54535";

string xxx = String.Empty;

I want to have in xxx: 32 555677!

Notice that part_of_var1 contains the third and fourth parts of var1. I want to have 32 555677 in xxx var because it is the third and fourth parts of var2.

I hope it's clear!

Please help.



Re: Visual C# General Text extraction -- This one will make you dizzy

Burak SARICA

You can use string.Split function, if you know which parts to get.




Re: Visual C# General Text extraction -- This one will make you dizzy

Angry Coder

The problem is I don't know the position at design or runtime. The only clue I have is the value of part_of_var1 variable.



Re: Visual C# General Text extraction -- This one will make you dizzy

Mark Dawson

Hi,

try something like:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string var1 = "5567 87865 4432 54535 6678 4435";
string var2 = "667 332 32 555677 4465 44444444";
string partOfVar1 = "4432 54535";

string[] splitString1 = var1.Split(new char[] { ' ' });
string[] splitString2 = var2.Split(new char[] { ' ' });
string[] splitPart = partOfVar1.Split(new char[] { ' ' });

if(splitPart.Length > splitString1.Length || (splitString1.Length != splitString2.Length))
{
//No match
return;
}

int matchIndexStart;
for (matchIndexStart = 0; matchIndexStart <= splitString1.Length - splitPart.Length; ++matchIndexStart)
{
bool foundMatch = true;
for (int j = 0; j < splitPart.Length; ++j)
{
if (splitPart[j] != splitString1[matchIndexStart + j])
{
foundMatch = false;
break;
}
}

if (foundMatch)
{
for (int i = 0; i < splitPart.Length; ++i)
{
Console.Write(splitString2[matchIndexStart + i] + " ");
}
}
}
}
}
}

Mark






Re: Visual C# General Text extraction -- This one will make you dizzy

Angry Coder

Hello,

Thanks a lot but I'm getting the same input!

And a notice saying Unreachable code detected:

Here:

for (int i = 0; i < splitPart.Length; ++i)

What's wrong





Re: Visual C# General Text extraction -- This one will make you dizzy

Mark Dawson

Hi,

I am not following what you mean by "same input". Did you modify the code from the example given, I can run the code sample without any compiler errors. The error you are getting suggests you put a return statement at some point in the function i.e.

int x = 0;

return;

int y = 0; //=> this is unreachable

which means some code will never be run. When I run the code sample it gives the correct result.

Mark.