Reference to values ​​from an array in java

When splitting a simple array using Java, how can I refer to specific values ​​without using println?

I have a line divided by "||"- I want to manipulate this line so that I can call each half of it and assign each bit to a new line. If it is php, I would use list () or explode (), but I cannot get the variables to work.

I want

  • displays the contents of each half of the temp array on the screen and
  • put the pieces together as message = temp[0]+ "-"+ temp[1];it 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); 
}
+4
source share
1 answer

At first glance it seems that your problem is here

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

split regex , regex | - , OR. , || : empty String "" "" "".
, "abc".split("||"), ["", "a", "b", "c"] ( ).

, |. , \ ( Java "\\") , Pattern.quote(regex), regex .

String delimiter = "||";
String[] temp = message.split(Pattern.quote(delimiter));
+7

All Articles