Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex

 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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....
 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic