Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Text extraction -- This one will make you dizzy

Text extraction -- This one will make you dizzy

Scheduled Pinned Locked Moved C#
help
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AngryC
    wrote on last edited by
    #1

    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.

    P G 2 Replies Last reply
    0
    • A AngryC

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        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.

        A Offline
        A Offline
        AngryC
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • A AngryC

          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.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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]

          A 1 Reply Last reply
          0
          • L Luc Pattyn

            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]

            A Offline
            A Offline
            AngryC
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • A AngryC

              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.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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;

              A 1 Reply Last reply
              0
              • G Guffa

                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;

                A Offline
                A Offline
                AngryC
                wrote on last edited by
                #7

                Can you please give me some code, I'm really a beginner in C#.

                G 1 Reply Last reply
                0
                • A AngryC

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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]

                  1 Reply Last reply
                  0
                  • A AngryC

                    Can you please give me some code, I'm really a beginner in C#.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    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;

                    A 2 Replies Last reply
                    0
                    • G Guffa

                      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;

                      A Offline
                      A Offline
                      AngryC
                      wrote on last edited by
                      #10

                      Ok sorry :) I did what you said except: "then join that array again except the first s1 items and the last s2 items." How can I do that? Thanks a lot.

                      1 Reply Last reply
                      0
                      • G Guffa

                        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;

                        A Offline
                        A Offline
                        AngryC
                        wrote on last edited by
                        #11

                        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:

                        G 1 Reply Last reply
                        0
                        • A AngryC

                          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:

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          Use the String.Join method, like this: string result = String.Join(" ", arrayOfStrings, s1, arrayOfStrings.Length - s1 - s2)

                          --- single minded; short sighted; long gone;

                          A 1 Reply Last reply
                          0
                          • G Guffa

                            Use the String.Join method, like this: string result = String.Join(" ", arrayOfStrings, s1, arrayOfStrings.Length - s1 - s2)

                            --- single minded; short sighted; long gone;

                            A Offline
                            A Offline
                            AngryC
                            wrote on last edited by
                            #13

                            THANK YOU SO MUCH.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups