Home > Java > Some simple String manipulation functions

Some simple String manipulation functions

In the Joys of String (Part 1) I discussed the sort of structure one might adopt when looking to customise and extend String manipulation functionality. In this article I am going to propose some of the methods and things which you could do to extend that functionality. In this article we’ll start with some simple time-savers.

String enquoting

Frequently I find myself having to build sql strings which are explicitly enquoted programmatically. To save the tiresomeness of having to remember exactly how to escape and quote this,  I have a very simple method called enquote which I pass sql statements to:

/*
   * Surrounds a string in quotations e.g. for sql inserts
    * @param sIn - String to enquote
    * @return enquoted string
    */
   public String enquote(String sIn)
   {
   String sEnquote = "\"";
   return sEnquote + sIn + sEnquote;
   }

Splitting a multiline String to an array of Strings & split by tokens

This like many of the very simple utilities in this article is a natural but having it all in a method saves a bit of remembering & typing:

/*
   * Splits a string with mulitple lines into an array, one element per string
    * @param sMultiline - String to split
    * @return array of strings, one element for each string
    */
public String[] splitToArray(String sString)
{
return sString.split("\n");
}

Obviously this could be overloaded to take an additional argument for a different end of line character. or for e.g. a single line split on an entirely different token:

/*
   * Splits a string with mulitple contending elements into an array, one element per string
    * @param sString - String to split
    * @param sToken - token to split the string on
    * @return array of strings, one element for each string
    */
public String[] splitToArray(String sString, String sToken)
{
return sString.split(sToken);
}

Handling String arrays

This is potentially a very complex subject and I’ll save the real meat of this until a later article, however, having split our String to an array, we may like to see its contents.

/** Loops through a passed String array and outputs its contents
 *@param sStringArray - the String array to output
 * @return N/A
 */
public void stringArrayPrinter(String[] sStringArray)
{
for (int i = 0; i < sStringArray.length; i++)
{
System.out.println(sStringArray&#91;i&#93;);
}
}&#91;/sourcecode&#93;

Here we have a simple <em>for</em> loop traversing each element in the array and at each loop iteration the appropriate element is passed to System.out.println.

<strong>Matching Strings for equality</strong>

A simple if based method to determine whether two strings are identical.


/** checks a string against a regexp and returns true/false
 *@param sString - the string to examine
 *@param sCompare - string to check against
 * @return true if matched or false if not
 */
public boolean bStringMatch(String sString, String sCompare)
{
if(sString.equals(sCompare) ) return true;
return false;
}

However a more efficient way of doing this is to use the == comparison, as opposed to the relatively expensive String.equals() method:

/** checks a string against a regexp and returns true/false
 *@param sString - the string to examine
 *@param sCompare - string to check against
 * @return true if matched or false if not
 */
public boolean bStringMatch(String sString, String sCompare)
{
if(sString == sCompare ) return true;
return false;
}

Less efficiently, but more interestingly, you could alternatively code this as a regexp condition as follows, however, it would run marginally slower (imperceptibly so in the normal run of things but what if you had hundreds of thousands of Strings to examine?). You will also have to add the following imports

import java.util.regex.Matcher;
import java.util.regex.Pattern;

to your class header.

/** checks a string against a regexp and returns true/fale
 *@param s - the string to examine
 *@param regexp - string to run as a regexp pattern
 * @return true or false if found or not
 */
public boolean bStringMatch(String sString, String regexp)
{
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(sString);
return m.matches();
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: