|
-
Nov 29th, 2004, 12:39 AM
#1
Thread Starter
Addicted Member
Recieving a string to search, then replace
I am creating a class called Editor that is able to perform a search/replace function on a document (a sentence or paragraph).
The Editor class includes two constructors:
• The first has no parameters
• The second has one parameter (the document to be searched)
The class includes a setDocument() and a getDocument() method.
The class includes two overloaded versions of a method called replace():
• The first has two parameters (search text, replacement text) and works on the current document.
• The second receives three parameters (the document to be searched, the search text, the replacement text).
• The replace method returns the new document as a String.
The replace method searches the document for instances of the search text, replaces that text with the replacement text.
Example:
document: “Rich is here”
search text: “is”
replacement text: “was”
result: “Rich was here”
What I need to know is how would I go about searching, then replacing the text in the string that I am searching?
Last edited by ddmeightball; Nov 29th, 2004 at 07:14 AM.
-
Nov 29th, 2004, 12:48 AM
#2
Thread Starter
Addicted Member
Here is my code
Code:
/**
* Title:Editor project
* @author Matthew Davis
* Class Info 1314
* Date: November 22, 2004
**/
package Editor;
import java.util.StringTokenizer;
public class Editor
{
String document;
String searchText;
String replacementText;
public Editor()
{
}//end constructor
public void setDocument(String recdocument)
{
document=recdocument;
}
public String getDocument()
{
return document;
}
public String replace(String searchText, String replacementText)
{
String [] token = new String[10];
StringTokenizer sTokens = new StringTokenizer(document, ",");
int count = 0;
while(sTokens.hasMoreTokens())
{
token[count++] = sTokens.nextToken().trim();
}
String resultStr = "";
for(int i = 0; i < count; i++)
{
if(token[i].equalsIgnoreCase(searchText))
{
resultStr = resultStr.replaceAll(token[i],replacementText);
}
else
{
resultStr += token[i] + ", ";
}
}
resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
return resultStr.toString();
}
public String replace(String document, String searchText, String replacementText)
{
String [] token = new String[10];
StringTokenizer sTokens = new StringTokenizer(document, ",");
int count = 0;
while(sTokens.hasMoreTokens())
{
token[count++] = sTokens.nextToken().trim();
}
String resultStr = "";
for(int i = 0; i < count; i++)
{
if(token[i].equalsIgnoreCase(searchText))
{
resultStr = resultStr.replaceAll(token[i],replacementText);
}
else
{
resultStr += token[i] + ", ";
}
}
resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
return resultStr.toString();
}
}
Last edited by ddmeightball; Nov 29th, 2004 at 03:12 PM.
-
Nov 29th, 2004, 03:44 AM
#3
Fanatic Member
search for RegEx or Regular Expression. Dunno this thing but it will help you. 
edit: Please use code tags when posting code. [code][/code]
-
Nov 29th, 2004, 09:06 AM
#4
Thread Starter
Addicted Member
-
Nov 29th, 2004, 12:06 PM
#5
Frenzied Member
Use the String types methods, look it up on the java docs.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Nov 29th, 2004, 12:38 PM
#6
Thread Starter
Addicted Member
Originally posted by vbNeo
Use the String types methods, look it up on the java docs.
what do you mean by string types methods and what are the java docs?
I am very new and java and just have been having some trouble even writing anything.
-
Nov 29th, 2004, 03:23 PM
#7
Dazed Member
-
Nov 30th, 2004, 09:19 AM
#8
Thread Starter
Addicted Member
Thank you everyone that helped me. I figured it out. My main problem was my delimiter on my string tokenizer. thanks again for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|