Search string using substring

Ads
 

Search string using substring

I want to print a string by searching a substring of that string. - i want to print email-ids from word files (resumes) in a directory. Search can be made using @ in the word file. So far i can find index position of @, that too in a single text file.

import java.io.*;

class FileRead { public static void main(String[] args) { String searchText = "@"; String fileName = "aa.doc"; StringBuilder sb = new StringBuilder();

    try
    {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));

        //Reads until the end-of-file met
        while (reader.ready())
        {
            //Read line-by-line directly
            sb.append(reader.readLine());
        }
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }

    String fileText = sb.toString();
    System.out.println("Position in file : " + fileText.indexOf(searchText));
}

}

View Answers

January 12, 2011 at 5:46 PM

Hello Friend,

Try the following code:

import java.io.*;
import java.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class FileRead{
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {
file = new File("c:\\Hello.doc");
String st="";
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
st+=fileData[i]+" ";
}
StringTokenizer stk=new StringTokenizer(st," ");
while(stk.hasMoreTokens()){
String str=stk.nextToken();
if(str.indexOf('@')>0){
System.out.println(str);
}
}
}
catch(Exception exep){}
}
}

For the above code, you need apache poi library.

Thanks


January 16, 2011 at 11:16 PM

Thanks, the code was upto the mark.

Now, I want to scan all doc files in a directory and print all email ids.

I tried using these....

File directory = new File("C:/New Folder/"); String filename[] = directory.list();

  • and

File files[] = directory.listFiles(); for (File f : files) { // do whatever you want with each File f }

instead of file = new File("c:\Hello.doc");

but it didn't help.
Kindly help.

Ads









Related Tutorials/Questions & Answers:
Search string using substring
Search string using substring  I want to print a string by searching a substring of that string. - i want to print email-ids from word files (resumes) in a directory. Search can be made using @ in the word file. So far i can
find a substring by using I/O package
find a substring by using I/O package  Write a java program to find a sub string from given string by using I/O package
Advertisements
C String Substring
C String Substring       In this section, you will learn how to get the substring from a string in C. You can see in the given example, a string and a method substring
How to check if a string contains a substring in iOS?
Function for checking the presence of a substring in a string (NSString... the presence of substring in a string (NSString) and there is no readymade function... of a substring in a string. This function will simply tell you if NSString object
String substring(int beginIndex)
String substring(int beginIndex)   ... the substring(int beginIndex) method of String class. We are going to use substring(int beginIndex) method of String class in Java. The description of the code
how to search for string using LIKE operator instead of integer?
how to search for string using LIKE operator instead of integer?  ... to search for itemid, then it shows "dbtable.jsp" file with 3 columns of itemid... in itemid. however, what i want to do is i want the user to search for the itemname
C get Substring from String
C get Substring from String       This section illustrates you how to get the substring using... have consider will as str2 and the whole string as str1. The function strstr
String substring(int beginIndex, int endIndex)
String substring(int beginIndex, int endIndex)  ... explanation about the substring(int beginIndex, int endIndex) method of String class. We are going to use substring(int beginIndex, int endIndex) method of String
JavaScript indexOf substring
; In this section, we are going to find the location of substring using... the parameters substring is the specified string and 0 is the index that will allow to start the search for the specified substring. Here is the code
Java Substring
of String is called substring. Substrings are also a String. Substring is used... this method to find out the substring from a given String. substring() method... which is a substring of the current String. This method is written in two forms
string palindrom using vbscript?
string palindrom using vbscript?  string palindrom using vbscript
NSMutableArray Search String
NSMutableArray Search String  Hi all, In my iPhone SDK application.. i wanted to search from the table data. My question is ..do i need to innsert the table data into search array? thanks
SubString in Java
SubString(), a method of String class is used to get a part of original string.... If beginIndex is equal to length in SubString(int beginIndex) it will return empty String... String. Example of SubString in Java: import java.io.*; import
ModuleNotFoundError: No module named 'data-string-search'
ModuleNotFoundError: No module named 'data-string-search'  Hi, My... named 'data-string-search' How to remove the ModuleNotFoundError: No module named 'data-string-search' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'string-path-search'
ModuleNotFoundError: No module named 'string-path-search'  Hi, My... named 'string-path-search' How to remove the ModuleNotFoundError: No module named 'string-path-search' error? Thanks   Hi, In your
Passing a parameter using URL string
Passing a parameter using URL string  How to pass a parameter using the URL string in a JSF application
JPA Substring() Function
() function. The substring() function returns the some part of string arguments..., second is starting index and third is length of string . JPA Substring... JPA Substring() Function   
how to search the string arraylist contains database rows?
how to search the string arraylist contains database rows?  i need to search 2 database. one is excel database and another is SQL database. stored the row values into string arraylist. now i want to print the common rows existed
find and substring
String"); String findstring = keybord.readLine(); String substring = keybord.readLine(); findreplace(findstring,substring); } catch(IOException io...find and substring   **import java.io.BufferedReader; import
Auto Search box using ajax
Auto Search box using ajax   Hi , i want to implement auto search box functionality like google and linkedin using ajax,jsp,servlet, javascript etc.when the value appears on the textbox then we should also be able to select
search functionality using jsp from database
search functionality using jsp from database  search functionality using jsp from database
How to perform search using AJAX?
How to perform search using AJAX?  Hi, I have following HTML code...; <td><label for="status">Search Status</td> <td><input..." value="Please enter search criteria"/></td> </tr> <tr>
How to substring in Java?
How to substring in Java?  Hi, I want to get a part of String... the substring() method of Java for getting a part of string based on the index number passed to substring() function. Here is an example: String string = "Deepak Kumar
String Reverse Using StringUtils
String Reverse Using StringUtils       In this example we are going to reverse a given string using StringUtils api. In this example we are reversing a string and the reversed
Taking Substring
are using the substring() method to get certain characters from the big string... string. You will learn how to use the substring() method of String class... string with the help of substring() function of String class
how to write a program to search a record using mvc2
how to write a program to search a record using mvc2   how can we write a code to search a record in table by using java bean as model, servlet as contoller and jsp as view
How to write a search functionality using javascript/jquery
How to write a search functionality using javascript/jquery   How to write a search functionality using javascript/jquery for ex: im searching "s" names it wil display "s" names
help with substring in c programing
. Please help with substring function,prompt the user for two strings and display whether or not the second string is a substring of the first string. Regards...help with substring in c programing  Hi I could not found programming
Breaking a string into words without using StringTokenizer
Breaking a string into words without using StringTokenizer  how can we Break a string into words without using StringTokenizer ??   The given code convert the string into words. import java.util.*; class StringExample
How to substring in JQuery?
How to substring in JQuery?  Hi, I my JQuery application I am getting the value from server using Ajax. I have to substring the value in jQuery.... Please let's know How to substring in JQuery? Thanks   Hi
find all unique character in string using java
find all unique character in string using java  example: hello how are you. output should be waryu

Ads