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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:41:59+00:00 2026-06-13T15:41:59+00:00

I would like to split string into List on two possible delimeters / or

  • 0

I would like to split string into List on two possible delimeters “/” or “//”. But what is more the delimeter should also be put into the same List. I can not do this with Splitter in Guava or java.util.Scanner.

Scanner s = new Scanner(str);
s.useDelimiter("//|/");
while (s.hasNext()) {
    System.out.println(s.delimiter());
    System.out.println(s.next());
}

s.delimiter() returns //|/. I want to get / or //.

Do you know any other library which can do this?

I wrote some code, and it works but it is not very nice solution:

public static ArrayList<String> processString(String s) {
    ArrayList<String> stringList = new ArrayList<String>();
    String word = "";
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '/' && i < s.length() && s.charAt(i + 1) == '/') {
            if (!word.equals(""))
                stringList.add(word);
            stringList.add("//");
            word = "";
            i++;
        } else if (s.charAt(i) == '/') {
            if (!word.equals(""))
                stringList.add(word);
            stringList.add("/");
            word = "";
        }else{
            word = word + String.valueOf(s.charAt(i));
        }
    }
    stringList.add(word);
    return stringList;
}

On "some/string//with/slash/or//two" returns List with some, /, string, //, with, /, slash, /, or, //, two

On "/some/string//with/slash/or//two" returns List with /, some, /, string, //, with, /, slash, /, or, //, two

On "//some/string//with/slash/or//two" returns List with //, some, /, string, //, with, /, slash, /, or, //, two

  • 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-13T15:42:01+00:00Added an answer on June 13, 2026 at 3:42 pm

    The useDelimiter method has a signature that takes a Pattern object, instead of a String.

    You should use that one instead:

    Scanner s = new Scanner(str);
    s.useDelimiter(Pattern.compile("/{1,2}"));
    while (s.hasNext()) {
        System.out.println(s.delimiter());
        System.out.println(s.next());
    }
    

    In order to capture the delimiter, you’re going to need to change your approach.

    Pattern p = new Pattern("(/{0,2})([^/]+)");
    Matcher m = p.matcher(str);
    while(m.find()) {
       String token     = m.group(2);
       String delimiter = m.group(1); // (preceding delimiter. may be null)
       /*
        * ...
        */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list (string) of data that I would like to split into
I would like to be able to split a string into several parts if
I need to split a string into a list of parts in Ruby, but
I would like to split the following string generated from a database resultset row.
I would like to split a line into words. I know this can be
I have a huge text file that I would like to split into multiple
I have a dataframe with one column that I would like to split into
I'm trying to split a string: 'QH QD JC KD JS' into a list
Possible Duplicate: How can I split an IEnumerable<String> into groups of IEnumerable<string> I have
Problem: split a string into a list of words by a delimiter characters passed

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.