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 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.









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
string and substring
string and substring  bbbnnnmmm*ccc remove second asterick and display it as bbb*nnnADS_TO_REPLACE_1 mmm*ccc
Advertisements
string and substring
string and substring  my code is bbbnnnmmm*ccc how to remove the asterick at second position and display the code as bbb*nnn mmm*ccc
remove a substring from a string
remove a substring from a string  Please I jave an arraylist composed as follwoing [w1, w2, w3, w4, w1.w2.w3, w2.w3.w4, w3.w4, w2.w4, w1.w3, w1.w2] w2.w4 is a subset of w2.w3.w4 ?how I recognize it also ADS_TO_REPLACE_1 w1.w3
check if a substring exists in a string
check if a substring exists in a string  Please I jave an arraylist composed as follwoing [w1, w2, w3, w4, w1.w2.w3, w2.w3.w4, w3.w4, w2.w4, w1.w3, w1.w2] w2.w4 is a subset of w2.w3.w4 ?how I recognize it also ADS
String substring method example
.style1 { font-size: medium; } String substring() method example... a string. String class provide methods for finding substring from string. Method...;; var substring:String = str.substring(5,13);ADS_TO_REPLACE_4
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
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
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... In the given code, we have consider will as str2 and the whole string as str1
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
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() methodADS... is String which is a substring of the current String. This method is written
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
PHP Array Sub String Search
PHP Array Search Sub-String In PHP if we need to search a sub-string within..., otherwise false PHP Search Array Substring Example 1: <?php...,$array1,true))     echo "The string: \"".$search
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
string palindrom using vbscript?
string palindrom using vbscript?  string palindrom using vbscript
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:ADS
SubString in Java
SubString(), a method of String class is used to get a part of original string... then it will return empty String. Example of SubString in Java: import java.io.... on the console System.out.println(strLine); String subString = str.substring(0
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   
Substring in java
Substring in java   example:output 0f the code.. Input String:University of the Cordilleras Input Substring:of Output:1 ...use non static methods,don't use (.Substring).. ..please help me.. ..thank you.. deadline:November
Substring in java
Substring in java   example:output 0f the code.. Input String:University of the Cordilleras Input Substring:of Output:1 ...use non static methods,don't use (.Substring).. ..please help me.. ..thank you.. deadline:November
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
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
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
read string - using loops in Java
read string - using loops in Java  Write a program to read a string composed of an unknown number of words, then count the number of words in the string, and Display the longest and shortest words, with first letter Uppercase
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
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
Interchanging String using RegularExpression
a String and interchange the index of string using Regularexpression. The steps involved in interchanging a String are described below: String parg = "For some... Interchanging String using RegularExpression
String indexOf method example
of the substring from the string. The indexOf() method start to search from beginning of the string and matching the substring from substring, if substring... .style1 { font-size: medium; } String indexOf() method example
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
Replacing String using Regularexpression
Replacing String using Regularexpression       This Example describe the way to replace a String using Regularexpression. The steps involved in replacing a String are described
Reverse String using Stack
Reverse String using Stack You all are aware of Stack, a data structure... the string using these operations. Here is the code: import java.util.*; class StackReverseString { public static String revString(String str) { Stack
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
string. You will learn how to use the substring() method of String class... are creating a new sub string with the help of substring() function of String class...): This method is used to find all sub string after index i. substring
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>
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
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
Use of <fn:subString(String, int, int)> Tag of JSTL
Use of <fn:subString(String, int, int)> Tag of JSTL...="${fn:substring(string,start,end)}"/> </font>... of a string of specified length. This takes string and integer type as arguments
create a string from an array using JavaScript
create a string from an array using JavaScript  How to use "join()" to create a string from an array using JavaScript
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
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
search functionality using jsp from database
search functionality using jsp from database  search functionality using jsp from database
Java String using STACK - Java Beginners
Java String using STACK  Can you give me a code using Java String STACK using the parenthesis symbol ( ) it tells its Valid or Invalid it tells... the stack sample output Enter a string: ()() VALID Enter a string
How to split string in Java using comma?
How to split string in Java using comma?  Hi, Share the example code for splitting the string in Java using comma as separator. Thanks   Hi, You can use the split() function of the String class to split the string
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import java.util.*; class CountWords{ public static void main(String[] args
String length without using length() method in java
String length without using length() method in java  How to count length of string without using length() method in java?   Here is Example... the number of character into the given string without using the length() method
Program using String functions - Java Beginners
Program using String functions  Write a program to accept a sentence... { public static void main(String[] args) { int count = 0...); System.out.print("Enter string:"); String st = input.nextLine
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