Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I need some help with finding text in a string and replacing it?  (Read 791 times)
0 Members and 1 Guest are viewing this topic.
Offline DecafJava

Senior Newbie





« Posted 2007-11-23 21:18:31 »

I'm working on a program that needs to be able to search a string of text for certain words and then replace them with something else. I believe the best way (I'm probably wrong though Smiley) would be to compare the string to an ArrayList of words, and then replace the words with different ones. Does this sound like it would work? If it does how do I compare individual words inside the string to the words in the arrayList? Thanks.

I've got another quick question. When using the replace() method, how come I can't just do

1  
2  
object.text.replace("abc", "def");
System.out.println(object.text);


Whenever I do that it just gives me the same string that was stored in object.text. But when I do

1  
2  
object.text = object.text.replace("abc", "def");
System.out.println(object.text);


it works fine. Is that because the replace() method returns a string? Thanks.
Offline kaosnkorruption

Senior Newbie





« Reply #1 - Posted 2007-11-23 23:41:57 »

It creates a new String.

1  
2  
String str = object.text.replace("abc", "def");
System.out.prinlnt(str);
Offline DzzD
« Reply #2 - Posted 2007-11-24 05:42:08 »

first for better efficiency you may use http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html

may be you can use something like that ?


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
class MyString
{
 public String value;
 
 public MyString(String value)
 {
    this.value=value;
 }
 
 String replace(String a,String b)
 {
  this.value=this.value.replace(a,b);
  return this.value;
 }

 public String toString()
 {
  return this.value;
 }
}

class myObj
{
 MyString text;
 myObj(String value)
 {
  this.text=new MyString(value);
 }
}

myObj object=new myObj("abc");
object.text.replace("abc", "def");
System.out.println(object.text);

you cannot overide String as it is a final class but providing a toString method in a class will make you able to use it as a string :

this works:

1  
System.out.println("a real String" + object.text);


this works too:

1  
2  
3  
MyString s=new MyString("a fake string");
String b= "a real string " + s;
System.out.println(b);



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline malberts

Junior Duke





« Reply #3 - Posted 2007-11-24 08:30:44 »

If you want to go through a dictionary-like array and then replace all the words in the string with those in the dictionary you might want to do something like this:
1  
2  
3  
for(int x=0;x<dictionary.lenght;x++){
  obj.text = obj.text.replace(dictionary[x][0], dictionary[x][1]);
}

where 0 and 1 is just the word to find and what to replace it with respectively.

If you want to compare the words in the string to an array then in pre-1.5 JDK you could use StringTokenizer and from 1.5 you can use string.split(" ") to get an array containing all the words from your string. But don't use this if you only want to replace words, because then you'll have to reconstruct the string from the array.

PS: Download or check out the JDK docs then you can check out all the String manipulation methods.

In space no-one can hear you System.out.println()
Offline DecafJava

Senior Newbie





« Reply #4 - Posted 2007-11-26 05:03:54 »

Wow, thanks a lot, it makes much more sense now!  Grin One more question. I was planning on using this as a basic chat filter for a game. Is this the most efficient way to do it or are there better ones? Thanks.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!