gpt4 book ai didi

java - 字符串分词器

转载 作者:行者123 更新时间:2023-11-29 04:03:16 25 4
gpt4 key购买 nike

任何人都可以通过在代码中添加一些注释来帮助我理解这个字符串分词器的工作原理吗?如果有任何帮助,我将不胜感激!

public String[] split(String toSplit, char delim, boolean ignoreEmpty) {

StringBuffer buffer = new StringBuffer();
Stack stringStack = new Stack();

for (int i = 0; i < toSplit.length(); i++) {
if (toSplit.charAt(i) != delim) {
buffer.append((char) toSplit.charAt(i));
} else {
if (buffer.toString().trim().length() == 0 && ignoreEmpty) {
} else {
stringStack.addElement(buffer.toString());
}
buffer = new StringBuffer();
}
}

if (buffer.length() !=0) {
stringStack.addElement(buffer.toString());
}

String[] split = new String[stringStack.size()];
for (int i = 0; i < split.length; i++) {
split[split.length - 1 - i] = (String) stringStack.pop();
}

stringStack = null;
buffer = null;

// System.out.println("There are " + split.length + " Words");
return split;
}

最佳答案

不是世界上最好的写法!但是下面的评论。总的来说,它所做的是使用字符 delim 将字符串拆分为“单词”来划定它们。如果ignoreEmpty为真,则不计算空词(即两个连续的定界符作为一个)。

public String[] split(String toSplit, char delim, boolean ignoreEmpty) {

// Buffer to construct words
StringBuffer buffer = new StringBuffer();
// Stack to store complete words
Stack stringStack = new Stack();

// Go through input string one character at a time
for (int i = 0; i < toSplit.length(); i++) {
// If next character is not the delimiter,
// add it to the buffer
if (toSplit.charAt(i) != delim) {
buffer.append((char) toSplit.charAt(i));
// Else it is the delimiter, so process the
// complete word
} else {
// If the word is empty (0 characters) we
// have the choice of ignoring it
if (buffer.toString().trim().length() == 0 && ignoreEmpty) {
// Otherwise, we push it onto the stack
} else {
stringStack.addElement(buffer.toString());
}
// Clear the buffer ready for the next word
buffer = new StringBuffer();
}
}

// If there are remaining characters in the buffer,
// then a word rather than the delimiter ends the
// string, so we push that onto the stack as well
if (buffer.length() !=0) {
stringStack.addElement(buffer.toString());
}

// We set up a new array to store the contents of
// the stack
String[] split = new String[stringStack.size()];

// Then we pop each element from the stack into an
// indexed position in the array, starting at the
// end as the last word was last on the stack
for (int i = 0; i < split.length; i++) {
split[split.length - 1 - i] = (String) stringStack.pop();
}

stringStack = null;
buffer = null;

// Then return the array
// System.out.println("There are " + split.length + " Words");
return split;
}

您可以使用 string.split 编写一个更高效的程序方法,将分隔符转换为合适的正则表达式(如果 + 为真,则以 ignoreEmpty 结尾)。

关于java - 字符串分词器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2038411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com