简体   繁体   English

我需要从java中给定的字符串打印第1个字符和第5个字符

[英]I need to print 1st and every 5th character from a given String in java

I have a String of a length of more than 1000 character. 我有一个长度超过1000个字符的字符串。 In this string i have to print 1st character after that every 5th character. 在这个字符串中,我必须在第5个字符之后打印第1个字符。

I tried writing a program to iterate from 0th character to last character and have a count variable. 我尝试编写一个程序以从第0个字符迭代到最后一个字符并具有一个count变量。

If count is equal to 5. I am printing the character and count is initializing with 0. 如果count等于5。我正在打印字符,并且count用0初始化。

private static String getMaskedToken(String token) {
    if (token == null)
        return null;
    char[] charArray = token.toCharArray();
    int length = token.length();
    StringBuilder sb = new StringBuilder();
    int count = 0;
    for (int i = 0; i < length; i++) {
        count++;
        if (i == 0 || i == length - 1) {
            sb.append(charArray[i]);
        } else if (count == 5) {
            sb.append(charArray[i]);
            count=0;
        } else if(count < 5 && i == length-1){
            sb.append(charArray[i]);
        }else {
            sb.append('*');
        }
    }
    return sb.toString();
}

Need to print last character if count is less than 5 of last iteration. 如果计数少于最后一次迭代的5,则需要打印最后一个字符。

If String of length 9, "12345678" then actual output will be like 1***5**8 如果字符串的长度为9, "12345678"则实际输出将类似于1***5**8

If String of length 9, "123456789abcd" then actual output will be like 1***5****a**d 如果字符串的长度为9, "123456789abcd"则实际输出将类似于1***5****a**d

    String output = "";
    for (int i = 0; i < str.length(); i++) {
        if (i == 0) {
            output += str.charAt(i);
            output += "***";
            output += str.charAt(4);
            i = 4;
        } else if ((i - 4) % 5 == 0) {
            output += str.charAt(i);
        } else if (i == str.length()-1) { 
            output += str.charAt(i);
        } else {
            output += "*";
        }
    }
    System.out.println(output);
}

This will print 1***5****a**d for string "123456789abcd". 这将为字符串“ 123456789abcd”打印1 *** 5 **** a ** d。

Try this code, 试试这个代码,

private void printEvery5thCharacter(String str) {
    for (int i = 1; i < str.length() - 1; i += 5) {
        System.out.print(str.charAt(i - 1) + "***");
        if (i == 1) {
            i = 0;
        }
    }
    if (str.length() % 5 > 0) {
        System.out.print(str.charAt(str.length() - 1));
    }
 }

try this code: 试试这个代码:

public void printFirstAndEveryFifthCharecter(String str) 
{
    for (int i = 0 ; i < str.length() ; i++) 
    {
       if ((i+1) == 1 | (i+1) % 5 == 0) {
          System.out.print(str.charAt(i) + "***");
       }
    }
    if (str.length() % 5 > 0) {
       System.out.print(str.charAt(str.length() - 1));
    }
 }

Your code should work fine. 您的代码应该可以正常工作。 Here's an alternative without using StringBuilder and with fewer checks. 这是不使用StringBuilder且检查较少的替代方法。

private static String getFirstFifthLast(String str) {
    String[] strArray = str.split(""); //returns an array of strings with length 1
    int arrayLength = strArray.length;
    String result = strArray[0]; //append the first element
    //append element if it is in 5th position, append "*" otherwise
    for (int i = 0; i < arrayLength; i++) {
        if ((i + 1) % 5 == 0) {
            result += strArray[i];
        } else {
            result += "*";
        }
    }
    result += strArray[arrayLength - 1]; //append the last element
    return result; 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要找到从文本文件中删除重复项,比较每行的第一和第五个字符串 - need to find remove duplicates from a text file comparing 1st and 5th string from every line 更改String Java中的每个第5个字符 - Changing every 5th character in a String java 如何将数据从第 5 个活动发送到第 1 个活动? - How to send data from the 5th activity to the 1st activity? 通过getItemViewType将项目添加到每个第5个备用位置时,在recyclelerview中删除第1个项目位置作为Facebook原生广告 - Removing the 1st item position as Facebook Native ad in recyclerview when adding item to every 5th alternate position by getItemViewType 在Java中的第1个,第4个和第9个字符后放置连字符 - put hyphen after the 1st, 4th, and 9th character in java 如何从java中的字符串中删除每第12个字符 - How to delete every 12th character from a string in java 如何将一个字符串的第一个字符与 Java 中另一个字符串的第 5 个字符进行比较? 并检查它们是否相同。我收到如下错误 - how to compare first character of one string to 5th char of another string in Java? and check if they are same.I am getting error as below 如何获得字符串中的第5个字? 爪哇 - How do I get the 5th word in a string? Java 如何在Java中打印给定字符串中的字符、数字和特殊字符? - How to print character, Number and special character from given String in Java? 组合需要第5个字符串 - 5th string needed from combination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM