Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: finding String in a file

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default finding String in a file

    Is there any straight way to find an string in a file? I know how to read a file line by line and see if the string exits there or not, but I am looking for a shorter way.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: finding String in a file

    Don't think there is a shorter way than reading the file. If you are on unix or linux you could grep it using the exec() command, but that ends up making the program platform specific.

  3. The Following User Says Thank You to copeg For This Useful Post:

    nasi (May 8th, 2010)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: finding String in a file

    With the given constraints (very few), the best you can get is O(n) by sequentially comparing the two data sets.

    You can speed up the amount of comparisons that are being done via multi-threading (have one thread to start reading from the front, have the second thread read from the end), but you are still left with an O(n) algorithm.

  5. #4
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: finding String in a file

    would you please tell me what is O(n)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: finding String in a file

    Big O notation - Wikipedia, the free encyclopedia
    It essentially describes the complexity/performance of an algorithm (usually in terms of a worst case scenario). O(n) is considered linear, eg its performance is directly related to the size of the input.

  7. #6
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: finding String in a file

    I have an html file and I am trying to find something like "<html>
    <head>

    </head>
    <body>
    <a href="http://en.wikipedia.org/wiki/Net_present_value" class="l">Net <em>present</em>
    value - Wikipedia, the free encyclopedia</a>"
    </body>
    </html>

    in it. when I see the html file in a browser I see that this content exists in the file but when I am trying to use following code it doesn't find the content that I am looking for

     
    // file has been read and its contet is in an StringBbuilder named Lines
    // what I am searching for is in a string named search
     
    if (lines.toString().contains(search))
    System.out.println("yes");
    else
    System.out.println("no");

    ofcourse when I check the source code of the file I see that there are some differences in comparison to the content that I am looking for. now I am thinking about two solutions. Is there any modification that I can do to fix the problem with the above approach? or is it possible to compare these two, using another way that doesn't compare the source codes but their form in web browser.or any other way?

    please help me I am really stuck.

  8. #7
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: finding String in a file

    How should I find

     href="http://en.wikipedia.org/wiki/Primary_care" l="l"

    in the following String?

     
    <html>
      <head>
     
      </head>
      <body>
        <a href="http://en.wikipedia.org/wiki/Primary_care" l="l">Primary <em>care</em> 
        - Wikipedia, the free encyclopedia</a>
     
        <div s="s">
          Primary <em>care</em> is the term for the health services that play a 
          central role in the local community. It refers to the work of health <em>care</em> 
          professionals who act <b>...</b><br><span f="f"><cite>en.wikipedia.org/wiki/Primary_<b>care</b></cite> 
          - </span><span gl="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:pnenOs-Aj-0J:en.wikipedia.org/wiki/Primary_care+care+site:wikipedia.org&cd=4&hl=en&ct=clnk">Cached 
          </a></span>
        </div>
      </body>
    </html>
    Last edited by nasi; May 22nd, 2010 at 02:47 AM.

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: finding String in a file

    String.indexOf(..) will give you the starting position. String.contains(..) will tell you if it is present.

    It's always worth checking the API of the class you're using - in fact, one might say you shouldn't use a class unless you know what it can do (you can find out exactly how it dies it in the API docs and tutorials).

  10. #9
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: finding String in a file

    Thanks. I know these methods. the problem is that
    String Str = new String("href="http://en.wikipedia.org/wiki/Primary_care" l="l"");

    doesn't work and generates errors because of multiple quotations("). I don't know how to wrtie this String.

  11. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: finding String in a file

    String str = new String("href=\"http://en.wikipedia.org/wiki/Primary_care\" l=\"l\"");

    Chris

  12. The Following User Says Thank You to Freaky Chris For This Useful Post:

    nasi (May 23rd, 2010)

  13. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: finding String in a file

    May be overkill but you might consider using a Regular Expression if you want your code to be a lot more extensible. Like I said in certain situations its overkill, but does the trick in more difficult situations where you are looking more for patterns rather than concrete strings (such as parsing html).

  14. #12
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: finding String in a file

    I have an String and I am looking for a subString in this String which starts with "what is new?" and ends with "Todays last news". I know that I can use string.contains(substring) but I don't know how to define the substring in a way that I mentioned. would you please tell me how to do it?

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding String in a file

    Have you looked at the definition of the String.contains() method?
    it says
    Returns true if and only if this string contains the specified sequence of char values.
    CharSequence is an interface that a class can implement. The String class implements it.

    To find a subString in a string that starts with x and ends with y, you could use: indexOf(x) and indexOf(y, x_loc), compute the offset and endLoc and then use the substring method to extract the substring.

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  3. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  4. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM
  5. Replies: 13
    Last Post: May 6th, 2009, 09:27 AM