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

  • Home
  • SEARCH
  • 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 7991407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:16:54+00:00 2026-06-04T13:16:54+00:00

I have following String and i want to split this string into number of

  • 0

I have following String and i want to split this string into number of sub strings(by taking ‘,’ as a delimeter) when its length reaches 36. Its not exactly splitting on 36’th position

      String message = "This is some(sampletext), and has to be splited properly";

I want to get the output as two substrings follows:
1. ‘This is some (sampletext)’
2. ‘and has to be splited properly’

Thanks in advance.

  • 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-04T13:16:56+00:00Added an answer on June 4, 2026 at 1:16 pm

    A solution based on regex:

        String s = "This is some sample text and has to be splited properly";
        Pattern splitPattern = Pattern.compile(".{1,15}\\b");
        Matcher m = splitPattern.matcher(s);
        List<String> stringList = new ArrayList<String>();
        while (m.find()) {
            stringList.add(m.group(0).trim());
        }
    

    Update:
    trim() can be droped by changing the pattern to end in space or end of string:

        String s = "This is some sample text and has to be splited properly";
        Pattern splitPattern = Pattern.compile("(.{1,15})\\b( |$)");
        Matcher m = splitPattern.matcher(s);
        List<String> stringList = new ArrayList<String>();
        while (m.find()) {
            stringList.add(m.group(1));
        }
    

    group(1) means that I only need the first part of the pattern (.{1,15}) as output.

    .{1,15} – a sequence of any characters (“.”) with any length between 1 and 15 ({1,15})

    \b – a word break (a non-character before of after any word)

    ( |$) – space or end of string

    In addition I’ve added () surrounding .{1,15} so I can use it as a whole group (m.group(1)).
    Depending on the desired result, this expression can be tweaked.

    Update:
    If you want to split message by comma only if it’s length would be over 36, try the following expression:

    Pattern splitPattern = Pattern.compile("(.{1,36})\\b(,|$)");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following String: (X,Y,Z),(A,B,C),(R,S,T) I want to split this into a multi-dimensional
I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following
Have the following string i need to split. $string = This is string sample
I have the following string 3/4Ton. I want to split it as --> word[1]
I have the following string: Mr John Smith Dickson <john@yahoo.com> I want to split
I have the following string: (1-$500.00)(2-$20.00) I want to turn it into an array
I want to split the following strings into two substrings. somestring(otherstring) I came with
I have a string like the following. ids = 'S1486S1485E444' I want to split
I have a following string and I want to extract image123.jpg. ..here_can_be_any_length and_here_any_length/image123.jpg and_here_also_any_length
I have the following string from which I want to extract gcc-4.3.2 and C

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.