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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:43:39+00:00 2026-06-04T16:43:39+00:00

I need to split Java Strings at any character. The main thing is, the

  • 0

I need to split Java Strings at any ” character.
The main thing is, the previous character to that may not be a backslash ( \ ).

So these Strings would split like so:

asdnaoe"asduwd"adfdgb         =>   asdnaoe, asduwd, adfgfb
addfgmmnp"fd asd\"das"fsfk    =>   addfgmmnp, fd asd\"das, fsfk

Is there any easy way to achieve this using regular expressions?
(I use RegEx because it is easiest for me, the coder. Also performance is not an issue…)

Thank you in advance.

I solved it like this:

    private static String[] split(String s) {
    char[] cs = s.toCharArray();

    int n = 1;

    for (int i = 0; i < cs.length; i++) {
        if (cs[i] == '"') {
            int sn = 0;

            for (int j = i - 1; j >= 0; j--) {
                if (cs[j] == '\\')
                    sn += 1;
                else
                    break;
            }

            if (sn % 2 == 0)
                n += 1;
        }
    }

    String[] result = new String[n];

    int lastBreakPos = 0;
    int index = 0;
    for (int i = 0; i < cs.length; i++) {
        if (cs[i] == '"') {
            int sn = 0;

            for (int j = i - 1; j >= 0; j--) {
                if (cs[j] == '\\')
                    sn += 1;
                else
                    break;
            }

            if (sn % 2 == 0) {
                char[] splitcs = new char[i - lastBreakPos];

                System.arraycopy(cs, lastBreakPos, splitcs, 0, i - lastBreakPos);
                lastBreakPos = i + 1;

                result[index] = new StringBuilder().append(splitcs).toString();
                index += 1;
            }
        }
    }

    char[] splitcs = new char[cs.length - (lastBreakPos + 1)];

    System.arraycopy(cs, lastBreakPos, splitcs, 0, cs.length - (lastBreakPos + 1));

    result[index] = new StringBuilder().append(splitcs).toString();

    return result;
}

Anyways, thanks for all your great responses!
(Oh, and despite this, I will be using either @biziclop’s or @Alan Moore’s version, as they
‘re shorter and probably more efficient! =)

  • 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-04T16:43:40+00:00Added an answer on June 4, 2026 at 4:43 pm

    Sure, just use

    (?<!\\)"
    

    Quick PowerShell test:

    PS> 'addfgmmnp"fd asd\"das"fsfk' -split '(?<!\\)"'
    addfgmmnp
    fd asd\"das
    fsfk
    

    However, this won’t split on \\" (an escaped backslash, followed by a normal quote [at least in most C-like languages’ escaping rules]). You cannot really solve that in Java, though, as arbitrary-length lookbehind isn’t supported:

    PS> 'addfgmmnp"fd asd\\"das"fsfk' -split '(?<!\\)"'
    addfgmmnp
    fd asd\\"das
    fsfk
    

    Usually you would expect a proper solution to split on the remaining " because it isn’t really escaped.

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

Sidebar

Related Questions

I have a string in Java that I need to split using <$ and
What regex pattern would need I to pass to java.lang.String.split() to split a String
I need to split a string (in Java) into individual words ... but I
I need to split a string that is going to be the input of
I'm actually having some strings that have some data separated by a pipe character,
I need to split a string on any of the following sequences: 1 or
I need to split a line of assembly code using split() method in Java.
I need split string by commas and spaces, but ignore the inside quotes, single
I need to split an flv file into chunks of the known size on
I need to split my string input into an array at the commas. Is

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.