aspose file tools*
The moose likes Java in General and the fly likes Regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex" Watch "Regex" New topic
Author

Regex

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 70
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 24, 2010
Posts: 70
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 26, 2011
Posts: 20
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....
Oscar G. Rodriguez
Greenhorn

Joined: Nov 05, 2011
Posts: 13
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 05, 2011
Posts: 13
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex
 
Similar Threads
regex question
Doubt on string tokenizer
Regex to split string on condition
Tokenising using String.split().
Tokenizing with regex pattern. Little confused!