• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please I'm trying to split a string into characters to, be stored in an array. But I don't know which delimiter I'm to use. I've been trying including spaces so that \\s splits it, but that's not exactly what I want.

import java.util.*;
class MyClass {
public static void main(String[] args) {
String[] token = args[0].split("\\s");
String a = token[0] + token[1] + token[2];
System.out.print (a);
}
}


Thanks in advance.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just want all the chars from a String in a char[], then String has getChars() or toCharArray() or something like that.

If that's not what you want, you'll have to TellTheDetails(⇐click) so that people here can understand what you're trying to do.
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, what I'm saying is that: I key in a string and I get characters in an array. You see, something like this. Split the word "SAMSON" into individual characters. So in my array I get 'S' 'A' 'M' 'S' 'O' 'N' . I tried string.split() and the delimiter was a "\\s". So what I mean in short is that, instead of spacing SAMSON at the command line, isn't there any REGULAR EXPRESSION i can use. Thanks in advance for your time!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shamsudeen Akanbi wrote:Hey, what I'm saying is that: I key in a string and I get characters in an array. You see, something like this. Split the word "SAMSON" into individual characters. So in my array I get 'S' 'A' 'M' 'S' 'O' 'N' .



So forget split() and regex and just use the String method that gives you back the char[]. Calling split() will give you a String[] anyway, not a char[].

I tried string.split() and the delimiter was a "\\s"



So you tried splitting on whitespace. I don't see any whitespace in the String "SAMSON".

. So what I mean in short is that, instead of spacing SAMSON at the command line,



What does the command line have to do with anything? Where did that suddenly come from?

isn't there any REGULAR EXPRESSION i can use.



Yes, but so far, you haven't shown any need for a regex.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shamsudeen Akanbi wrote:Please I'm trying to split a string into characters to, be stored in an array. But I don't know which delimiter I'm to use. I've been trying including spaces so that \\s splits it, but that's not exactly what I want.

import java.util.*;
class MyClass {
public static void main(String[] args) {
String[] token = args[0].split("\\s");
String a = token[0] + token[1] + token[2];
System.out.print (a);
}
}


Thanks in advance.


Can you specify your exact requirement or concern?

I guess you are trying to split the command line argument i.e the string at each occurrence of spaces,
So I guess this should be good enough if the string is like "My name is Shamsudeen Akanbi"


And for just conversion of string to character arrays, the String methods like toCharArray() would do the job.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chetan Sarnad wrote:
I guess you are trying to split the command line argument i.e the string at each occurrence of spaces,
So I guess this should be good enough if the string is like "My name is Shamsudeen Akanbi"



He's actually stated quite explicitly that that's NOT what he wants.
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

He's actually stated quite explicitly that that's NOT what he wants.



oops..Apologies
I missed the latest posts by the time i posted and edited my reply....
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another one that thinks you have to use toCharArray(), but if you don't want to, you can try with

String [] p=args[0].split("");



You'll obtain the String[], but the first element will be "".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oscar G. Rodriguez wrote:Another one that thinks you have to use toCharArray(), but if you don't want to, you can try with

String [] p=args[0].split("");



You'll obtain the String[], but the first element will be "".



Nobody said he "has to" use toCharArrary(), just that it's a simpler alternative to regex for what he's asking. And if he's going to use regex, that would not be the one to use.
 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know if I wrote what I want to said, my English isn't very good.

I think that he ought to use toCharArray(), another one was me because I understood that you said the same.

Sorry for the misunderstanding.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oscar G. Rodriguez wrote:Well, I don't know if I wrote what I want to said, my English isn't very good.

I think that he ought to use toCharArray(), another one was me because I understood that you said the same.

Sorry for the misunderstanding.



No worries.
 
reply
    Bookmark Topic Watch Topic
  • New Topic