【问题标题】:Prevent whitespace in a string after one is entered输入后防止字符串中出现空格
【发布时间】:2016-11-18 15:27:56
【问题描述】:

我编写了一个用户无法在字符串中输入第一个空格的代码。 允许用户在最少 2 个字符后输入空格。 我需要重新定义我的方法,以便用户输入一次空格,并且只在两个或多个字符之后输入一次。之后应该防止它。我该怎么做?

case UPDATE_NAME:
	    if (firstName.getText().toString().startsWith(" "))
		firstName.setText(firstName.getText().toString().trim());

	    if (firstName.getText().toString().contains("  "))
		firstName.setText(firstName.getText().toString().replace("  ", " "));

	    int indexOfSpace = firstName.getText().toString().lastIndexOf(" ");
	    if (indexOfSpace > 0) {
		String beforeSpace = firstName.getText().toString().substring(0, indexOfSpace);
		String[] splitted = beforeSpace.split(" ");
		if (splitted != null && splitted.length > 0) {
		    if (splitted[splitted.length - 1].length() < 2)
			firstName.setText(firstName.getText().toString().trim());
		}
	    }

【问题讨论】:

    标签: android string substring whitespace


    【解决方案1】:

    使用正则表达式pattern。我made one 应该符合您的要求。

    \S{2}\S*\s\S*\n
    
    Explanation:
    \S{2} two non whitespace
    \S* n non whitespace
    \s a whitespace
    \S* n non whitespace
    \n newline (i only added that for regexr, you may not need it)
    

    另一种方式: 遍历String.charAt(int),如果前两个字符中有空格,则返回false,计算所有空格,如果n > 1,则返回false。

    【讨论】:

    • 如何将字母添加到此模式中
    • \S 实际上是字母/数字/等。它包含除空格之外的所有字符,
    【解决方案2】:

    这个方法应该满足你的要求:

    private static boolean isValidFirstName(String firstName) {
        if (firstName != null && !firstName.startsWith(" ")) {
            int numberOfSpaces = firstName.length() - firstName.replace(" ", "").length();
            if (firstName.length() < 2 || numberOfSpaces <= 1) {
                return true;
            }
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      你需要做的是使用 TextWatcher

      public class CustomWatcher implements TextWatcher {
      
      private String myText;
      private int count = 0;
      
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after){
          myText= s;
      }
      
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
      
      }
      
      @Override
      public void afterTextChanged(Editable s) {
          //check if there is a space in the first 2 characters, if so, sets the string to the previous before the space
          if(s.length() < 3 && s.contains(" "))
              s= myText;
      
          //if the length is higher than 2, and the count is higher than 0 (1 space added already), puts the string back if a space is entered
          else if(s.contains(" ") && count > 0)
              s= myText;
      
          //If none of the above is verified and you enter a space, increase count so the previous if statement can do its job
          else if(s.contains(" "))
              count++;
      
      }
      

      }

      然后,将其设置为您的 EditText

      mTargetEditText.addTextChangedListener(new CustomWatcher());
      

      【讨论】:

        【解决方案4】:

        您可以使用TextWatcher 控制您的editText(我假设),如果长度为

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-28
          • 1970-01-01
          • 2014-03-29
          • 1970-01-01
          • 2012-08-02
          • 1970-01-01
          • 1970-01-01
          • 2011-05-23
          相关资源
          最近更新 更多