[英](Java) Returns a new string version of the current string where all the letters either >= or <= the given char n, are removed
Returns a new string version of the current string where all the letters either >= or <= the given char n, are removed.返回当前字符串的新字符串版本,其中所有 >= 或 <= 给定字符 n 的字母都被删除。
The given letter may be az or AZ, inclusive.给定的字母可以是 az 或 AZ,包括在内。 The letters to be removed are case insensitive.
要删除的字母不区分大小写。 If 'more' is false, all letters less than or equal to n will be removed in the returned string.
如果 'more' 为 false,则返回的字符串中所有小于或等于 n 的字母都将被删除。 If 'more' is true, all letters greater than or equal to n will be removed in the returned string.
如果 'more' 为真,则返回的字符串中所有大于或等于 n 的字母都将被删除。 If the current string is null, the method should return an empty string.
如果当前字符串是 null,该方法应该返回一个空字符串。 If n is not a letter (and the current string is not null), the method should return an empty string.
如果 n 不是字母(并且当前字符串不为空),该方法应返回一个空字符串。
Questions: Since the method receives char n, how do I apply for loop to each character?问题:由于该方法接收 char n,我如何为每个字符申请循环? Do I use arrayOfArg and is it correct to use arrayOfArg[i] ++ to store letters more than the letter?
我是否使用 arrayOfArg 并且使用 arrayOfArg[i] ++ 来存储比字母更多的字母是否正确?
public String filterLetters(char n, boolean more) {
char [] arrayOfArg = n.toCharArray();
String myNewString = "";
String removedChar = "";
if (n == null) {
return "";
}
for (int i = 0; i < Character.length; i++) {
if (more == false) {
//all letters less than or equal to n will be removed in the returned string
if (Character.isLetter(i) == true) continue;
removedChar = arrayOfArg[i] ++;
myNewString = arrayOfArg.replace(removedChar,"");
}
if (more == true) {
//all letters less than or equal to n will be removed in the returned string
if (Character.isLetter(i) == true) continue;
removedChar = arrayOfArg[i] -- ;
myNewString = arrayOfArg.replace(removedChar,"");
}
}
return myNewString;
}
This is a slight modification to the code you requested but the output works properly.这是对您请求的代码的轻微修改,但 output 可以正常工作。 I'm sure someone else has a better idea than me.
我敢肯定有人比我有更好的主意。 This is just a starting place.
这只是一个起点。
public static String filterLetters(String word, String n, boolean more) {
String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
int charValue = 0;
//Find Numerical Value of Char n
for (int i = 0; i < alphabet.length; i++) {
if (alphabet[i].equalsIgnoreCase(n)) {
charValue = i + 1;
}
}
if (more) {
for (int i = charValue; i < alphabet.length; i++) {
word = word.replaceAll(alphabet[i],"");
}
} else {
for (int i = charValue; i > 0; i--) {
word = word.replaceAll(alphabet[i],"");
}
}
return word;
}
Use regex!使用正则表达式!
public String filterLetters(String str, char n, boolean more) {
if (str == null || !Character.isLetter(n)) {
return "";
}
String range = more ? n + "-z" : "a-" + n;
return str.replaceAll("(?i)[" + range + "]", "");
}
This works by creating a regular expression as follows:这通过创建一个正则表达式来工作,如下所示:
more
is true: (?i)[nz]
more
为真: (?i)[nz]
more
is false: (?i)[an]
more
为假: (?i)[an]
where "n" is the actual char passed in.其中“n”是传入的实际字符。
(?i)
flag turns on case insensitivity. (?i)
标志打开不区分大小写。
[an]
is the range (inclusive) of characters from a
to n
. [an]
是从a
到n
的字符范围(包括)。
[nz]
is the range (inclusive) of characters from n
to z
. [nz]
是从n
到z
的字符范围(包括)。
Note: The necessary String
parameter has been added to the method.注意:方法中已添加必要的
String
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.