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.
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.
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.
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.
nasi (May 8th, 2010)
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.
would you please tell me what is O(n)
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.
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.
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.
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).
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.
String str = new String("href=\"http://en.wikipedia.org/wiki/Primary_care\" l=\"l\"");
Chris
nasi (May 23rd, 2010)
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).
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?
Have you looked at the definition of the String.contains() method?
it saysCharSequence is an interface that a class can implement. The String class implements it.Returns true if and only if this string contains the specified sequence of char values.
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.