Solved

String Concat vs. +

Posted on 1998-11-15
1
330 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now