4

When splitting a simple array using Java, how can I reference the specific values without using println?

I have a string separated by "||" - I want to manipulate that string such that I can call each half of it and assign each bit to a new string. If this was php I'd use list() or explode(), but I can't seem to get the variables to work.

I want to

  1. output the contents of each half of the temp array to the screen and
  2. join the portions together as message = temp[0]+ "-"+ temp[1]; doesn't seem to work.
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
             getActionBar().setDisplayHomeAsUpEnabled(true);
           }
    Intent intent = getIntent();       
    String message = intent.getStringExtra(MainActivity.SENTMESSAGE);

    //The string is (correctly) submitted in the format foo||bar
    String delimiter = "||";
    String[] temp = message.split(delimiter);

    //??? How do I output temp[0] and temp[1] to the screen without using println?

    //This gives me a single character from one of the variables e.g. f-
    for(int i =0; i < temp.length ; i++)
    message = temp[0]+ "-"+ temp[1];

    //if I escape the above 2 lines this shows foo||bar to the eclipse screen
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView); 
}
3
  • "How do I output temp[0] and temp[1] to the screen without using println" seriously without using println? maybe use print, printf, JOptionPane.showMessageDialog, etc? Commented Jan 22, 2014 at 1:17
  • Eclipse said there was an error when I use System.out.println() as it is in a void class. Commented Jan 22, 2014 at 1:19
  • 1
    Why did you put it as single String instead of e.g. Intent#putStringArrayListExtra(..) Commented Jan 22, 2014 at 1:22

1 Answer 1

7

At fist glance it seems that your problem is here

String delimiter = "||";
String[] temp = message.split(delimiter);

because split uses regex as parameter and in regex | is special character representing OR. So with || you split on: empty String "" OR empty string "" OR empty String "".
Since empty String is always before each character and after character result of split such as "abc".split("||") will be ["", "a", "b", "c"] (last empty Strings by default removed are removed from result array).

To solve this problem you will have to escape |. You can do it by placing \ (which in Java needs to be written as "\\") before this metacharacter or you can just use Pattern.quote(regex) to escape all regex metacharacters for you. Try

String delimiter = "||";
String[] temp = message.split(Pattern.quote(delimiter));
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh. Thank you so much. I thought I was going mad.

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.