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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:00:38+00:00 2026-06-04T08:00:38+00:00

I have the following string: A:B:1111;domain:80;a;b The A is optional so B:1111;domain:80;a;b is also

  • 0

I have the following string:
A:B:1111;domain:80;a;b
The A is optional so B:1111;domain:80;a;b is also valid input.
The :80 is optional as well so B:1111;domain;a;b or :1111;domain;a;b are also valid input
What I want is to end up with a String[] that has:

s[0] = "A";  
s[1] = "B";  
s[2] = "1111";  
s[3] = "domain:80"  
s[4] = "a"  
s[5] = "b"  

I did this as follows:

List<String> tokens = new ArrayList<String>();  
String[] values = s.split(";");  
String[] actions = values[0].split(":");   

for(String a:actions){  
    tokens.add(a);  
}  
//Start from 1 to skip A:B:1111
for(int i = 1; i < values.length; i++){  
    tokens.add(values[i]);  
}  
String[] finalResult = tokens.toArray();

I was wondering is there a better way to do this? How else could I do this more efficiently?

  • 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-06-04T08:00:40+00:00Added an answer on June 4, 2026 at 8:00 am

    There are not many efficiency concerns here, all I see is linear.

    Anyway, you could either use a regular expression or a manual tokenizer.

    You can avoid the list. You know the length of values and actions, so you can do

    String[] values = s.split(";");  
    String[] actions = values[0].split(":");
    String[] result = new String[actions.length + values.length - 1];
    System.arraycopy(actions, 0, result, 0, actions.legnth);
    System.arraycopy(values, 1, result, actions.length, values.length - 1);
    return result;
    

    It should be reasonably efficient, unless you insist on implementing split yourself.

    Untested low-level approach (make sure to unit test and benchmark before use):

    // Separator characters, as char, not string.
    final static int s1 = ':';
    final static int s2 = ';';
    // Compute required size:
    int components = 1;
    for(int p = Math.min(s.indexOf(s1), s.indexOf(s2));
      p < s.length() && p > -1;
      p = s.indexOf(s2, p+1)) {
        components++;
    }
    String[] result = new String[components];
    // Build result
    int in=0, i=0, out=Math.min(s.indexOf(s1), s.indexOf(s2));
    while(out < s.length() && out > -1) {
      result[i] = s.substring(in, out);
      i++;
      in = out + 1;
      out = s.indexOf(s2, in);
    }
    assert(i == result.length - 1);
    result[i] = s.substring(in, s.length());
    return result;
    

    Note: this code is optimized in the crazy way of that it will consider a : only in the first component. Handling the last component is a bit tricky, as out will have the value -1.

    I would usually not use this last approach, unless performance and memory is extremely crucial. Most likely there are still some bugs in it, and the code is fairly unreadable, in particulare compare to the one above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following string task BLABLA@{taskId} @{BLABLA.title} and want to extract all placeholders from
I have following example string that needs to be filtered 0173556677 (Alice), 017545454 (Bob)
I have following string: xxxxx GL=>G0 yyyyyy I want to extract GL and G0
I have following string hello[code:1], world [code:2], hello world I want to replace this
I have following string, I want to convert it to DataTable Id,Name ,Dept\r\n1,Mike,IT\r\n2,Joe,HR\r\n3,Peter,IT\r\n I
I have following String that I have put together: v1fColor = '2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3' I am
I have the following json string and I want to retrieve just the email
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
I have the following test case that I want to post a piece of
I have following string 3.14, 123.56f, .123e5f, 123D, 1234, 343E12, 32. What I want

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.