我试图以这样的方式对字符串进行标记:...
示例字符串
Public Static void main(String[args])
String tokenizer tokenize like
public
static
void
main
String
args
但我想以这种方式进行标记化
public
static
void
main
(
String
[
args
]
)
表示它还会打印字符串移动到标记化
的字符答案 0 :(得分:1)
public String[] tokenise(String str){
String progress = "";
LinkedList<String> list = new LinkedList<String>();
for(int c = 0; c < str.length(); c++){
char ch = str.charAt(c);
// Skip next char if the current char is an escape character
if(ch == '\\'){
c++;
continue;
}
// If current char is to be tokenised, add progress and char to list
if(ch == ' ' || ch == '(' || ch == ')' || ch == '[' || ch == ']'){
if(!progress.equals("")) list.add(progress);
list.add(ch+"");
progress = "";
}else{
progress += ch;
}
}
String[] result = new String[list.size()];
for(int c = 0; c < result.length; c++) result[c] = list.get(c);
return result;
}
答案 1 :(得分:0)
import java.util.Scanner;
import java.util.ArrayList;
public class SOQ17
{
public Scanner scan;
public String test;
public boolean check = true;
public SOQ17()
{
System.out.print("Enter your string.\n");
scan = new Scanner(System.in);
test = scan.nextLine();
for(int i = 0; i < test.length(); i++)
{
if((test.charAt(i) >= 'A' && test.charAt(i) <= 'Z') || (test.charAt(i) >= 'a' && test.charAt(i) <= 'z'))
{
System.out.print(test.charAt(i) + "");
check = true;
}
else
{
if(check)
{
System.out.println("");
}
System.out.println(test.charAt(i));
check = false;
}
}
}
public static void main(String[] args)
{
while(true)
{
SOQ17 soq = new SOQ17();
}
}
}
以下是我的工作方式,它将为每一件不是字母的东西创造一条新线。但是,如果它是一封信,它只会将其打印出来。另外,我使用布尔'检查'来确保在字母之间来回交替时应用正确的格式。