Extracting characters and words from a string
I want to scan an input line character by character and produce Strings based on valid tokens which are “true”, “false”, “^” “&”, “!”, “(”, “)”
For example if i was given a string such as String line = true & ! (false ^ true)
String line = true & ! (false ^ true)
I would have to produce the tokens "true", "&", "!", "(", "false", "^", "true", ")"
I have been trying to use split() to divide the string into tokens and store them in an array like this String[] result = line.split(" ")
, and then just using a bunch of if-statements inside a loop to see if the token at that index matches any of the valid tokens and just returning the token. this is kind of what i have been trying to use so far
for(int i = 0; i < line.length();i++){
if(result[i].equals("true") || result[i].equals("false") || result[i].equals("^")
|| result[i].equals("&") || result[i].equals("!") || result[i].equals("(")
|| result[i].equals(")")){
nextToken = result[i];
}
but obviously this wont extract valid tokens that are adjacent to one another, such as when the string contains something like this (true
or this true^false
, which should return three tokens being "true", "^", "false"
. Is there a way to divide a string that doesn't contain spaces or any special characters into tokens i am interested in?
So long as the input is accurate, the following will tokenize your input:
public class Tokenizer {
public static void main(String[] args) {
// true, false, ^ &, !, (, )
String SYMBOLS = "^&!()";
String line = "true&!(false^true)";
List<String> tokens = new ArrayList<String>();
char[] in = line.toCharArray();
for (int i = 0; i<in.length; i++) {
if (in[i] == ' ')
continue;
if (SYMBOLS.indexOf(in[i]) >= 0) {
tokens.add(String.valueOf(in[i]));
} else if (in[i] == 't') {
tokens.add("true");
i += "true".length()-1;
} else if (in[i] == 'f') {
tokens.add("false");
i += "false".length()-1;
}
}
for (String token : tokens)
System.out.println(token);
}
}
Producing output:
true
&
!
(
false
^
true
)
Try using delimiters. They will separate strings based on whatever you set as the tokens. I would take a look at this question for more information: How do I use a delimiter in Java Scanner?
Edit :-
if you need the exact count in the exact order you could do this :-
public static void main(String[] args)
{
final String TOKENS = "true,false,!,),(";
String [] splittedTokens = TOKENS.split(",");
String Data = "'true','^','false'";
ArrayList <String> existingTokens = new ArrayList<String>();
for(int i = 0; i < splittedTokens.length; i++)
{
if(Data.contains(splittedTokens[i]))
{
existingTokens.add(splittedTokens[i]);
}
}
for(int i = 0; i < splittedTokens.length; i++)
{
int count = 0;
for(int j = 0; j < existingTokens.size(); j++)
{
if(splittedTokens[i].equals(existingTokens.get(j)))
{
count++;
}
}
System.out.println("Number of "+splittedTokens[i]+" = "+count);
}
}
if you only need all the tokens that the string contains :-
public static void main(String[] args)
{
final String TOKENS = "true,false,!,),(";
String [] splittedTokens = TOKENS.split(",");
String Data = "true^false";
for(int i = 0; i < splittedTokens.length; i++)
{
if(Data.contains(splittedTokens[i]))
{
System.out.println("The String Contains "+ splittedTokens[i]);
}
}
}
链接地址: http://www.djcxy.com/p/78354.html
下一篇: 从字符串中提取字符和单词