5

I have two strings in a java program, which I want to mix in a certain way to form two new strings. To do this I have to pick up some constituent chars from each string and add them to form the new strings. I have a code like this(this.eka and this.toka are the original strings):

String muutettu1 = new String();
String muutettu2 = new String();
muutettu1 += this.toka.charAt(0) + this.toka.charAt(1) + this.eka.substring(2);
muutettu2 += this.eka.charAt(0) + this.eka.charAt(1) + this.toka.substring(2);
System.out.println(muutettu1 + " " + muutettu2);

I'm getting numbers for the .charAt(x) parts, so how do I convert the chars to string?

2
  • 1
    Nice to see some Finnish code ;-) Commented Oct 31, 2009 at 14:39
  • As your example shows, you can also use substring to return a String directly. Any reason why you are using both charAt and substring in the same statement? Commented Oct 31, 2009 at 14:39

5 Answers 5

15
StringBuilder builder = new StringBuilder();
builder
   .append(this.toka.charAt(0))
   .append(this.toka.charAt(1))
   .append(this.toka.charAt(2))
   .append(' ')  
   .append(this.eka.charAt(0))
   .append(this.eka.charAt(1))
   .append(this.eka.charAt(2));
System.out.println (builder.toString());
Sign up to request clarification or add additional context in comments.

5 Comments

I think, your result will be wrong. subString(2) is the string beginning with the index 2.
Yep, I missed that. Regardless, append is an overloaded method and you can pass it Strings as well. The salient point here was to not use String directly and use StringBuilder (or StringBuffer if on older JRE) to minimize the performance cost.
a + b will be compiled to new StringBuilder().append(a).append(b).toString() anyway.
I really like this answer, but why not an enhanced for loop? eg. for(String i : this.toka) and do the same for this.eka
Try javap on both variants. (Haven't done it myself, but) likely the iterative form will peform superflous allocations for the 2 control structures (possibly unrolled by the compiler). Also do note that OP's Q did not indicate that the source Strings actually terminate at the indices provided, so it will need to be an explicit for loop form.
7

Just use always use substring() instead of charAt()


In this particular case, the values are mutable, consequently, we can use the built in String class method substring() to solve this problem (@see the example below):

Example specific to the OP's use case:

    muutettu1 += toka.substring(0,1) + toka.substring(1,2) + eka.substring(2);
   
    muutettu2 += eka.substring(0,1) + eka.substring(1,2) + toka.substring(2);


Concept Example, (i.e Example showing the generalized approach to take when attempting to solve a problem using this concept)

    muutettu1 += toka.substring(x,x+1) + toka.substring(y,y+1) + eka.substring(z);

    muutettu2 += eka.substring(x,x+1) + eka.substring(y,y+1) + toka.substring(z);

"...Where x,y,z are the variables holding the positions from where to extract."

Comments

7

The obvious conversion method is Character.toString.

A better solution is:

String muutettu1 = toka.substring(0,2) + eka.substring(2);
String muutettu2 = eka.substring(0,2) + toka.substring(2);

You should create a method for this operation as it is redundant.

The string object instatiantiation new String() is unnecessary. When you append something to an empty string the result will be the appended content.

Comments

0

You can also convert an integer into a String representation in two ways: 1) String.valueOf(a) with a denoting an integer 2) Integer.toString(a)

Comments

-2

This thing can adding a chars to the end of a string

StringBuilder strBind = new StringBuilder("Abcd");
strBind.append('E');
System.out.println("string = " + str);
//Output => AbcdE
str.append('f');
//Output => AbcdEf

3 Comments

What is this? Where does str come from? What are you even doing?
What is this? you didn't even declare what str was yet!
You answered this question with JavaScript lol. This is a question that references JAVA, a totally different language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.