Text extraction -- This one will make you dizzy
-
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. Your help would be much appreciated.
-
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. Your help would be much appreciated.
I'm not really sure what you are trying to achieve here, but you can achieve what you want to do quite easily (and without hardcoding the result):
string[] splitter = var2.Split(' '); if (splitter != null && splitter.Length > 4) { xxx = string.Format("{0} {1}", splitter[2], splitter[3]); }
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before. -
I'm not really sure what you are trying to achieve here, but you can achieve what you want to do quite easily (and without hardcoding the result):
string[] splitter = var2.Split(' '); if (splitter != null && splitter.Length > 4) { xxx = string.Format("{0} {1}", splitter[2], splitter[3]); }
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before. -
Hi, Thank you very much. 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. So maybe I have to count spaces? Thank again.
I think this will come close to what you want:
public void test() {
string var1 = "5567 87865 4432 54535 6678 4435";
string var2 = "667 332 32 555677 4465 44444444";
string part_of_var1 = "4432 54535";
string xxx=getPartsOfVar2(var1, var2, part_of_var1, ' ');
Console.WriteLine(xxx);
|public string getPartsOfVar2(string var1, string var2, string part_of_var1, char sep) {
string[] arr1=var1.Split(sep);
string[] arr2=var2.Split(sep);
if (arr1.Length!=arr2.Length) throw some exception;
string[] arr1p=part_of_var1.Split(sep);
int partCount=arr1p.Length;
string[] arr2p=new string[partCount];
for(int i=0; i<partCount; i++) {
string part=arr1p[i];
int index=arr1.IndexOf(part);
if (index<0) throw some exception;
arr2p[i]=arr2[index];
}
return String.Join(sep, arr2p);
}:)
Luc Pattyn [My Articles]
-
I think this will come close to what you want:
public void test() {
string var1 = "5567 87865 4432 54535 6678 4435";
string var2 = "667 332 32 555677 4465 44444444";
string part_of_var1 = "4432 54535";
string xxx=getPartsOfVar2(var1, var2, part_of_var1, ' ');
Console.WriteLine(xxx);
|public string getPartsOfVar2(string var1, string var2, string part_of_var1, char sep) {
string[] arr1=var1.Split(sep);
string[] arr2=var2.Split(sep);
if (arr1.Length!=arr2.Length) throw some exception;
string[] arr1p=part_of_var1.Split(sep);
int partCount=arr1p.Length;
string[] arr2p=new string[partCount];
for(int i=0; i<partCount; i++) {
string part=arr1p[i];
int index=arr1.IndexOf(part);
if (index<0) throw some exception;
arr2p[i]=arr2[index];
}
return String.Join(sep, arr2p);
}:)
Luc Pattyn [My Articles]
Hello Luc, I'm getting the following errors when I'm trying to compile the code: Error 1 No overload for method 'IndexOf' takes '1' arguments ccc.cs 626 19 ccc Error 2 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments ccc.cs 630 12 ccc Error 3 Argument '1': cannot convert from 'char' to 'string' ccc.cs 630 24 ccc
-
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. Your help would be much appreciated.
Split var1 by part_of_var1, so that you get an array with two items. Count the number of spaces in each of them and put in s1 and s2. Split var2 by space, then join that array again except the first s1 items and the last s2 items. Not dizzy yet... ;)
--- single minded; short sighted; long gone;
-
Split var1 by part_of_var1, so that you get an array with two items. Count the number of spaces in each of them and put in s1 and s2. Split var2 by space, then join that array again except the first s1 items and the last s2 items. Not dizzy yet... ;)
--- single minded; short sighted; long gone;
-
Hello Luc, I'm getting the following errors when I'm trying to compile the code: Error 1 No overload for method 'IndexOf' takes '1' arguments ccc.cs 626 19 ccc Error 2 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments ccc.cs 630 12 ccc Error 3 Argument '1': cannot convert from 'char' to 'string' ccc.cs 630 24 ccc
I did not compile/run so you have to check the documentation and make small adaptations. As an example Array.IndexOf seems to be a static method with two args, so study and correct it. :)
Luc Pattyn [My Articles]
-
I figured out exactly how to do it for you. Can't you at least make an attempt at writing the code yourself?
--- single minded; short sighted; long gone;
-
I figured out exactly how to do it for you. Can't you at least make an attempt at writing the code yourself?
--- single minded; short sighted; long gone;
-
I'm still trying but I can't figure out how to "join that array again except the first s1 items and the last s2 items." :doh:
-
Use the String.Join method, like this:
string result = String.Join(" ", arrayOfStrings, s1, arrayOfStrings.Length - s1 - s2)
--- single minded; short sighted; long gone;