1

I want to check a string for each character I replace it with other characters or keep it in the string. and also because it's a long string the time to do this task is so important. what is the best way of these, or any better idea? for all of them I append the result to an StringBuilder. check all of the characters with a for and charAt commands. use switch like the previous way. use replaceAll twice.

and if one of the first to methods is better is there any way to check a character with a group of characters, like : if (st.charAt(i)=='a'..'z') ....

Edit: please tell the less consuming in time way and tell the reason.I know all of these ways you said!

5 Answers5

1

Check the documentation and find some good methods:

char from = 'a';
char to = 'b';
str = str.replace(from, to);
jlordo
  • 37,490
  • 6
  • 58
  • 83
1
String replaceSample = "This String replace Example shows 
how to replace one char from String";
String newString = replaceSample.replace('r', 't');

Output: This Stting teplace Example shows how to teplace one chat ftom Stting

Also, you could use contains:

 str1.toLowerCase().contains(str2.toLowerCase())

To check if the substring str2 exists in str1

Edit.

Just read that the String come from a file. You can use Regex for this. That would be the best method.

http://docs.oracle.com/javase/tutorial/essential/regex/literals.html

aran
  • 10,978
  • 5
  • 39
  • 69
  • read the question once again. I know all of the ways but looking for the best one! –  Apr 25 '13 at 21:24
  • Okay, this is the best one. 0: ) – aran Apr 25 '13 at 21:25
  • better than surveying the String char by char?why? –  Apr 25 '13 at 21:26
  • Because if you have an API, and you do for this, it's always a better idea to use it than to create a new method by yourself. Don't make what it's already made. And if it's made by the guys that create and maintain Java, trust in their methods. – aran Apr 25 '13 at 21:27
  • what if I have to use it twice and it surveys my long String twice, but if I write it just one time I survey. –  Apr 25 '13 at 21:29
  • Could you explain yourself a little "deeper" please. – aran Apr 25 '13 at 21:30
  • I want to replace all of the uppercases to lower cases and replace all of the characters except a-z with space.Is it enough? –  Apr 25 '13 at 21:34
  • With Regex you can find substring parseing the file in the best possible computing time. Then you can replace the substrings with the code I gave you. Take a look: http://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclu – aran Apr 25 '13 at 21:35
1

This is your comment:

I want to replace all of the uppercases to lower cases and replace all of the characters except a-z with space.

You can do it like this:

str = str.toLowerCase().replaceAll("[^a-z]", " ");

Your requirement should be part of the question, not in comment #7 under a posted answer...

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • this is not my question. my important question is the best way! i mentioned all of these ways in the question and asked the best one: "what is the best way of these, or any better idea? for all of them I append the result to an StringBuilder. check all of the characters with a for and charAt commands. use switch like the previous way. use replaceAll twice." –  Apr 25 '13 at 21:51
  • @user2273552: Are you using a profiler? Is this part of the code your bottleneck? I'd say it won't get any faster than the code I posted. I bet you lose much more time for I/O than the string manipulation. Also, what is a long string in your case? Length a million, billion, trillion, longer?? – jlordo Apr 25 '13 at 21:54
  • yes. surely other part consume time more but I want to decreas this part too.it's a 1.5G .txt file that I read part by part and so on. thanx any how –  Apr 25 '13 at 22:04
  • @Paniz: Does this have to be done in java? this can be done in one line in a shell, and it'll be ultra-fast. – jlordo Apr 25 '13 at 22:06
  • it's a part of a big program. how should I concat them if write to different palces? –  Apr 25 '13 at 22:08
  • with the `cat` command. Seems to me your program can be replaced by a few line short shell script and be much much faster. – jlordo Apr 25 '13 at 22:10
1

If you want to replace a single character (or a single sequence), use replace(), as other answers have suggested.

If you want to replace several characters (e.g., 'a', 'b', and 'c') with a single substitute character or character sequence (e.g., "X"), you should use a regular expression replace:

String result = original.replaceAll("[abc]", "X");

If you want to replace several characters, each with a different replacement (e.g., 'a' with 'A', 'b' with 'B'), then looping through the string yourself and building the result in a StringBuilder will probably be the most efficient. This is because, as you point out in your question, you will be going through the string only once.

String sb = new StringBuilder();
String targets = "abc";
String replacements = "ABC";
for (int i = 0; i < result.length; ++i) {
    char c = original.charAt(i);
    int loc = targets.indexOf(c);
    sb.append(loc >= 0 ? replacements.charAt(loc) : c);
}
String result = sb.toString();
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

You should look into regex for Java. You can match an entire set of characters. Strings have several functions: replace, replaceAll, and match, which you may find useful here.

You can match the set of alphanumeric, for instance, using [a-zA-Z], which may be what you're looking for.