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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. little problem with strings

little problem with strings

Scheduled Pinned Locked Moved Visual Basic
helpquestionlounge
6 Posts 2 Posters 1 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.
  • S Offline
    S Offline
    Stephan Wright
    wrote on last edited by
    #1

    I do want to search paragraphs for strings. I managed to do so, if the string is somewhere inbetween like the word "ten" in "I had ten cups of tea!". But what I was not able to do is the following: Searchstring is at the end of the sentence like "See you at ten." In there I did not mangage to erease the ".". Maybe the left function will work. But I have to take care not to eliminate too much, because I will have to keep 1.2 (meaning the dot inbetween) but I will have to remove it when it looks like "1.". In general: 1.x (where x is any nonprintable character or nothing) should be 1 1.2.x (x like above) should be 1.2 but 1.2.3 should remain 1.2.3! Any suggestions? I thought I could use the find operation which I got through recording a macro and using the find operation. But then I will get the 8 in 8.1 and 8.1.1 as well. Well maybe I should really try to use the left and right function to eliminate the dots etc. So any suggestion is very welcome! Stephan.

    R 1 Reply Last reply
    0
    • S Stephan Wright

      I do want to search paragraphs for strings. I managed to do so, if the string is somewhere inbetween like the word "ten" in "I had ten cups of tea!". But what I was not able to do is the following: Searchstring is at the end of the sentence like "See you at ten." In there I did not mangage to erease the ".". Maybe the left function will work. But I have to take care not to eliminate too much, because I will have to keep 1.2 (meaning the dot inbetween) but I will have to remove it when it looks like "1.". In general: 1.x (where x is any nonprintable character or nothing) should be 1 1.2.x (x like above) should be 1.2 but 1.2.3 should remain 1.2.3! Any suggestions? I thought I could use the find operation which I got through recording a macro and using the find operation. But then I will get the 8 in 8.1 and 8.1.1 as well. Well maybe I should really try to use the left and right function to eliminate the dots etc. So any suggestion is very welcome! Stephan.

      R Offline
      R Offline
      rwestgraham
      wrote on last edited by
      #2

      Use the Instr() function. It is not affected by the character after the search pattern. For example, I tested Instr() on the folowing string: "Ten people ordered ten cups of tea at ten." and the function correctly returned the position of all three occurences of the substring "ten". Robert

      S 1 Reply Last reply
      0
      • R rwestgraham

        Use the Instr() function. It is not affected by the character after the search pattern. For example, I tested Instr() on the folowing string: "Ten people ordered ten cups of tea at ten." and the function correctly returned the position of all three occurences of the substring "ten". Robert

        S Offline
        S Offline
        Stephan Wright
        wrote on last edited by
        #3

        well yes I guess I will get every occuring of the string but I just tried and it didn't do it totally correct. Example: I do have the enumartion of the chapters. Lets say they are 8, 8.1, 8.1.1, 8.2 and so on If I do use instr() on them to search for "8" then it matches all of them! But I do not want 8.1, 8.1.1 and so on in this case. What I want to do is a strict stringmatching. So like in your example if the last "ten" would look like "ten.twenty" then I would like to get the "ten" but ignore the ".twenty" afterwards. So my search should parse through the paragraphs to find the string. another example: Let's say I do have these two paragraphs If you want to know more about this product please have a look at clause 8.1.2. To find the cheapest product in our shop please have a look at chapter 8. So let's assume we do want to get the "8" but not the 8.1.2. Instr() searches every occurence of "8" within a string. But in my example above there would be a hit in "8.1.2" aswell, which is not desired. That's my problem. As you might have realized in the second paragraph, there is a dot and a linefead afterwards. So if I compared this string, after having split it in parts at the blanks, to "8" it would not match, because of the dot and linefead. So what I am looking for is a solution to remove things like that, to get the hit, but not to influence the string matching in any other case, so e.g. if the "8" would have been somewere near the center of the paragraph, without anything behind. Well, as I said in my last posting, I guess I will have to find a way using left() and right() to check what is behind the string, so to find out if it is 8.1.2 or 8. and linefeed. But thanks anyway. Stephan.

        R 1 Reply Last reply
        0
        • S Stephan Wright

          well yes I guess I will get every occuring of the string but I just tried and it didn't do it totally correct. Example: I do have the enumartion of the chapters. Lets say they are 8, 8.1, 8.1.1, 8.2 and so on If I do use instr() on them to search for "8" then it matches all of them! But I do not want 8.1, 8.1.1 and so on in this case. What I want to do is a strict stringmatching. So like in your example if the last "ten" would look like "ten.twenty" then I would like to get the "ten" but ignore the ".twenty" afterwards. So my search should parse through the paragraphs to find the string. another example: Let's say I do have these two paragraphs If you want to know more about this product please have a look at clause 8.1.2. To find the cheapest product in our shop please have a look at chapter 8. So let's assume we do want to get the "8" but not the 8.1.2. Instr() searches every occurence of "8" within a string. But in my example above there would be a hit in "8.1.2" aswell, which is not desired. That's my problem. As you might have realized in the second paragraph, there is a dot and a linefead afterwards. So if I compared this string, after having split it in parts at the blanks, to "8" it would not match, because of the dot and linefead. So what I am looking for is a solution to remove things like that, to get the hit, but not to influence the string matching in any other case, so e.g. if the "8" would have been somewere near the center of the paragraph, without anything behind. Well, as I said in my last posting, I guess I will have to find a way using left() and right() to check what is behind the string, so to find out if it is 8.1.2 or 8. and linefeed. But thanks anyway. Stephan.

          R Offline
          R Offline
          rwestgraham
          wrote on last edited by
          #4

          Hi, In that case, use the String.ToCharArray to copy the text into a character array, then enumerate the characters looking for your target string. Then you can use the index to check the next characters and determine whether you consider it a match or not. It may take a little effort to get the loop logic correct, but so would using combinations of Left() and Right(). PLus I think it will run a lot faster than trying to do the same thing with left and right calls. Good Luck, Robert

          S 2 Replies Last reply
          0
          • R rwestgraham

            Hi, In that case, use the String.ToCharArray to copy the text into a character array, then enumerate the characters looking for your target string. Then you can use the index to check the next characters and determine whether you consider it a match or not. It may take a little effort to get the loop logic correct, but so would using combinations of Left() and Right(). PLus I think it will run a lot faster than trying to do the same thing with left and right calls. Good Luck, Robert

            S Offline
            S Offline
            Stephan Wright
            wrote on last edited by
            #5

            Is this function available in VBA for Word? Because I can't find it there. And the editior just asks for the end of the command at the dot. I guess this function is only available in .net but not in VBA. Stephan.

            1 Reply Last reply
            0
            • R rwestgraham

              Hi, In that case, use the String.ToCharArray to copy the text into a character array, then enumerate the characters looking for your target string. Then you can use the index to check the next characters and determine whether you consider it a match or not. It may take a little effort to get the loop logic correct, but so would using combinations of Left() and Right(). PLus I think it will run a lot faster than trying to do the same thing with left and right calls. Good Luck, Robert

              S Offline
              S Offline
              Stephan Wright
              wrote on last edited by
              #6

              I guess I could have done similar operations by using left() and right() parsing one character after the next one or copying it into an array, but well, I guess the result and the speed would be the same if not worse because of first copying it and afterwards parsing the array. I did the following: * I checked if the length of the searchstring is equal, then I found it * I checked if the length of the searchstring is one smaller (or the string in the paragraph one bigger respectively). If so I take the last char (with right and length operations) and compare it to the non printable or to a dot (this will get the 8.1. or 8.1x where x is the linefeed or tab or ...) * I checkd if the length of the searchstring is two smaller (or the string in the paragraph two bigger respectively). If so I once again take the last char to check, if it is a nonprintable. if it is, then I do have something like 8.1.x and not 8.1.2 e.g. Except in the first case I do range.select the paragraph and have a look for this word, by using the comparestring. I just tried with a test document, and it worked with the testtext which is part of a bigger one. Well I know this might not be correct in every case but it matches most of these cases. And besides I do parse every word in the paragraph, and if I find a possible match (using the InStr()) the operations above take place. To avoid parsing strings once again in the same paragraph, I marked those I already did the operations from above. Well, I guess I will try the other solution (rebuild the tochararray operation and the comparing every char) as well to find out which of those has fewer errors or even none! Thanks anyway! Stephan.

              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