1

I have a char array from string:

public class CharIndexes {
    public static void main (String[] args) {
        String a = "A good example is the best sermon.";
        int len = a.length();
        char[] tempA = new char[len] ;
        for (int i = 0; i < len; i++) {
             tempA[i] = a.charAt(i);
        }
}

I need to create 2 strings: a1 from elements [3],[0],[28] and a2 from: [15],[24] and to get a string equal to 'oasis' (i.e. 3,0,28 elements + 15,24):

System.out.println(a1.concat(a2));

What can I do?

3
  • tempA[i] =3D - What.. is.. this? Commented Feb 27, 2014 at 12:27
  • tempA[i] = a.charAt(i); ? Commented Feb 27, 2014 at 12:27
  • 1
    Interestingly enough, the choice of characters prints "oAeis" and not "oasis". Commented Feb 27, 2014 at 12:38

5 Answers 5

1

Maybe something like this...?

String a = "A good example is the best sermon.";
char[] c = a.toCharArray();

String a1 = new String(new char[]{c[3],c[0],c[28]});
String a2 = new String(new char[]{c[15],c[24]});

System.out.println(a1+a2);
Sign up to request clarification or add additional context in comments.

4 Comments

You should use StringBuilder for performance issues when you are programatically handling string elements and modifying its contents.
a is a string, you defenetely mean the char array tempA.
@BorisBrodski agreed, but thought it'd be better to just use toCharArray() rather than building tempA in a for loop - I saw this as a way to improve on the OPs code.
@WATTOStudios no more comments after your last edit :)
1

You can create an array of chars like this

char [] a1 = new char[] {tempA[3], tempA[0], tempA[28]};
char [] a2 = new char[] {tempA[15], temp[24]};

Then you can build your target string as simple as this

System.out.println(new String(a1) + new String(a2));

Comments

1
String a = new StringBuffer().append(a.charAt(...)).append(...).toString();
String b = new StringBuffer().append(a.charAt(...)).append(...).toString();
System.out(a.concat(b));

2 Comments

Are other solutions more readable? The problem to solve is pretty particular...you can go newline if you don't want a line too long
fair call :) I revoke my comment.
0

Here's how oasis would get formed:

String string = "A good example is the best sermon.";
char[] chars = string.toCharArray();

String firstPart = new String(new char[] { chars[3], chars[0], chars[28] });
String secondPart = new String(new char[] { chars[15], chars[24] });

String oasis = firstPart.concat(secondPart);

Or,

String string = "A good example is the best sermon.";
char[] chars = string.toCharArray();

StringBuilder firstPart = new StringBuilder().append(chars[3]).append(chars[0]).append(chars[28]);
StringBuilder secondPart = new StringBuilder().append(chars[15]).append(chars[24]);

String oasis = firstPart.append(secondPart).toString();

2 Comments

String dnt have append method,switch to stringBuffer or use concat method
Typo. Concat. Changed it.
0
public class CharIndexes 
{
    public static void main (String[] args) 
    {
        String a = "A good example is the best sermon.";
        int len = a.length();
        char[] tempA = new char[len] ;
        for (int i = 0; i < len; i++) 
        {
             tempA[i] = a.charAt(i);
        }
        String a1 = new String (tempA[3]+""+tempA[9]+""+tempA[27]);
        String a2 = new String (tempA[15]+""+tempA[24]);
        System.out.print(a1+a2);
    }
}

Comments

Your Answer

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