ap9
asked on
String Concat vs. +
What is the difference between the concat() method in String and the '+' operator for the String? I thought they were the same, but when I try to "build" a string by concatening (using the concat method) strings to an initially empty string, I appear to get nothing, whereas if I do it via '+' (or in this case '+=') it turns out ok.
Here is the sample code:
String processedString = "";
String workingString = myTextArea.getText();
int stringLength = workingString.length();
for (int i = 0; i < stringLength; i++){
if (workingString.charAt(i) == '\n'){
processedString += "\r\n";
// Or processedString.concat("\r
}
else{
processedString += workingString.charAt(i);
// Or processedString.concat("" + workingString.charAt(i));
}
}
}
Thanks for shedding any light on this!
ap9
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.