⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zstring.java

📁 Java实现的遗传算法工具集:GA Playground
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

    for(int n = 0; n < myStr.length(); n++)
      if (s.indexOf(myStr.charAt(n)) == -1)
        tmpStr += myStr.charAt(n);
      else
        tmpStr += Character.toUpperCase(myStr.charAt(n));


    myStr = tmpStr;
  }


  //converts to uppercase all characters that occur in ZString s
  public void toUpperCaseAll(ZString s)
  {
    toUpperCaseAll(s.toString());
  }


  //converts to uppercase all c characters
  public void toUpperCaseAll(char c)
  {
    toUpperCaseAll(String.valueOf(c));
  }


  //converts to lowercase all characters that occur in string s
  public void toLowerCaseAll(String s)
  {
    String tmpStr = "";

    for(int n = 0; n < myStr.length(); n++)
      if (s.indexOf(myStr.charAt(n)) == -1)
        tmpStr += myStr.charAt(n);
      else
        tmpStr += Character.toLowerCase(myStr.charAt(n));


    myStr = tmpStr;
  }


  //converts to lowercase all characters that occur in ZString s
  public void toLowerCaseAll(ZString s)
  {
    toLowerCaseAll(s.toString());
  }


  //converts to lowercase all c characters
  public void toLowerCaseAll(char c)
  {
    toLowerCaseAll(String.valueOf(c));
  }


  //converts to uppercase all characters but those in string s
  public void toUpperCaseAllBut(String s)
  {
    String tmpStr = "";

    for(int n = 0; n < myStr.length(); n++)
      if (s.indexOf(myStr.charAt(n)) != -1)
        tmpStr += myStr.charAt(n);
      else
        tmpStr += Character.toUpperCase(myStr.charAt(n));


    myStr = tmpStr;
  }


  //converts to uppercase all characters but those in ZString s
  public void toUpperCaseAllBut(ZString s)
  {
    toUpperCaseAllBut(s.toString());
  }


  //converts to uppercase all characters but c
  public void toUpperCaseAllBut(char c)
  {
    toUpperCaseAllBut(String.valueOf(c));
  }


  //converts to lowercase all characters but those in string s
  public void toLowerCaseAllBut(String s)
  {
    String tmpStr = "";

    for(int n = 0; n < myStr.length(); n++)
      if (s.indexOf(myStr.charAt(n)) != -1)
        tmpStr += myStr.charAt(n);
      else
        tmpStr += Character.toLowerCase(myStr.charAt(n));


    myStr = tmpStr;
  }


  //converts to lowercase all characters but those in ZString s
  public void toLowerCaseAllBut(ZString s)
  {
    toLowerCaseAllBut(s.toString());
  }


  //converts to lowercase all characters but c
  public void toLowerCaseAllBut(char c)
  {
    toLowerCaseAllBut(String.valueOf(c));
  }


  //shifts all characters to the left
  public void shiftLeft()
  {
    deleteFirstChar();
  }


  /* shifts all characters to the left
     and inserts the specified character
     at rightmost position (i.e. last position) */
  public void shiftLeft(char c)
  {
    deleteFirstChar();
    plus(c);
  }


  //shifts all characters to the right
  public void shiftRight()
  {
    deleteLastChar();
  }


  /* shifts all characters to the right
     and inserts the specified character
     at leftmost position (i.e. first position) */
  public void shiftRight(char c)
  {
    deleteLastChar();
    insert(c, 0);
  }


  //rotates all characters to the left
  public void rotateLeft()
  {
    char c = firstChar();
    deleteFirstChar();
    plus(c);
  }


  //rotates all characters to the right
  public void rotateRight()
  {
    char c = lastChar();
    deleteLastChar();
    insert(c, 0);
  }


  //reverses the string
  public void reverse()
  {
    String tmpStr = "";

    for(int n = myStr.length() - 1; n >= 0; n--)
      tmpStr += myStr.charAt(n);

    myStr = tmpStr;
  }


  //fills the string with n c characters (deletes the original string!)
  public void fillWithNChars(int n, char c)
  {
    String tmpStr = "";
    
    for (; n > 0; n--)
      tmpStr += c;

    myStr = tmpStr;
  }


  /* fills the string with c characters
     (deletes the original string! maintains its size) */
  public void fillWithChars(char c)
  {
    fillWithNChars(myStr.length(), c);
  }


  //shortens the string if it is larger than pos
  public void shorten(int pos)
  {
    if (myStr.length() > pos)
    {
      String tmpStr = myStr.substring(myStr.length() - pos, myStr.length());

      myStr = tmpStr;
    }
  }


  /* compacts the string if it is larger than pos
     a compacted string has the form: "..." + shortened string */
  public void compact(int pos)
  {
    if (myStr.length() > pos)
    {
      String tmpStr = myStr.substring(myStr.length() + 3 - pos, myStr.length());
      
      myStr = "..." + tmpStr;
    }
  }


  /* arranges the string by deleting:
    - spaces at the beggining and end of the string
    - all spaces before a space, comma or period
  */
  public void arrange()
  {
    String tmpStr = "";
    
    deleteHeadUntilNot(' ');
    deleteTailUntilNot(' ');

    if (myStr.length() == 0)
      return;

    char c = myStr.charAt(0);

    for (int n = 1; n < myStr.length(); n++)
    {
      if (! (c == ' ' &&
         ((myStr.charAt(n) == ' ') || (myStr.charAt(n) == ',') || (myStr.charAt(n) == '.'))))
        tmpStr += c;

      c = myStr.charAt(n);
    }
        
    tmpStr += c;
    myStr = tmpStr;
  }


  /*
    our string value:
      - is consired invalid when equal to one of the values specified in invalidValues;
      - remains unchanged if consired valid;
      - changes to defaultValue if consired invalid.

    values in invalidValues are separated by separatorChar;
    when ignoreCase is set to true, comparisions between our string and invalid values
    are case insensitive

    returns:
      -1 if string value is valid
      or position of invalid value inside invalidValues string.
  */
  public int validate(ZString invalidValues, ZString defaultValue, char separatorChar,
    boolean ignoreCase)
  {
    String curValue; // current value inside invalidValues
    int n,
        numTokens;

    StringTokenizer st = new StringTokenizer(invalidValues.toString(),
      String.valueOf(separatorChar));
    numTokens = st.countTokens();

    for (n = 0; n < numTokens; n++)
    {
      curValue = st.nextToken();
      
      if ((!ignoreCase) && (myStr.equals(curValue))) // case sensitive
      { // string is invalid
        myStr = defaultValue.toString();
        return n;  // return position of invalid value
      }
      else 
        if (ignoreCase && (myStr.equalsIgnoreCase(curValue))) // case insensitive
        { // string is invalid
          myStr = defaultValue.toString();
          return n;  // return position of invalid value
        }
    }

    return -1; // string is valid
  }   


  // calls validate(..,..,..,..) with case sensitivity on
  public int validate(ZString invalidValues, ZString defaultValue, char separatorChar)
  {
    return validate(invalidValues, defaultValue, separatorChar, false);
  }


  /* calls validate(..,..,..,..) with:
    - case sensitivity on;
    - comma as separator char.
  */
  public int validate(ZString invalidValues, ZString defaultValue)
  {
    return validate(invalidValues, defaultValue, ',', false);
  }


  /* calls validate(..,..,..,..) with:
    - case sensitivity on;
    - comma as separator char;
    - empty string as default value.
  */
  public int validate(ZString invalidValues)
  {
    ZString tmpStr = new ZString("");
    return validate(invalidValues, tmpStr, ',', false);
  }


  /*
    our string value is consired invalid when equal
    to one of the values specified in invalidValues

    values in invalidValues are separated by separatorChar;
    when ignoreCase is set to true, comparisions between our string and invalid values
    are case insensitive

    returns:
      -1 if string value is valid
      or position of invalid value inside invalidValues string.
  */
  public int check(ZString invalidValues, char separatorChar, boolean ignoreCase)
  {
    String curValue; // current value inside invalidValues
    int n,
        numTokens;

    StringTokenizer st = new StringTokenizer(invalidValues.toString(),
      String.valueOf(separatorChar));
    numTokens = st.countTokens();

    for (n = 0; n < numTokens; n++)
    {
      curValue = st.nextToken();
      
      if ((!ignoreCase) && (myStr.equals(curValue))) // case sensitive
      { // string is invalid
        return n;  // return position of invalid value
      }
      else 
        if (ignoreCase && (myStr.equalsIgnoreCase(curValue))) // case insensitive
        { // string is invalid
          return n;  // return position of invalid value
        }
    }

    return -1; // string is valid
  }   


  // calls check(..,..,..) with case sensitivity on
  public int check(ZString invalidValues, char separatorChar)
  {
    return check(invalidValues, separatorChar, false);
  }


  /* calls check(..,..,..) with:
    - case sensitivity on;
    - comma as separator char.
  */
  public int check(ZString invalidValues)
  {
    return check(invalidValues, ',', false);
  }


  /* returns position of a digit in the string
     or -1 if no digit found */
  public int findADigit()
  {
    int pos = 0;

    for (int n = 0; n < myStr.length(); n++, pos++)
      if (digits.indexOf(myStr.charAt(n)) != -1)
        return pos;
    
    return -1;
  }


  /* returns position of anything but a digit in the string
     or -1 if only digits found */
  public int findButADigit()
  {
    int pos = 0;

    for (int n = 0; n < myStr.length(); n++, pos++)
      if (digits.indexOf(myStr.charAt(n)) == -1)
        return pos;
    
    return -1;
  }


  // returns number of occurrences of character c in string
  public int countOccurences(char c)
  {
    int count = 0;
    
    for (int n = 0; n < myStr.length(); n++)
      if (myStr.charAt(n) == c)
        count++;
  
    return count;
  }


  // returns number of occurrences of string s in our string
  public int countOccurences(String s)
  {
    int count = 0,
        n = 0,
        pos;
    
    while ((pos = myStr.indexOf(s, n)) != -1)
    {
      n = pos + s.length();
      count++;
    }

    return count;
  }


  // returns number of occurrences of ZString s in our string
  public int countOccurences(ZString s)
  {
    return (countOccurences(s.toString()));
  }


  /* returns position of first occurence of char c
     starts the search at position pos and going to the left
     returns -1 if no char c found */
  public int findNearestCharAtLeft(char c, int pos)
  {
    for(int n = pos; n >= 0; n--)
      if (myStr.charAt(n) == c)
        return n;

    return -1;
  }


  /* returns position of first occurence of char c
     starts the search at position pos and going to the right
     returns -1 if no char c found */
  public int findNearestCharAtRight(char c, int pos)
  {
    for(int n = pos;  n < myStr.length(); n++)
      if (myStr.charAt(n) == c)
        return n;

    return -1;
  }

  
  /* returns position of first occurence of char c
     starts the search at the beginning of the string
     returns -1 if no char c found */
  public int findNearestCharAtBeginning(char c)
  {
    return (myStr.indexOf(c));
  }


  /* returns position of first occurence of char c
     starts the search at the end of the string
     returns -1 if no char c found */
  public int findNearestCharAtEnd(char c)
  {
    return (findNearestCharAtLeft(c, myStr.length() - 1));
  }


  /* returns position of first occurence of char c
     starts the search at position pos
     returns -1 if no char c found */
  public int findNearestCharAt(char c, int pos)
  {
    int leftPos = findNearestCharAtLeft(c, pos),
        rightPos = findNearestCharAtRight(c, pos);

    return (( (pos - leftPos) <= (rightPos - pos)) ? leftPos : rightPos);
  }


  /* returns position of first char different than c
     starts the search at position pos and going to the left
     returns -1 if just c chars found */
  public int findNearestDifferentCharAtLeft(char c, int pos)
  {
    for(int n = pos; n >= 0; n--)
      if (myStr.charAt(n) != c)
        return n;

    return -1;
  }


  /* returns position of first char different than c
     starts the search at position pos and going to the right
     returns -1 if just c chars found */
  public int findNearestDifferentCharAtRight(char c, int pos)
  {
    for(int n = pos;  n < myStr.length(); n++)
      if (myStr.charAt(n) != c)
        return n;

    return -1;
  }


  /* returns position of first char different than c
     starts the search at the beginning of the string
     returns -1 if just c chars found */
  public int findNearestDifferentCharAtBeginning(char c)
  {
    return (findNearestDifferentCharAtRight(c, 0));
  }


  /* returns position of first char different than c
     starts the search at the end of the string
     returns -1 if just c chars found */
  public int findNearestDifferentCharAtEnd(char c)
  {
    return (findNearestDifferentCharAtLeft(c, myStr.length() - 1));
  }


  /* returns position of first char different than c
     starts the search at position pos
     returns -1 if just c chars found */
  public int findNearestDifferentCharAt(char c, int pos)
  {
    int leftPos = findNearestDifferentCharAtLeft(c, pos),
        rightPos = findNearestDifferentCharAtRight(c, pos);

    return (( (pos - leftPos) <= (rightPos - pos)) ? leftPos : rightPos);
  }



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -