Solved

String Concat vs. +

Posted on 1998-11-15
1
334 Views
Last Modified: 2011-09-20

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\n");
            }
            else{
                processedString += workingString.charAt(i);
                // Or processedString.concat("" + workingString.charAt(i));
            }
        }
    }

Thanks for shedding any light on this!


ap9
0
Comment
Question by:ap9
1 Comment
 
LVL 4

Accepted Solution

by:
martinag earned 50 total points
ID: 1227491
The concat() function doesn't change the string's value, it just returns a new String object.

processedString = processedString.concat("" + workingString.charAt(i));
should work for you.

Martin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
MySQL  on Tomcat 8 70
servlet filter example 37 72
jboss 7.1 start up error 1 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question