Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 808855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:37:55+00:00 2026-05-15T00:37:55+00:00

I like to convert string for example : String data = 1|apple,2|ball,3|cat; into a

  • 0

I like to convert string for example :

String data = "1|apple,2|ball,3|cat";

into a two dimensional array like this

{{1,apple},{2,ball},{3,cat}}

I have tried using the split("") method but still no solution 🙁

Thanks..

Kai

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:37:56+00:00Added an answer on May 15, 2026 at 12:37 am
        String data = "1|apple,2|ball,3|cat";
        String[] rows = data.split(",");
    
        String[][] matrix = new String[rows.length][]; 
        int r = 0;
        for (String row : rows) {
            matrix[r++] = row.split("\\|");
        }
    
        System.out.println(matrix[1][1]);
        // prints "ball"
    
        System.out.println(Arrays.deepToString(matrix));
        // prints "[[1, apple], [2, ball], [3, cat]]"
    

    Pretty straightforward except that String.split takes regex, so metacharacter | needs escaping.

    See also

    • Regular expressions and escaping special characters
    • Java Arrays.equals() returns false for two dimensional arrays.
      • Use Arrays.deepToString and Arrays.deepEquals for multidimensional arrays

    Alternative

    If you know how many rows and columns there will be, you can pre-allocate a String[][] and use a Scanner as follows:

        Scanner sc = new Scanner(data).useDelimiter("[,|]");
        final int M = 3;
        final int N = 2;
        String[][] matrix = new String[M][N];
        for (int r = 0; r < M; r++) {
            for (int c = 0; c < N; c++) {
                matrix[r][c] = sc.next();
            }
        }
        System.out.println(Arrays.deepToString(matrix));
        // prints "[[1, apple], [2, ball], [3, cat]]"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to convert the following string into an array/nested array and iterate
I want to convert a string like 2012-03-08T00:00:00+01:00 into Date.. I tried to use
So I have two custom complex types like this (oversimplified for this example): public
I would like to know how to convert a 2 dimensional array into a
How do I convert strings like this is an example to this-is-an-example under ruby?
I would like to convert this string foo_utf = u'nästy chäräctörs with å and
I'd like to convert a string of text, e.g., User Name into something which
I have double values which I would like to convert to String values with
I would like to convert a Dictionary to a string array (or list) The
I'd like to convert a money formatted string into a float in javascript. ex:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.