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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:11:02+00:00 2026-06-15T02:11:02+00:00

I have following code in my program. It splits a line when a hyphen

  • 0

I have following code in my program. It splits a line when a hyphen is encountered and stores each word in the String Array ‘tokens’. But I want the hyphen also to be stored in the String Array ‘tokens’ when it is encountered in a sentence.

String[] tokens = line.split("-");

The above code splits the sentence but also totally ignores the hyphen in the resulting array.
What can I do to store hyphen also in the resulting array?

  • 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-15T02:11:04+00:00Added an answer on June 15, 2026 at 2:11 am

    Edit : –

    Seems like you want to split on both whitespaces and hyphen but keeping only the hyphen in the array (As, I infer from your this line – stores each word in the String Array), you can use this: –

    String[] tokens = "abc this is-a hyphen def".split("((?<=-)|(?=-))|\\s+");
    System.out.println(Arrays.toString(tokens));
    

    Output: –

    [abc, this, is, -, a, hyphen, def]
    

    For handling spaces before and after hyphen, you can first trim those spaces using replaceAll method, and then do split: –

    "abc this is - a hyphen def".replaceAll("[ ]*-[ ]*", "-")
                                .split("((?<=-)|(?=-))|\\s+");
    

    Previous answer : –

    You can use this: –

    String[] tokens = "abc-efg".split("((?<=-)|(?=-))");
    System.out.println(Arrays.toString(tokens));
    

    OUTPUT : –

    [abc, -, efg]
    

    It splits on an empty character before and after the hyphen (-).

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

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
i have written the following code class Program { static void Main(string[] args) {
I have the following code $env=array('PATH'=>'C:\Program Files\MySQL\MySQL Server 5.1\bin', 'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'); $cmd='mysql --port=3306
I have the following code while(((line = in.readLine()) != null)) { String[] val =
I have the following code: class Program { static void Main(string[] args) { using
I have the following code that splits a string on newlines and converts it
Assume that we have the following code: class Program { static volatile bool flag1;
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
I have the following code in my C# program. DateTime dateForButton = DateTime.Now; dateForButton
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]

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.