I would like to combine a string of words for eg. aero-plane To do that , I would do a if else to check for the string "-" and if it is valid in the words it would then combine both aero and plane together to form aeroplane. I know how to do the check portion but I don't really know how to concatenate them together. I am using the java stringtokenizer api to do the following.
Asked
Active
Viewed 1,900 times
-4
-
1Did you even google this question ? – Dici Oct 12 '14 at 08:16
-
Probably didn't know what to search because it is an XY question. – nmore Oct 12 '14 at 08:18
-
From the [Javadoc for `StringTokenizer`](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html): _StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code_. Don't use `StringTokenizer`. – Boris the Spider Oct 12 '14 at 08:40
2 Answers
0
You can invoke the replace method on java strings to replace the target string (in your case a dash) with a blank string:
String dash = "aero-plane";
String noDash = dash.replace("-", "");
System.out.println(noDash);
prints aeroplane
There is no reason to use StringTokenizer here unless you are getting these tokens from outside of the code that you control, which doesn't seem to be the case.

nmore
- 2,474
- 1
- 14
- 21
-
-
-
You don't need to join anything, replace will give you the complete string back to you. – nmore Oct 12 '14 at 08:20
-
this answer has been flagged as a low quality answer, please provide some explanation :) – Kick Buttowski Oct 12 '14 at 08:22
-
Sure, added some more code and link to the javadoc of method in question. – nmore Oct 12 '14 at 08:28
-
`replaceAll` is the wrong method here, `replace` is the correct one. Using `replaceAll`, which takes **regex** input, shows a fundamental misunderstanding of the `String` API. – Boris the Spider Oct 12 '14 at 08:41
-
@BoristheSpider you are right; I had it as replace before and then doubted myself due to a previous comment to change it to replaceAll. Should have confirmed it again on my own. – nmore Oct 12 '14 at 08:50
0
If you are tokenizing the string using StringTokenizer in this way then this might help or else provide more information to get better help
String str="aero-plane";
StringTokenizer tr= new StringTokenizer(str,"-");
StringBuilder br = new StringBuilder();
while(tr.hasMoreTokens()){
String token = tr.nextToken();
br.append(token);
}
System.out.println(br.toString());

SparkOn
- 8,806
- 4
- 29
- 34