[英]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 like1***5**8
如果字符串的长度为9,
"12345678"
则实际输出将类似于1***5**8
If String of length 9,
"123456789abcd"
then actual output will be like1***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.