简体   繁体   English

(Java) 返回当前字符串的新字符串版本,其中所有 >= 或 <= 给定字符 n 的字母都被删除

[英](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:这通过创建一个正则表达式来工作,如下所示:

  • if more is true: (?i)[nz]如果more为真: (?i)[nz]
  • if 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]是从an的字符范围(包括)。
[nz] is the range (inclusive) of characters from n to z . [nz]是从nz的字符范围(包括)。

Note: The necessary String parameter has been added to the method.注意:方法中已添加必要的String参数。

暂无
暂无

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

相关问题 如何返回当前字符串的新字符串版本,其中删除了给定 arg 中指定的字母字符 - how to Returns a new string version of the current string where the alphabetical characters specified in the given arg, are removed 给定两个字符串 base 和 remove,返回基本字符串的一个版本,其中删除字符串的所有实例都已被删除 - Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed 如何更改字符串中的字符 - 替换字母java - How to change a char in a String - replacing letters java 如何在 Java 中编写一个 function 解决方案,当“a”出现在“b”之前时,给定由字母“a”或/和“b”组成的字符串返回 true? - How to write a function solution in Java that given a String consisting of letters 'a' or/and 'b' returns true when occurences of 'a' are before 'b'? 将char放入每N个字符的java字符串中 - Putting char into a java string for each N characters 我希望将给定字符串的用户的所有小写字母都转换为“#”。但是在运行代码时,仅最后一个小写字符被转换 - I wish to convert all the lowercase letters of a user given string to '#'.But upon running the code,only the last lowercase char is getting converted 给定一个字符串,返回一个字符串,其中每个字符都是原始字符,有两个字符? - Given a string, return a string where for every char is the original, there are two chars? String n = new String(char + int)添加但应该连接 - String n = new String( char + int) adds but should concatenate 如何检查字符串中的字母/字符是否都在char数组中找到 - How to check if the letters/chars in a string are all found in a char array 在java中查找字符串的所有大写字母 - Finding all uppercase letters of a string in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM