A STR class

xiaoxiao2021-03-06  41

Package com.wen;

Import java.text. *; import java.util. *;

/ ** a Robust string processing class.

The Java String and Stringbuffer Classes Are Final. So it is not possible to inherit All of the functionality of one of those classes and enhance it with a few more methods.

This Class Attempts to Provide All of the FunctionAryalog, Plus Provide A Wide Collection of Additional FunctionAlity.

This class internally functions more like StringBuffer than String. If a Str object is appended and needs to grow beyond its current size, it will do so without programmer intervention. It also keeps a little extra buffer of size to accomodate small amounts of growth without needing Memory Re-Allocation.

- - - - - - - - - - - - - -

CopyRight (C) 1998-2000 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell IT, Share it ... add a copyright for the portions you add ...

My Goal in Giving this Away and maintaining the copyright is to hopefully direct development. "P>

The Original Source Can Be Found At

- - - - - - - - - - - - - -

@author Paul Wheaton * / public class Str {private char [] buffy; // the string characters private int len ​​= 0; // current length of the string private int extra = 50; // how much extra to grow when growth is Needed

/ ** Set the Object to Have An Initial Capacity.

@Param InitialSize How Many Characters The Object Can Initially Hold.

* / Public Str (INT InitialSize) {Buffy = New Char [InitialSize];

/ ** CREATE An Object with a specific initial capacity and to becomle to grow by a specific increment.

@param initialSize How many characters the object can initially hold.

@param initialExtra How many characters the object will grow by when more memory is needed.

* / public Str (int initialSize, int initialExtra) {buffy = new CHAR [InitialSize]; EXTRA = INIALEXTRA;

/ ** The Object Has Memory Allocated to Store An Average Size String.

* / Public Str () {Buffy = New Char [Extra];

/ ** The Object is inititialized to have an exact copy of the character array. ENOUGH MEMORY IS Allocated for Some Growth.

@Param C The Character Array To Copy.

* / public strick {len = C.LENGTH; buffy = new char [len extra]; int i; for (i = 0; i < Len; i ) {buffy [i] = c [i];}}

/ ** a New Object is createha is an exact copy of the provided str ject.

Enough memory is allocated for some growth.

@Param s the str ject to copy.

* / public strick {set (s);}

/ ** Force the contents of this object to be the same as another str ject.

@Param s the str ject to copy.

* / public void set (str s) {len = s.len; extra = s.extra; buffy = new char [s.buffy.Length]; int i; for (i = 0; i

@Param s the string Object to copy.

* / public str (string s) {if (s == null) {buffy = new char [extra];} else {set (s);}}

/ ** Force the contents of this object to be the same as a string object.

@Param s the string Object to copy.

* / public void set (string s) {len = s.Length (); buffy = new char [len extra]; int 1; for (i = 0; i

/ ** Get the current length of your string - NOT The Same as the Amount of Memory Allocated.

Works Just Like The Length () Methods in string and stringbuffer.

@Return The Current String Length.

* / public int length () {return len;}

/ ** Force the length of your string.

If you want to empty your string, use setlength (0);

If you specify a Number That Is Longer Than The Current String Length, The New Characters Will Be Null Characters.

@Param Newlen What You Want The New Length of Your String To BE.

* / public void setlength (int newlen) {if (newlen> buffy.Length) {setcapacity (newlen extra);} if (Newlen> LEN ) {// Fill New Chars with Zero While (LEN

A lot like the ansi strcmp () function.

@Param s The str Object to compare to.

@Return 0 if this is equal to s, a negative value if this is less s, a posient value if this is well this is basing tour,

* / public int int Compareto (STR S) {int Returnval = 0; int mincomp = math.min (len, s.len); int i = 0; while ((i

/ ** a String Comparison Method That Returns a Numeric Result.

A lot like the ansi strcmp () function.

@Param s the string object to compare to.

@Return 0 if this is equal to s, a negative value if this is less s, a posient value if this is get tour,

* / public int int Compareto (string s) {int Returnval = 0; int mincomp = math.min (len, s.Length ()); int i = 0; while ((i

This Method is Case Sensitive.

@Param s The str Object to compare to.

@Return True if s iS identical.

* / public boolean EQ (Str s) {Return (Compareto (s) == 0);}

/ ** Test for equality Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the string Object to compare to.

@Return True if s iS identical.

* / public boolean eq (string s) {return (compareto (s) == 0);}

/ ** Test for "not equal" betWeen Your string and the string in s.

This Method is Case Sensitive.

@Param s The str Object to compare to.

@Return False if s iS identical.

* / public boolean ne (str s) {return! (Compareto (s) == 0);} / ** Test for "not equal" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the string object to compare to.

@Return false if s iS identical.

* / public boolean ne (string s) {return! (Compareto (s) == 0);}

/ ** Test for "Less Than" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s The str Object to compare to.

@Return True if your string is "less Than" s.

* / public boolean lt (str s) {return (Compareto (S) <0);}

/ ** Test for "Less Than" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the string object to compare to.

@Return True if your string is "less" s.

* / public boilean lt (string s) {return (Compareto (S) <0);}

/ ** Test for "less tour or equal to" betWeen your string and the string in s.

This Method is Case Sensitive.

@Param s The str ject to compare to.

@Return True if you string is "less tour or equal to" s.

* / public boolean le (str s) {return (Compareto (S) <= 0);

/ ** Test for "less tour or equal to" betWeen your string and the string in s.

This Method is Case Sensitive.

@Param s the string Object to compare to.

@Return True if your string is "less tour" s.

* / public boolean le (string s) {return (Compareto (S) <= 0);} / ** Test for "Greater Than or equal to" betWeen Your string and the string in s.

This Method is Case Sensitive.

@Param s the str Object to compare to.

@Return True if you string is "greater tour or equal to" s.

* / public boolean ge (str s) {return (Compareto (s)> = 0);

/ ** Test for "Greater Than" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the string Object to compare to.

@Return True if your string is "greater tour or equal to" s.

* / public boolean ge (string s) {return (Compareto (s)> = 0);

/ ** Test for "Greater Than" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the str Object to compare to.

@Return True if your string is "greater Than" s.

* / public boolean gt (str s) {return (Compareto (s)> 0);}

/ ** Test for "Greater Than" Between Your string and the string in s.

This Method is Case Sensitive.

@Param s the string object to compare to.

@Return True if your string is "greater tour" s.

* / public boolean gt (string s) {return (Compareto (s)> 0);}

/ ** SETHOW MUCH Extra to Grow when Growth Is Needed.

smaller value, but frequent Reallocation May Take a Lot of Time.

@param howMuch The number of extra characters to allocate when memory reallocation is required Values ​​must be greater than zero

* / public void setExtra (int howMuch) {if (howMuch> 0) {extra = howMuch;}}..

/ ** Get The Number of Characters this Object Can Hold WITHOUT DOING A Reallocation.

@Return The Number of Characters this Object Can Currently Hold without Reallocation.

* / public int getCapacity () {return buffy.Length;}

/ ** Force this Object to Be Aable to Hold A Specific Number of Characters WITHOUT Reallocation.

Any Attempt to make the string gring gring growth.

Setting The Capacity To Be Smaller Than The Current String Size Will Result In Trailing Characters Being Clipped.

@Param HowMuch How Many Characters Will Be The New Capacity.

* / public void setcapacity (int hoWMUCH) {char [] newbuffy = new char [hoWMUCH]; howMuch

/ ** Retrieve a Copy of One Character.

@Param Index Which Character (0 is the first character).

@return the retrieved character. Returns a null character if index is outside of 0..LENGTH.

* / public char Get (int index) {char ReturnValue = 0; IF (Numbers.inrange (INDEX, 0, LEN - 1)) {returnvalue = buffy [index];} Return ReturnValue;} / ** Retrieve A Copy of the last character.

@Return The Retrieved Character. Returns a Null Character if The string is empty.

* / public char Getlast () {return get (len - 1);

/ ** USED for Compatibility with string and stringbuffer.

See get ().

* / public char charat (int index) {return get (index);

/ ** RETRIEVE A COPY OF A SUBSTRING.

@param index The character of the substring.

@param length The length of the substring. Specifying a substring beyond the end of the string will return as much of the string as possible.

@return A Str object containing the Substring.

* / public str get (int index, int length) {str s = new str (length extra); if (index <0) {index = 0;}}} (index LEN) {Length = LEN - INDEX;} INT i; for (i = 0; i

/ ** Provided for compatibility with String and StringBuffer and the case mixing many people expect

* / public Str subString (int index, int length) {return get (index, length);}. / ** Provided for compatibility with String and stringbuffer and the case.

* / public str on {return get (index, length);

/ ** Get all of the substring That Occurs Before a particular character.

@param index The character that marks the end of the substring. This character is not included in the substring.

@return A Str object containing the substring.

* / public Str before (int index) {return get ( 0, index);

/ ** Get All of the substring That Occurs through a particular character.

@param index The character that marks the end of the substring. This character is included in the substring.

@return A Str object containing the substring.

* / public Str through (int index) {return get (0 , index;

/ ** Get All of the substring That Occurs After a particular character.

@param index The character that marks the beginning of the substring. This character is not included in the substring.

@return A Str object containing the substring.

* / public Str after (int index) {return get ( INDEX 1, (Len - Index) - 1);

/ ** get all of the substring That Occurs from a particular character to the end of the string.

@param index The character that marks the beginning of the substring. This character is included in the substring.

@return A Str object containing the substring.

* / public Str from (int index) {return get (index , Len - index;} / ** generate a class of the string.

@Return a new char Array.

* / public char [] TOCHARARRAY () {char [] b = new char [len]; int i; for (i = 0; i

/ ** Set One Character in the string.

@param index Which character to set. If index references a character beyond the last character of the string, the string will be lengthened and the last character will be set appropriately (undefined characters will be set to a null character).

@ PARAM C The Character to Be Pland At Index.

* / Public Void Set (INDEX> = 0) {if (INDEX> = LEN) {setLength (INDEX 1);} Buffy [index] = C;}}

/ ** Provided for Compatibility with stringbuffer.

* / public void setcharat (int index, char c) {set (index, c);}

/ ** Add one character to the end of the string.

@Param c the character to append to the end of the string.

* / public void append (char c) {testbuffylen (len 1); buffy [len] = C; LEN ;}

/ ** append a string on to the end of your string.

@Param s the str ject to be appended.

* / public void append (str s) {int newlen = len s.len; testbuffylen; INT i; for (i = 0; i

@Param s the string Object to be appended.

* / public void append (st NEWLEN = LEN S.LENGTH (); testbuffylen (newlen); int i; for (i = 0; i < S.Length (); i ) {buffy [len i] = S.Charat (i);} len = newlen;

/ ** INSERT a Character Immedierately Before A Particular Character.

@Param C the Character to be inserted.

@Param Index WHERE to INSERT C. The Character Currently At Index Will Me Moved To The Right.

* / public void insert (CHAR C, INDEX) {TestBuffylen LEN 1);

//move chars from index on one to the right int I; for (i = len - 1; i> = index; i--) {buffy [i 1] = buffy [i];

BUFFY [INDEX] = C; LEN ;

// use by several other method private void insertjun (int howmany, int index) {int newlen = LEN HOWMANY; TestBuffylen (Newlen)

// Move Chars from index on to the right int I; for (i = len - 1; i> = index; i--) {buffy [i howmany] = buffy [i];}

Len = newlen;

/ ** Insert a Str object immediately before a particular character.

@param s The Str object to be inserted.

@param index Where to insert s. The character currently at index will me moved to the right. < P> * / public void insert (Str s, int index) {INSERTJUNK (S.LEN, INDEX);

INT i; for (i = 0; i

/ ** Insert A String Object Immedierately Before A Particular Character.

@param s The String object to be inserted.

@param index Where to insert s. The character currently at index will me moved to the right.

* / public void insert (String s, int index) {insertJunk (S.Length (), INDEX);

INT i; for (i = 0; i

/ ** Remove Characters from Your String.

@Param Index Referencing The First Character to Be Removed.

@Param HowMany The Numr of Characters To Remove.

* / Public Void Delete (INDEX, INT HOWMANY) {IF (Len> 0) {IF (INDEX LEN) {Howmany = LEN - INDEX;} int newlen = LEN - HOWMANY; INT i; for (i = index; i

/ ** Remove Characters from The Beginning of Your String.

@Param HowMany The Number of Characters To Remove.

* / public void deletefirst (int howmany) {delete (0, howmany);

/ ** REMOVE Characters from the end of your string.

@Param HowMany The Number of Characters To Remove.

* / public void deletelast (int ~ - howmany, howmany);

/ ** REMOVE The Last Character from the end of your string.

* / public void deletelast () {deletelast (1);

/ ** if you wish to Treat Your String Like a Stack, You Can Push and Pop Characters.

@Param c the character That Will be appended to the end of your string.

* / public void push (char c) {append (c);

/ ** if you wish to Treat Your String Like a Stack, You Can Push and Pop Characters.

Remember the old assembly programming pearl: May all your pushes be popped.

@return the last character in Your string. If there is a null character is returned.

* / public char pop () {char returnval = 0; if (len> 0) {LEN- -; Returnval = Buffy [len];} return returnval;

/ ** Test to see if you string begin with a specific sequence of characters.

THIS TEST I MUCH FASTER THAN THE Equivalent in The Regular Expression Library. And is Also Much Faster Than Doing A Substring Comparison.

THIS Test Is Case Sensitive.

@param s A String object representing the first few characters being tested for

@return True if s matches the beginning of your string exactly

* / public boolean startsWith (String s) {boolean returnValue = false;.. if (LEN> S.LENGTH ()) {Boolean match = true; int 1; for (i = 0; i

This Method is Case Sensitive.

@param c The character to search for.

@param startPos The character position in your string to start looking for c.

@return The position of the character that matches c where 0 is the first character of the string. -1 is returned if a match.

* / public int indexof (char c, int startpos) {boolean done = false; while (! Done) {if (startpos> = len) {startpos = -1; DONE = true;} else {ix (buffy [startpos] == c) {done = true;} else {startpos ;}}} Return StartPos;

/ ** Find the position for your string of a particular character, ignoring case.

this method is not copy sensive.

@param c The character to search for.

@param startPos The character position in your string to start looking for c.

@return The position of the character that matches c where 0 is the first character of the string. -1 is returned if a match.

* / public int indexofignorecase (CHAR C, INT Startpos) {char lowerc = character.tolowercase (c); boolean done = false; while (! Done) { IF (startpos> = len) {startpos = -1; done = true;} else {if (buffy [startpos]) == LowerC) {done = true;}}}}} return StartPOS }

/ ** Find The position for a string of a particular sequence of characters.

This Method is Case Sensitive.

@param s A String object representing the character sequence to search for.

@param startPos The character position in your string to start looking for s.

@return -1 is returned if a match could not be found. Otherwise The position of the first character of the match is returned.

* / public int indexof (S.LENGTH ()> 0) {boolean done = false; while (! done) { StartPOS = indexof (S.Charat (0), StartPos; if (startpos == -1) {DONE = true;} else {if (StartPOS S.LENGTH ()> LEN) {startpos = -1; done = Else {boolean match = true; int i; fo R (i = 1; i

} Return StartPos;

This Method is Not Case Sensitive.

@param s A String object representing the character sequence to search for.

@param startPos The character position in your string to start looking for s.

@return -1 is returned if a match could not be found. Otherwise The position of the first character of the match is returned.

* / public int indexofignorecase (String s, int startpos) {string lowers = S.tolowercase (); if (lowers.length ()> 0) {Boolean DONE = false; while (! "= indexofignorecase (lowers.charat (0), startpos); if (startpos == -1) {DONE = true;} else {ix (startpos lowers.length ()> LEN ) {Startpos = -1; done = true;} else {bool Ean match = true; int i; for (i = 1; i

}}}}} Else {StartPos = -1;} Return StartPos;

This Method Is Case Snsitive. Searching Begins with The First Character in Your String.

@Param c the character to search for.

@return the position of the character thing matches c. -1 is returned if a match.

* / public int indexof (char c) {RETURN IndexOf (C, 0);

/ ** Find The position for a string of a particular sequence of characters.

This Method Is Case Snsitive. Searching Begins with The First Character in Your String.

@param s A String object representing the character sequence to search for.

@return -1 is returned if a match could not be found. Otherwise the position of the first character of the match is returned.

* / public INT indexof (String s) {Return IndexOf (s, 0);}

/ ** Find The position for your string of a particular character, ignoring case.

This Method Is Not Case Snsitive. Searching Begins with The First Character in Your String.

@Param c The Character to search for.

@Return the position of the character thing matches c. -1 is returned if a matching character could not be found.

* / public int indexofignorecase (char c) {Return Indexofignorecase (C, 0);

/ ** Find the position for your string of a particular sequence of characters, ignoring case.

This method is not case sensitive. Searching begins with the first character in your string.

@param s A String object representing the character sequence to search for.

@return -1 is returned if a match could not be found Otherwise The position of the first character of the match is returned.

* / public int indexofignorecase (String s) {Return Indexofignorecase (s, 0);}

Public int lastindexof (int RETURNVAL = -1; if (len> 0) {INT POS = LEN - 1; Boolean Done = false; while (! done) {IF (POS <0) {DONE = true; } Else {if (buffy [pOS] == c) {returnval = POS; DONE = true;} else {POS-;}}}} returnval;}}

/ ** Does the string Object End with this bit of text.

@Param s the suffix you are looking for.

* / public boolean endswith (string s) {boolean returnval = true; if (s.Length ()> len) {returnval = false;} else {int buffypos = LEN - S.LENGTH (); for (int i = 0; i * / public int firstdigit () {int Returnval = - 1; int pos = 0; Boolean Done = False; While (! = len) {done = true;} else {char c = buffy [pOS]; if (NumBers.inrange (C, '0', '9')) { Returnval = POS; done = true;} else {POS ;}}} return returnval;}}

/ ** Replace All Instances of One Character with Another Character.

All Occurances Are Replaced.

Searching for the "Next Instance" of Character Will Begin Immedieration After The Last Replacement So That there is no chance of an infinite loop.

@Param c1 The Character to search for.

@Param C2 The Character That Is To Replace C1.

* / Public Void Replace (CHAR C1, CHAR C2) {INT I; for (i = 0; i < Len; i ) {if (buffy [i] == c1) {buffy [i] = c2;}}} // buy by the replace stuff private void copyspace, {INT i; for (i = 0; i

/ ** Replace All Instances of One Character with a string object.

All Occurances Are Replaced.

Searching for the "Next Instance" of Character Will Begin Immedieration After The Last Replacement So That there is no chance of an infinite loop.

@Param c the character to search for.

@Param s the string Object this is to replace C.

* / public void replace (CHAR C, STRING S) {INT POS = 0; if (s == NULL) {s = "";} while (pOS! = -1) {POS = IndexOf (C, POS); if (POS! = -1) {IF (S.Length ()> 1) {INSERTJUNK (S .leeness ()} else if (sLength () == 0) {Delete (POS, 1);} COPYSTRINGTO (S, POS); POS = S.LENGTH ();}} }

/ ** Search for a Particular Character Sequence and Replace It with one character.

all occurances are replaced.

Searching for the "Next Instance" of a character sequence will beg, immediately after the last replacement so what there is no chance of an infinite loop.

@Param s a string Object replanding a character sequence to search for.

@Param c the character That is to replace s.

* / public void replace (string s, char c) {int pos = 0; while (POS! = -1) {POS = IndexOf (S, POS); if (POS! = -1) {if (S.Length ()> 1) {Delete (POS, S.LENGTH () - 1); } Buffy [POS] = C; POS ;}}}

/ ** Search for a Particular Character Sequence and Replace It with a different character sequence.

All Occurances Are Replaced.

Searching for the "Next Instance" of a character sequence will beg, immediately after the last replacement so what there is no chance of an infinite loop.

@param s1 A String object representing a character sequence to search for.

@param s2 A String object representing a character sequence that is to replace s1.

* / public void replace (String s1, String s2) {if (S2 == NULL) {S2 = "";} INT POS = 0; While (POS! = -1) {POS = INDEXOF (S1, POS); if (POS! = -1) {IF (S1.Length () s2.length ()) {delete (POS, S1. Length () - s2.length ());} COPYSTRINGTO (S2, POS); POS = s2.length ();}}} // if Buffy is not big enough to hold Len, Buffy is Grown Private Void TestBuffylen INT newlen {if (newlen> buffy.length) {setcapa City (newlen extra);}}

// Initialize this string to be a substring of c private vidinitis {len = 0; if (index C.Length) {Length = C.Length - Index;} TestBuffylen (Length); INT i; for (i = 0; i

/ ** Eliminate All Occurances of Two More Space in a row.

When this method is done, there will not be an occurance of two spaces in a row. Anywhere that there was two or more spaces, there is now only one space.

* / public void removeDoubleSpaces () {boolean prevCharIsSpace = false ; Int 1; int 1; for (i = 0; i

As Dumb As this Sounds, The Method Does End Up Getting Used A Lot.

@Param C The Character to Look for.

@Return The Number of Times The Character Occured In this string.

* / public int charcount (char c) // How much Times does C Appear in this str {int Count = 0; INT i; for (i = 0; i

/ ** Trims All Leading Spaces, No Trailing Spaces.

* / public void Trimlead () {= 0) {INT i = 0; While ((i 0) {deletefirst (i);}}} / ** trims all trailing spaces, no letting spaces.

* / public void trimtrail () {ix> 0) {LEN -; while ((len> = 0) && (buffy [len] == ')) {len -;} len ;}}

/ ** Trims All Leading Spaces and All Trailing Spaces.

* / public void Trim () {Trimlead (); Trimtrail ();

Private static final string whitespace = "/ n / r / t";

/ ** Trims All Leading Spaces, NewLines, Returns and Tabs.

* / public void trimleadwhitespace () {IF (len> 0) {INT i = 0; While ((i 0) {DeleteFirst (i);}}}

/ ** Trims All Trailing Spaces, Newlines, Returns and Tabs.

* / public void trimtrailwhitespace () {if (len> 0) {le -; while ((len> = 0) && (Whitespace.indexof Buffy [len])! = -1)) {len-;} len ;}}

/ ** Trims all leading spaces, newlines, returns and tabs and all trailing spaces, newlines, returns and tabs

* / public void trimWhitespace (). {TrimLeadWhitespace (); trimTrailWhitespace ();} / ** Trims all characters From end of the string unient a particular character is encountered.

The Character Being Searched for Is Also Trimmed.

@Param C the Character to search for.

* / public void trimtrailto (char c) {if (len> 0) {len-; while ((len> 0) && (Buffy [len]! = c) ) {Len-;}}}

/ ** Extract and return the first word.

Space Delimited (All Double Spaces Are Removed).

@return A Str object representing the first word found in the string. No spaces are returned. If there are no words left, an empty Str object is returned.

* / public Str extractWord () {Str s = new Str ( ); If (len> 0) {RemoveDoubleSpaces (); Trim (); int pos = indexof (''); if (pOS == -1) {s.set (this); len = 0;} else {s .init (Buffy, 0, POS); deletefirst (POS 1);}}

/ ** Convert this str ject to a string object.

@Return a string Object with an exact copy of your string.

* / public string toString () {Return New String (Buffy, 0, Len);

/ ** Force String to be the first number found.

Everything this is not a number, period or hyphen is involved to a space. The resulting string is the first word extracted.

* / public void forcenumeric () {INT i; for (i = 0; i

/ ** TRY TO Convert this string to an integer value.

@Return Zero if the value is supposed to be zero or if there is any problem () {i = integer.valueof ()) .intValueOf ()) .intValue () ;} CatCh (NumberFormatexception E) {} return i;}

/ ** TRY TO Convert this String to a double value.

@Return Zero if the value is supposed to be zero or if there is any problem.

* / public double todouble () {double d = 0.0; try {d = double.valueof ()) .doublevalue () ;} CatCH (Numberformatexception E) {} return d;}

/ ** Report How Many Leading Spaces There Are.

@Return the Number of Leading Spaces in Your String.

* / public int = 0; while ((i

/ ** Report How Many Trailing Spaces there is.

@Return The Number of Trailing Spaces in Your String.

* / public int rightspacecount () {INT i = LEN - 1; while ((i> 0) && (Buffy [i] == ')) {i -;} RETURN ((LEN - I) - 1);} / ** force the length of the string and keep text to the left.

If The New Length Is Longer, Spaces Will Be Added To The Right.

if The New Length Is Shorter, Characters Are Chopped Off from The Right.

Useful for formatting text to be left justified.

@Param Newlen THE Desired New Length.

* / public void left (int newlen) {IF (NEWLEN) {TestBuffylen (Newlen); INT i; for (i = le; i

/ ** Force the length of the string and key text to the right.

If The New Length Is Longer, Spaces Will Be Added To The Left.

if The New Length Is Shorter, Characters Are Chopped Off The Right (Not The Left).

Useful for formatting text to be right justified.

@Param new release,

* / public void right (int newlen) {if (len

/ ** Try to center the text in a pad of spaces.

first, all leading and trailing spaces are removed.

IF the new length is longer, spaces will be added to the left and right.

ife, the resulting string. In this case, the resulting string is longer than the new length!

Useful for formatting text to be centered.

@Param new release,

* / public void center (int newlen) {TRIM (); if (newlen> len) {Int totalspaces = newlen - len; int = Totalspaces / 2; int rightspaces = Totalspaces - Leftspaces; Right (Len Rightspaces); Left (Len Leftspaces);}}

/ *** All Characters Are Forced to Upper Case.

* / public void tour () {INT i; for (i = 0; i = 'a') && (c <= 'z')) {buffy [i] = (char) (c - 32);}}}

/ *** All Characters Are Forced to Lower Case.

* / public void tolower () {INT i; for (i = 0; i = 'a')) {buffy [i] = (char) (C 32);}}}

/ ** Count the number of trailing digits.

ONLY VALUES '0' Through '9' Are Considered.

* / public int trackingdigits () {int count = 0; int index = length () - 1; boolean done = false; while (! Done) {IF INDEX <0) {DONE = true;} else {char c = get (index); if (NumBers.inrange (C, '0', '9')) {count ; index -;} else {done = true ;}}} Return count;} / ** Reverse the Order of all the characters.

Designed to Behave The Same Way As StringBuffer.Reverse ().

* / public void revrse () {int halfway = len / 2; int lastchar = len - 1;

// swap the the last chars for the first for (int i = 0; i

/// static methods

/ ** Converts a Double to a string with two decimal places.

*

 * example: in out * * 0.0 "0.00" * 1.9999 "2.00" * 222.2222 "222.22" * 

@Param D The double value.

@return a string Object with the formated number.

* / public static string MoneyStr (double d) // usees the format ############ 0.00 and then trims spaces {decimalformat f = new decimalformat ("############# 0.00"); returnif.format (d) .trim ();} / ** Converts a double to a String with a specified Number of Decimal Places.

@param d The double value.

@param decimalPlaces The number of decimal places needed.

@return A String object with the formated number.

* / public static String formatDouble (double d, int decimalPlaces) { String format = "########### 0." Stringof (Decimalplaces, '0'); decimalformat f = new decimalformat (format); returnif.Format d) .trim ();

/ ** Converts a double to a string with commas inserted for Thousands, Millions, etc.

Fractional Values ​​Are Ignored.

@Param Val the double value.

@Return a string Object with the formated number.

* / public static string commstr (double val) {decimalformat f = new decimalformat ("###, ###, # ##, ## 0 "); return f.format (val) .trim ();

Private static string [] Numstr = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Eight", "Nine", "Ten "," Eleven "," Twelve "," Thirteen "," Fifteen "," Sixteen "," Seventeen "," EIGHTEEN "," Nineteen "}; private static string [] tee = {" twenty " , "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "EIGHTY", "Ninety"};

/ ** Like the ANSI C Function ATOI ().

@Param s the string That Begins with a Number.

@Return the integer.

* / static public int atoi (string s) {INT i = 0; try { i = integer.valueof (s) .intValue ();} catch (number}) {} returni}

/ ** Sort of Like the ANSI C Function ATOI ().

Instead of Passing in a string, pass in a character.

@Param s the string That Begins with a number.

@Return The Integer That Was Extracted (0 THROUGH 9) OR 0 if any problems were encountered.

* / static public int atoi (char c) {int ReturnVal = 0; int CHARVAL = (int) C; Final Int Zeroval = (int) '0'; Final Int Nineval = (int) '9'; if (Charval> Zeroval) && (Charval <= nineval)) {ReturnVal = Charval - zeroval;} Return ReturnVal;

/ ** Like the ANSI C Function Atod ().

@Param S The String That Begins with a Number.

@return the double thing. @Retur 0.0 if anyproblems were encountered.

* / static Public double atod (string s) // ascii to double {double d = 0.0; try {d = double.parsedouble (s);} catch (exception e) {//system.out.println (} return d) }

// use by the verbosemoney () Method Private static str {// pass in a number That is one to three digits str s = new str (); // return value if (n.Length () = = 3) {char C = N.GET (0); if (c! = '0') {S.Append (NumStr [ATOI (C)]); S.Append ("hundred");} n.deletefirst (1);} if (n.Length () == 2) {char c = N.GET (0); if (c == '0') {n.deletefirst (1);} else {IF (c == '1') {S.Append (NumStr [n.toint ()]);} else {S.Append (Tee [atoi (c) - 2]); if (n.get (1)! = ' 0 ') {S.Append (' - '); n.deletefirst (1); }}}}}} {INT i = ATOI (N.Get (0)); if (i> 0) {S.Append (Numstr [i]);}}}} Return S } / ** Convert a double to English (12.01 -> "Twelve and 01/100").

*

 * Examples: In Out * * 0.0 "zero and 00/100" * 23.23 "twenty-three and 23/100" * 12345678.90 "twelve million three hundred forty-five thousand six hundred seventy-eight and 90/100 "*  @Param x the double to control. 

@Return the str/biting the results.

* / public static str verbosemoney (double x) {str n = new str (MoneyStr (x) ); Int decimalpos = N.LENGTH () - 3; Str Cents = n.after (decimalpos); n = n.before (decimalpos); str returnval = new str (); if (n.Length ()> 6) {Int millionlen = N.LENGTH () - 6; ReturnVal.Append (LittleMoney (N.Before (Millionlen))); ReturnVal.Append ("Million"); n = n.from (Millionlen);} if (n. Length ()> 3) {Int thousandlen = N.LENGTH () - 3; ReturnVal.Append (Littlemone y (n.before (stayandlen))); returnval.append ("thousand"); n = n.from (thousandlen);} returnval.append (LittleMoney (N)); ReturnVal.Append ("and" CENTS "/ 100"); return returnction;

/ ** Get The Current Local Date and Time.

@Return a string Object with Time.

* / public static string nowStr () {simpledateformat f = new simpledateformat ("YYYY.MM.DD EEE HH: MM: SS"); Return F.Format NEW date ());

/ ** Create A Str Object Containing Nothing But a Quantity of One Character.

EXAMPLE: CALLING STRINGOF (20, '-') Returns A String of 20 Hyphens.

@Param quan the desired string length.

@Param c the desired character.

@Return the new str in

* / public static stringof (int quever, char c) {str retrunval = new STR (quan); if (quan> 0) {INT i; for (i = 0; i

/ ** CREATE A STR Object Containing Nothing But A Quantity of Spaces.

@Param quan the number of spaces desired.

* / public static string {return stringof (quan, '');}

/ ** a static sub-string method That Works the Way You Expect It to work.

The Java Substring Thing IS, IMO, Twisted. This is the Normal Way to Do Substrings. Plus, this Object Makes The Best of awkward requests without throwing an exception.

@Param s a string Object That You Want to Extract a sub-string from.

@Param INDEX WHERE The SUB-STRING BEGINS.

@Param Length How long you want your sub-string to be.

* / Public static string subString (string s, int index, int length) {string returnval = ""; if (index = S.LENGTH )) {RETURNVAL = S.SUBSTRING (INDEX);} else {returnval = s.substring (index, rightindex);}} Return ReturnVal;} / ** Same As Substring, But Allows All Lower Case.

* / Public Static String Substring (String S, INDEX, INTLENGTH) {Return Substring (S, INDEX, LENGTH);

/ ** Get all of the substring That Occurs Before a particular character.

@param s The String object to extract the substring from.

@param index The character that marks the end of the substring. This character is not included in the substring.

@return A String object containing the substring. < P> * / public static string before (string s, int index) {return substring (s, 0, index);

/ ** Get All of the substring That Occurs through a particular character.

@param s The String object to extract the substring from.

@param index The character that marks the end of the substring. This character is included in the substring.

@return A String object containing the substring.

* / Public static string through (string s, int index) {return substring (s, 0, index);} / ** get all of the substring That Occurs After A Particular Character.

@param s The String object to extract the substring from.

@param index The character that marks the beginning of the substring. This character is not included in the substring.

@return A String object containing the substring. < P> * / public static string after (String S, int index) {Return Substring (s, index 1, (s.Length () - index) - 1);}

/ ** get all of the substring That Occurs from a particular character to the end of the string.

@param s The String object to extract the substring from.

@param index The character that marks the beginning of the substring. This character is included in the substring.

@return A String object containing the substring.

* / Public static string from (string s, int index) {return substring (s, index, s.length () - index);

/ ** Force the length of the string and keep text to the left.

If The New Length Is Longer, Spaces Will Be Added To The Right.

if The New Length Is Shorter, Characters Are Chopped Off from The Right.

Useful for formatting text to be left justified.

@Param s the string object to process.

@Param Newlen the desired new length.

@return the new string object.

* / static public string left (string s, int newlen) {str temp = NEW STR (s); Temp.Left (newlen); return temp.tostring ();} / ** force the length of the string and keep text to the right.

If The New Length Is Longer, Spaces Will Be Added To The Left.

if The New Length Is Shorter, Characters Are Chopped Off The Right (Not The Left).

Useful for formatting text to be right justified.

@Param s the string object to process.

@Param Newlen the desired new length.

@Return the new string object.

* / static public string right (string s, int newlen) {str temp = NEW STR (s); Temp.right (newlen); return temp.tostring ();

/ ** Try to center the text in a pad of spaces.

First, All Leading and Trailing Spaces Are Removed.

IF the new length is longer, spaces will be added to the left and right.

ife, the resulting string. In this case, the resulting string is longer than the new length!

Useful for formatting text to be centered.

@Param s the string object to process.

@P> @return the new string Object.

* / static public string center (string s, int newlen) {str temp = NEW STR (s); Temp.center (Newlen); return temp.tostring ();

/ ** Count the number of trailing digits.

Only Values ​​'0' Through '9' Are Considered.

* / static public int trackingdigits (string s) {int count = 0; int index = S.LENGTH () - 1; Boolean Done = false; while (! "{Done = true;} else {char c = s.charat (index); if (NumBers.inrange (C, '0 ',' 9 ')) {count ; index -;} else {done = true;}}} return


New Post(0)