Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带计数的Android字符串制动器_Android_String_Count - Fatal编程技术网

带计数的Android字符串制动器

带计数的Android字符串制动器,android,string,count,Android,String,Count,我想对字符串中的字符进行计数,如果计数器达到某个限制,就会换行。但我不知道怎么解决 我发现了这个单词计数器,但是如何在特定的位置添加一个分隔符呢?例如,在每个第四个单词之后 public static int countWords(String s){ int wordCount = 0; boolean word = false; int endOfLine = s.length() - 1; for (int i = 0; i < s.length

我想对字符串中的字符进行计数,如果计数器达到某个限制,就会换行。但我不知道怎么解决

我发现了这个单词计数器,但是如何在特定的位置添加一个分隔符呢?例如,在每个第四个单词之后

public static int countWords(String s){

    int wordCount = 0;

    boolean word = false;
    int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!Character.isLetter(s.charAt(i)) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}
公共静态int countWords(字符串s){
int字数=0;
布尔字=假;
int endOfLine=s.length()-1;
对于(int i=0;i
您目前有哪些代码?它的结果是什么?好的,但是如何在限制之后向原始字符串添加一个\n,并且不停止一个单词?保存找到所需条件时要执行操作的位置。然后再次迭代原始字符串,并为之前保存的位置添加\n。这需要对您试图实现的目标进行详细说明,您能否向我们展示一个输入和输出示例?