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 6931789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:22+00:00 2026-05-27T11:37:22+00:00

There is a String temp = M0-1 How to convert this String to a

  • 0

There is a

String temp = "M0-1"

How to convert this String to a String[] ?

If you output the new String array, the result should be:

String[] newStringArray;
newStringArray[0] = M;
newStringArray[1] = 0;
newStringArray[2] = -1;

The last element of the new String array should be “-1”, which means “-1” will be considered as a entirety, but not “-” and “1” separately.

I tried to use String.split(“”) parse this string, but only got a String array like this:

String[] newStringArray;
newStringArray[0] = M;
newStringArray[1] = 0;
newStringArray[2] = -;
newStringArray[3] = 1;

UPDATE: String 56 should be [5, 6], and -10 should be [-1, 0]

  • 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-27T11:37:23+00:00Added an answer on May 27, 2026 at 11:37 am

    This split() will do it:

    String[] array = temp.split("(?<=[^-])");
    

    It works by splitting when the previous character is not a -

    That odd-looking regex is called a look-behind. Its syntax is (?<=regex), which is a non-capturing assertion that the preceding input matches the specified regex, which in this case is [^-], which means “any character that isn’t a minus”.

    There are 4 versions of this kind of assertion:

    • (?=regex) is a positive look ahead, meaning the next input must match regex
    • (?!regex) is a negative look ahead, meaning the next input must not match regex
    • (?<=regex) is a positive look behind, meaning the preceeding input must match regex
    • (?<!regex) is a negative look behind, meaning the nepreceedingxt input must not match regex

    Here’s a test:

    public static void main(String[] args) {
        String temp = "M0-1";
        String[] newStringArray = temp.split("(?<=[^-])");
        System.out.println(Arrays.toString(newStringArray));
    }
    

    Output:

    [M, 0, -1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to de/serialize an object without round-tripping a XmlDocument/temp string? I
So, I have a file called 'dummy' which contains the string: There is 100%
There is a string array myDownloadList containing 100 string URIs. I want to start
suppose there is a string of characters in an array of d[20] . How
There must be something obvious I don't realize about C++ with this one. load(string
My code counts spaces in string temp , which works fine. Later in the
Is there a string.Empty in JavaScript, or is it just a case of checking
Is there some string class in Python like StringBuilder in C#?
In C# there are String objects and string objects. What is the difference between
Is there a format string (?) I can pass to the VB6 Format function

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.