comtest
Class StringUtilities

java.lang.Object
  extended by comtest.StringUtilities

public class StringUtilities
extends java.lang.Object

A collection of static string handling functions to make the string handling in ComTest more easy.

Version:
22.9.2007
Author:
vesal

Field Summary
static java.lang.String QUOTES
          Chars that are handled as quote chars
 
Constructor Summary
StringUtilities()
           
 
Method Summary
static java.lang.String addToName(java.lang.String fileName, java.lang.String newPart)
          Adds a new part to filename
static int countEndIndex(java.lang.String start, java.lang.String end)
          Counts the index in ending word when the builded string should contain end-string as last characters.
static Strings getFileContents(java.lang.String fileName)
          Read the file and return the contents as a ArrayList
static java.lang.String getLineIndent(java.lang.String line, int pos)
          Returns indent string copied from line.
static int indexOfBack(java.lang.String s, java.lang.String what, int pos)
          Searches what backwards from pos and returns position found
static int indexOfNotInQuotes(java.lang.String s, java.lang.String what)
          Find first instance of what that is not inside of quotes
static int indexOfNotInQuotes(java.lang.String s, java.lang.String what, java.lang.String quote)
          Find first instance of what that is not inside of quotes
static int indexOfNotInQuotes(java.lang.String s, java.lang.String what, java.lang.String quote, int pos)
          Find first instance of what that is not inside of quotes
static boolean isEmpty(java.lang.String s)
          Check if string is empty
static java.lang.String justFilePart(java.lang.String fileName)
          Returns just the file part from fileName.
static java.lang.String remove(java.lang.String s, java.lang.String what)
          Removes from s the first instance of what
static java.lang.String removeAllAfter(java.lang.String s, java.lang.String afterWhat)
          Returns a string where all after afterWhat string is removed Also the afterWhat is removed.
static java.lang.String removeFromBegining(java.lang.String s, java.lang.String what)
          Removes all instances of what from the beginning of s
static java.lang.String removeFromBegining(java.lang.String s, java.lang.String what, int n)
          Removes n instances of what from the beginning of s
static java.lang.String removeFromBeginingTrim(java.lang.String s, java.lang.String what)
          Removes all instances of what from the beginning of s If there is only spaces before s, then remove them also.
static Strings splitBy(java.lang.String s, java.lang.String where)
          Splits s to pieces from where.
static Strings splitBy(java.lang.String s, java.lang.String where, int jump)
          Splits s to pieces from where.
static Strings splitBy(java.lang.String s, java.lang.String where, int jump, boolean trim)
          Splits s to pieces from where.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

QUOTES

public static final java.lang.String QUOTES
Chars that are handled as quote chars

See Also:
Constant Field Values
Constructor Detail

StringUtilities

public StringUtilities()
Method Detail

splitBy

public static Strings splitBy(java.lang.String s,
                              java.lang.String where,
                              int jump,
                              boolean trim)
Splits s to pieces from where. Quotes is taken account so where inside quotes, then it is ignored.

Parameters:
s - string to split
where - from where to split
jump - how much delete from each part
trim - to trim or not the parts
Returns:
list of parts
Example:
 String s = "first; second;\"third;3\";4;";
 Strings p = splitBy(s,";",1);
 p.size() === 5
 p.get(0) === "first"; p.get(1) === " second"; p.get(2) === "\"third;3\""
 p.get(3) === "4";     p.get(4) === ""
 
 s = "first; second;\"third;3\";4";
 p = splitBy(s,";",0,true);
 p.size() === 4
 p.get(0) === "first;"
 p.get(1) === "second;"
 p.get(2) === "\"third;3\";"
 p.get(3) === "4"
 

splitBy

public static Strings splitBy(java.lang.String s,
                              java.lang.String where,
                              int jump)
Splits s to pieces from where. Quotes is taken account so where inside quotes, then it is ignored.

Parameters:
s - string to split
where - from where to split
jump - how much delete from each part
Returns:
list of parts

splitBy

public static Strings splitBy(java.lang.String s,
                              java.lang.String where)
Splits s to pieces from where. Quotes is taken account so where inside quotes, then it is ignored.

Parameters:
s - string to split
where - from where to split
Returns:
list of parts
Example:
 Strings p;
 p = splitBy("one|two|three","|");
 p.size() === 3;
 p.get(0) === "one";
 p.get(1) === "two";
 p.get(2) === "three";
 
 p = splitBy("one|","|");
 p.size() === 2;
 p.get(0) === "one";
 p.get(1) === "";
 
 p = splitBy("one","|");
 p.size() === 1;
 p.get(0) === "one";

 p = splitBy("","|");
 p.size() === 1;
 p.get(0) === "";

 

indexOfNotInQuotes

public static int indexOfNotInQuotes(java.lang.String s,
                                     java.lang.String what,
                                     java.lang.String quote,
                                     int pos)
Find first instance of what that is not inside of quotes

Parameters:
s - from where to find
what - string to find
quote - what chars are used as a quote
pos - position where to start
Returns:
index of first occurrence or -1 if not found

indexOfNotInQuotes

public static int indexOfNotInQuotes(java.lang.String s,
                                     java.lang.String what)
Find first instance of what that is not inside of quotes

Parameters:
s - from where to find
what - string to find
Returns:
index of first occurrence or -1 if not found

indexOfNotInQuotes

public static int indexOfNotInQuotes(java.lang.String s,
                                     java.lang.String what,
                                     java.lang.String quote)
Find first instance of what that is not inside of quotes

Parameters:
s - from where to find
what - string to find
quote - what chars are used as a quote
Returns:
index of first occurrence or -1 if not found
Example:
 indexOfNotInQuotes("a = a+1; // add one ", "//","\"")        === 9  
 indexOfNotInQuotes("a = a+1; ", "//","\"")                   === -1  
 indexOfNotInQuotes("s = \"//\"; // s is comment", "//","\"") === 10  
 indexOfNotInQuotes("s = \"//\"; ", "//","\"")                === -1  
 indexOfNotInQuotes("s = \"a\";  // \"//\" ", "//","\"")      === 10  
 indexOfNotInQuotes("s = \"a\";  // \"//\" ", "\"","\"")      === 4  
 indexOfNotInQuotes("s = \"a\";  // \"//\" ", "a","")         === 5  
 indexOfNotInQuotes("s = \"a\"; c='a'; d=a; ", "a","\"'")     === 18  
 

removeFromBegining

public static java.lang.String removeFromBegining(java.lang.String s,
                                                  java.lang.String what,
                                                  int n)
Removes n instances of what from the beginning of s

Parameters:
s - where to remove
what - string what to remove
n - how many times to remove
Returns:
string without what in the beginning
Example:
 removeFromBegining("* a=a+1;","*",2)      === " a=a+1;"  
 removeFromBegining("*** a=a+1;","*",2)    === "* a=a+1;"  
 removeFromBegining("* ** a=a+1;","*",2)   === " ** a=a+1;"  
 

removeFromBegining

public static java.lang.String removeFromBegining(java.lang.String s,
                                                  java.lang.String what)
Removes all instances of what from the beginning of s

Parameters:
s - where to remove
what - string what to remove
Returns:
string without what in the beginning
Example:
 removeFromBegining("* a=a+1;","*")      === " a=a+1;"  
 removeFromBegining("*** a=a+1;","*")    === " a=a+1;"  
 removeFromBegining("* ** a=a+1;","*")   === " ** a=a+1;"  
 

removeFromBeginingTrim

public static java.lang.String removeFromBeginingTrim(java.lang.String s,
                                                      java.lang.String what)
Removes all instances of what from the beginning of s If there is only spaces before s, then remove them also.

Parameters:
s - where to remove
what - string what to remove
Returns:
string without what in the beginning
Example:
 removeFromBeginingTrim("  * a=a+1;","*")      === " a=a+1;"  
 removeFromBeginingTrim("  *** a=a+1;","*")    === " a=a+1;"  
 removeFromBeginingTrim("  * ** a=a+1;","*")   === " ** a=a+1;"  
 removeFromBeginingTrim("  a=a+1;","*")        === "  a=a+1;"  
 

removeAllAfter

public static java.lang.String removeAllAfter(java.lang.String s,
                                              java.lang.String afterWhat)
Returns a string where all after afterWhat string is removed Also the afterWhat is removed. If afterWhat is inside quotes, then it is not affected.

Parameters:
s - string to handle
afterWhat - string to look for
Returns:
the handled string
Example:
 removeAllAfter("a = a+1; // add one ", "//")         === "a = a+1; "  
 removeAllAfter("a = a+1; ","//")                     === "a = a+1; "  
 removeAllAfter("s = \"//\"; // s is comment", "//" ) === "s = \"//\"; "  
 removeAllAfter("s = \"//\"; " ,"//")                 === "s = \"//\"; "  
 removeAllAfter("s = \"a\";  // \"//\" " ,"//")       === "s = \"a\";  "  
 

addToName

public static java.lang.String addToName(java.lang.String fileName,
                                         java.lang.String newPart)
Adds a new part to filename

Parameters:
fileName - filename to change
newPart - part to add
Returns:
filename + newPart
Example:
 addToName("my.java","Test") === "myTest.java"
 addToName("my","Test") === "myTest"
 

justFilePart

public static java.lang.String justFilePart(java.lang.String fileName)
Returns just the file part from fileName. No path, no extension

Parameters:
fileName -
Returns:
only file name part from fileName
Example:
testJustFilePart
 justFilePart("myTest.java")                     === "myTest"
 justFilePart("myTest")                          === "myTest"
 justFilePart("/users/my/java/myTest.java")      === "myTest"
 justFilePart("/users/my.prg/java/myTest.java")  === "myTest"
 justFilePart("/users/my.prg/java/myTest")       === "myTest"
 justFilePart("/users/my.prg/java/myTest/")      === ""
 justFilePart("e:\\users\\my.prg\\java\\myTest") === "myTest"
 justFilePart("\\users\\my.prg\\java\\myTest")   === "myTest"
 justFilePart("e:myTest.java")                   === "myTest"
 

remove

public static java.lang.String remove(java.lang.String s,
                                      java.lang.String what)
Removes from s the first instance of what

Parameters:
s - where to remove
what - what string to remove
Returns:
s without first instance on what
Example:
 remove($s,$what) === $result;
 
   $s        |  $what |  $result 
 ----------------------------------
 "aab"       |  "a"   |  "ab"
 "bab"       |  "a"   |  "bb"
 "bba"       |  "a"   |  "bb"
 
,
 remove($s,$what) === $result;
 
   $s        |  $what |  $result 
 ----------------------------------
 "\"aa\"b"   |  "a"   |  "\"aa\"b"
 "\"a\"ab"   |  "a"   |  "\"a\"b"
 

getFileContents

public static Strings getFileContents(java.lang.String fileName)
Read the file and return the contents as a ArrayList

Parameters:
fileName - the file name to read
Returns:
null if problems, otherwise the file contents
Example:
 Strings list = getFileContents("comtest/StringUtilities.java");
 int lastn = list.size()-1;
 lastn > 200 === true;
 list.get(0) === "package comtest;"; 
 list.get(lastn) === "}";
 list = getFileContents("comtest/StringUtilitiesXYZ.java");
 list === null;
 
 

countEndIndex

public static int countEndIndex(java.lang.String start,
                                java.lang.String end)
Counts the index in ending word when the builded string should contain end-string as last characters.

Parameters:
start - The starting string.
end - The required ending.
Returns:
The starting index where end-string should be inserted after the start.
Example:
 countEndIndex("#STATIC","#STATICIMPORT") === 7;
 countEndIndex("ABCD","1234") === 0;
 countEndIndex("ABCD","ABCD") === 4;
 countEndIndex("1234567890#STATIC","#STATICIMPORT") === 7;
 
 

indexOfBack

public static int indexOfBack(java.lang.String s,
                              java.lang.String what,
                              int pos)
Searches what backwards from pos and returns position found

Parameters:
s - where to search
what - to search
pos - where to start search
Returns:
position found or -1 if not found
Example:
   indexOfBack("0123456789","4",8)  === 4;
   indexOfBack("0123456789","9",8)  === -1;
   indexOfBack("0123456789","4",4)  === 4;
   indexOfBack("0123456789","4",3)  === -1;
   indexOfBack(null,"4",5)          === -1;
   indexOfBack(null,null,5)         === -1;
   indexOfBack("012345678",null,5)  === -1;
   indexOfBack("0123456789","4",18) === 4;
   indexOfBack("0123456789","",18)  === 10;
   indexOfBack("0123456789","",5)   === 5;
   indexOfBack("0123456789","abc",15) === -1;
 

getLineIndent

public static java.lang.String getLineIndent(java.lang.String line,
                                             int pos)
Returns indent string copied from line. Search for line is started backwards from pos.

Parameters:
line - line to look indent template
pos - where to start looking
Returns:
the same indent that current line has
Example:
   getLineIndent("first\n    second\n",15)  === "    ";   
   getLineIndent("first\n    second\n",5)   === "    ";   
   getLineIndent("first\n    second\n",4)   === "";   
   getLineIndent("  first\n    second\n",6) === "  ";   
 

isEmpty

public static boolean isEmpty(java.lang.String s)
Check if string is empty

Parameters:
s - string to check
Returns:
true if null or just spaces or tabs, false in other case
Example:
   isEmpty(null)   === true;
   isEmpty("")     === true;
   isEmpty(" ")    === true;
   isEmpty("a")    === false;
   isEmpty("\n")   === false;