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

📄 zstring.java

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


  // gives a new value to the string
  public void set(String s)
  {
    myStr = s;
  }


  // gives a new value to the string
  public void set(ZString s)
  {
    myStr = s.toString();
  }


  // gives a new value to the string
  public void set(char c)
  {
    myStr = String.valueOf(c);
  }


  // adds a string
  public void add(String s)
  {
    myStr += s;
  }


  // adds a ZString
  public void add(ZString s)
  {
    myStr += s.toString();
  }


  // adds a character
  public void add(char c)
  {
    myStr += String.valueOf(c);
  }


  /* adds a character
     does the same as the method add(char c) */
  public void addChar(char c)
  {
    add(c);
  }


  //adds n c characters
  public void addNChars(int n, char c)
  {
    for(int i = 0; i < n; i++)
      myStr += String.valueOf(c);
  }


  /* adds a string
     does the same as the method add(String s) */
  public void plus(String s)
  {
    add(s);
  }


  /* adds a ZString
     does the same as the method add(ZString s) */
  public void plus(ZString s)
  {
    add(s);
  }


  /* adds a character
     does the same as the method add(char c) */
  public void plus(char c)
  {
    add(c);
  }


  /* adds a character
     does the same as the method add(char c) */
  public void plusChar(char c)
  {
    add(c);
  }


  /* adds n c characters
     does the same as the method addNChars(n, c) */
  public void plusNChars(int n, char c)
  {
    addNChars(n, c);
  }


  /* adds a string
     does the same as the method add(String s) */
  public void append(String s)
  {
    add(s);
  }


  /* adds a ZString
     does the same as the method add(ZString s) */
  public void append(ZString s)
  {
    add(s);
  }


  /* adds a character
     does the same as the method add(char c) */
  public void append(char c)
  {
    add(c);
  }


  /* adds a character
     does the same as the method add(char c) */
  public void appendChar(char c)
  {
    add(c);
  }


  /* adds n c characters
     does the same as the method addNChars(n, c) */
  public void appendNChars(int n, char c)
  {
    addNChars(n, c);
  }


  // deletes string s from the end of the string
  public void subtract(String s)
  {
    if (myStr.endsWith(s))
      deleteLastNChars(s.length());
  }


  // deletes ZString s from the end of the string
  public void subtract(ZString s)
  {
    subtract(s.toString());
  }


  // deletes character c from the end of the string
  public void subtract(char c)
  {
    deleteLastCharIf(c);
  }
 

  /* deletes character c from the end of the string
     does the same as the method subtract(char c) */
  public void subtractChar(char c)
  {
    subtract(c);
  }

 
  /* deletes string s from the end of the string
     does the same as the method subtract(String s) */
  public void minus(String s)
  {
    subtract(s);
  }


  /* deletes ZString s from the end of the string
     does the same as the method subtract(ZString s) */
  public void minus(ZString s)
  {
    subtract(s);
  }


  /* deletes character c from the end of the string
     does the same as the method subtract(char c) */
  public void minus(char c)
  {
    subtract(c);
  }


  /* deletes character c from the end of the string
     does the same as the method subtract(char c) */
  public void minusChar(char c)
  {
    subtract(c);
  }


  //inserts a string s at position pos
  public void insert(String s, int pos)
  {
    String tmpStr1 = myStr.substring(0, pos),
           tmpStr2 = myStr.substring(pos, myStr.length());

    myStr = tmpStr1 + s + tmpStr2;
  }


  //inserts a string s at first position
  public void insert(String s)
  {
    insert(s, 0);
  }

  //inserts a ZString s at position pos
  public void insert(ZString s, int pos)
  {
    insert(s.toString(), pos);
  }


  //inserts a ZString s at first position
  public void insert(ZString s)
  {
    insert(s, 0);
  }


  //inserts a character c at position pos
  public void insert(char c, int pos)
  {
    insert(String.valueOf(c), pos);
  }


  //inserts a character c at first position
  public void insert(char c)
  {
    insert(c, 0);
  }


  /* inserts a character c at position pos
     does the same as the method insert(char c, int pos) */
  public void insertChar(char c, int pos)
  {
    insert(c, pos);
  }


  /* inserts a character c at first position
     does the same as the method insert(char c) */
  public void insertChar(char c)
  {
    insert(c);
  }


  //inserts n c characters at position pos
  public void insertNChars(int n, char c, int pos)
  {
    for(int i = 0; i < n; i++)
      insertChar(c, pos);
  }


  //inserts n c characters at first position
  public void insertNChars(int n, char c)
  {
    insertNChars(n, c, 0);
  }



  //inserts a newline character at position pos
  public void insertNewlineChar(int pos)
  {
    insert("\n", pos);
  }


  //inserts a newline character at first position
  public void insertNewlineChar()
  {
    insertNewlineChar(0);
  }


  //inserts a quote before the string and another one after it
  public void insertQuotes()
  {
    String tmpStr = myStr;

    myStr = "\"" + tmpStr + "\"";
  }


  /* returns true if the string has a quote at the beginning
     and another one at the end */
  public boolean hasQuotes()
  {
    String tmpStr = myStr;

    return (firstChar() == '\"') && (lastChar() == '\"');
  }


  /* if the string has a quote at the beginning and another one at the end
     then the two quotes are deleted and a true value is returned */
  public boolean deleteQuotes()
  {
    if (!hasQuotes())
      return false;

    deleteFirstChar();
    deleteLastChar();

    return true;
  }


  //inserts a '(' before the string and a ')' after it
  public void insertParenthesis()
  {
    String tmpStr = myStr;

    myStr = "(" + tmpStr + ")";
  }


  /* returns true if the string has a '(' at the beginning
     and a ')' at the end */
  public boolean hasParenthesis()
  {
    String tmpStr = myStr;

    return (firstChar() == '(') && (lastChar() == ')');
  }


  /* if the string has a '(' at the beginning and a ')' at the end
     then these two parenthesis are deleted and a true value is returned */
  public boolean deleteParenthesis()
  {
    if (!hasParenthesis())
      return false;

    deleteFirstChar();
    deleteLastChar();

    return true;
  }


  //deletes n charateres starting at position pos
  public void deleteNChars(int n, int pos)
  {
    String tmpStr1 = myStr.substring(0, pos),
           tmpStr2 = myStr.substring(pos + n, myStr.length());

    myStr = tmpStr1 + tmpStr2;
  }


  /* deletes first n charaters
     i.e. deletes n charateres starting at first position */
  public void deleteNChars(int n)
  {
    deleteNChars(n, 0);
  }


  /* deletes first n charaters
     does the same as the method deleteNChars(int n) */
  public void deleteFirstNChars(int n)
  {
    deleteNChars(n);
  }


  //deletes first charater
  public void deleteFirstChar()
  {
    deleteNChars(1, 0);
  }


  //deletes first charater if equal to c
  public void deleteFirstCharIf(char c)
  {
    if (firstChar() == c)
      deleteFirstChar();
  }


  //deletes charater at position pos
  public void deleteChar(int pos)
  {
    deleteNChars(1, pos);
  }


  /* deletes first charater
     does the same as the method deleteFirstChar() */
  public void deleteChar()
  {
    deleteFirstChar();
  }


  //deletes charater at position pos if equal to c
  public void deleteCharIf(int pos, char c)
  {
    if (charAt(pos) == c)
      deleteChar(pos);
  }


  //deletes last n characters
  public void deleteLastNChars(int n)
  {
    String tmpStr = myStr.substring(0, myStr.length() - n);

    myStr = tmpStr;
  }


  //deletes last character
  public void deleteLastChar()
  {
    deleteLastNChars(1);
  }


  //deletes last character if equal to c
  public void deleteLastCharIf(char c)
  {
    if (lastChar() == c)
      deleteLastChar();
  }


  //deletes last character if newline
  public void deleteLastCharIfNewline()
  {
    if (lastChar() == '\n')
      deleteLastChar();
  }


  /* deletes characters until character c is found
     (starts from the end of the string) */
  public void deleteTailUntil(char c)
  {
    while (lastChar() != c)
      deleteLastChar();
  }


  /* deletes characters until a different character than c is found
     (starts from the end of the string) */
  public void deleteTailUntilNot(char c)
  {
    while (lastChar() == c)
      deleteLastChar();
  }


  /* deletes characters until character c is found
     (starts from the beginning of the string) */
  public void deleteHeadUntil(char c)
  {
    while (firstChar() != c)
      deleteFirstChar();
  }


  /* deletes characters until a different character than c is found
     (starts from the beginning of the string) */
  public void deleteHeadUntilNot(char c)
  {
    while (firstChar() == c)
      deleteFirstChar();
  }


  //deletes all characters that are in string s
  public void deleteAll(String s)
  {
    String tmpStr = "";

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

    myStr = tmpStr;
  }


  //deletes all characters that are in ZString s
  public void deleteAll(ZString s)
  {
    deleteAll(s.toString());
  }


  //deletes all c characters
  public void deleteAll(char c)
  {
    String tmpStr = "";

    for(int n = 0; n < myStr.length(); n++)
      if (myStr.charAt(n) != c)
        tmpStr += myStr.charAt(n);

    myStr = tmpStr;
  }


  //deletes all spaces
  public void deleteAllSpaces()
  {
    deleteAll(' ');
  }


  //deletes all newline characters
  public void deleteAllNewlines()
  {
    deleteAll('\n');
  }


  //deletes all quotes
  public void deleteAllQuotes()
  {
    deleteAll('\"');
  }


  //deletes all characters but those in string s
  public void deleteAllBut(String s)
  {
    String tmpStr = "";

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

    myStr = tmpStr;
  }


  //deletes all characters but those in ZString s
  public void deleteAllBut(ZString s)
  {
    deleteAllBut(s.toString());
  }


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

⌨️ 快捷键说明

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