Use String Constructors: Public Class Public Static Void Char New
Use String Constructors: Public Class Public Static Void Char New
class MainClass
{
public static void main( String args[] )
{
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
String s = new String( "hello" );
// use String constructors
String s1 = new String();
String s2 = new String( s );
String s3 = new String( charArray );
String s4 = new String( charArray, 6, 3 );
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n",
s1, s2, s3, s4 );
}
}
s1 =
s2 = hello
s3 = birth day
s4 = day
2.
public class MainClass
{
public static void main( String args[] )
{
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
String s = new String( "hello" );
// use String constructors
String s1 = new String();
String s2 = new String( s );
String s3 = new String( charArray );
String s4 = new String( charArray, 6, 3 );
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n",
s1, s2, s3, s4 );
}
}
s1 =
s2 = hello
s3 = birth day
s4 = day
3.
public class StringDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
4.
public class Main {
public static void main(String[] args) {
String name = "Java";
int length = name.length();
System.out.println("Length = " + length);
}
}
5.
public class MainClass {
public static void main(String[] arg) {
String s = null;
System.out.println(s);
}
6.
public class MainClass {
public static void main(String[] arg) {
String s = null;
System.out.println(s.length());
}
}
Exception in thread "main" java.lang.NullPointerException
at MainClass.main(MainClass.java:6)
7.
public class MainClass
{
public static void main( String args[] )
{
String s1 = new String( "hello" );
String s2 = new String( "GOODBYE" );
String s3 = new String( " spaces " );
System.out.printf( "s1 = %s\ns2 = %s\ns3 = %s\n\n", s1, s2, s3 );
// test toLowerCase and toUpperCase
System.out.printf( "s1.toUpperCase() = %s\n", s1.toUpperCase() );
System.out.printf( "s2.toLowerCase() = %s\n\n", s2.toLowerCase() );
}
}
s1 = hello
s2 = GOODBYE
s3 = spaces
s1.toUpperCase() = HELLO
s2.toLowerCase() = goodbye
8.
public class MainClass {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
if (s1.equals(s2)) {
System.out.println("==");
}
}
}
if ("Java".equals (s1))
In the following code, if s1 is null, the if statement will return false without evaluating the second
expression.
if (s1 != null && s1.equals("Java"))
9.
public class MainClass{
public static void main(String[] a){
String s = "John \"The Great\" Monroe";
}
10.
public class MainClass {
public static void main(String[] arg) {
String[] names = new String[5];
names[0] = "qwert";
names[1] = "yuiop";
names[2] = "asdfg";
names[3] = "hjkl";
names[4] = "zxcvb";
System.out.println(names[4]);
}
11.
public class MainClass {
public static void main(String[] arg) {
String[] colors = {"red", "orange", "yellow", "green", "blue", "indigo", "
violet"};
for(String s: colors){
System.out.println(s);
}
}
}
red
orange
yellow
green
blue
indigo
violet
12.
public class MainClass
{
public static void main( String args[] )
{
String letters = "abcdefghijklmabcdefghijklm";
// test substring methods
System.out.printf( "Substring from index 20 to end is \"%s\"\n",
letters.substring( 20 ) );
System.out.printf( "%s \"%s\"\n",
"Substring from index 3 up to, but not including 6 is",
letters.substring( 3, 6 ) );
} // end main
}
Substring from index 20 to end is "hijklm"
Substring from index 3 up to, but not including 6 is "def"
13.
2. 19. 14. String Concatenation
public class MainClass
{
public static void main( String args[] )
{
String s1 = new String( "Happy " );
String s2 = new String( "Birthday" );
System.out.printf( "s1 = %s\ns2 = %s\n\n",s1, s2 );
System.out.printf(
"Result of s1.concat( s2 ) = %s\n", s1.concat( s2 ) );
System.out.printf( "s1 after concatenation = %s\n", s1 );
} // end main
}
s1 = Happy
s2 = Birthday
Result of s1.concat( s2 ) = Happy Birthday
s1 after concatenation = Happy
14.
public class MainClass
{
public static void main( String args[] )
{
String letters = "abcdefghijklmabcdefghijklm";
// test substring methods
System.out.printf( "Substring from index 20 to end is \"%s\"\n",
letters.substring( 20 ) );
System.out.printf( "%s \"%s\"\n",
"Substring from index 3 up to, but not including 6 is",
letters.substring( 3, 6 ) );
} // end main
}
Substring from index 20 to end is "hijklm"
Substring from index 3 up to, but not including 6 is "def"
15.
2. 19. 14. String Concatenation
public class MainClass
{
public static void main( String args[] )
{
String s1 = new String( "Happy " );
String s2 = new String( "Birthday" );
System.out.printf( "s1 = %s\ns2 = %s\n\n",s1, s2 );
System.out.printf(
"Result of s1.concat( s2 ) = %s\n", s1.concat( s2 ) );
System.out.printf( "s1 after concatenation = %s\n", s1 );
} // end main
}
s1 = Happy
s2 = Birthday
16.
2. 19. 16. Using trim() to process commands.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class UseTrim {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter 'stop' to quit.");
System.out.println("Enter letter: ");
do {
str = br.readLine();
str = str.trim();
if (str.equals("I"))
System.out.println("I");
else if (str.equals("M"))
System.out.println("M");
else if (str.equals("C"))
System.out.println("C.");
else if (str.equals("W"))
System.out.println("W");
} while (!str.equals("stop"));
}
}
17.
public class Main {
public static void main(String[] args) {
String text = " t ";
System.out.println("Result: " + text.trim());
}
}
18.
public class Main {
public static void main(String args[]) {
String str = "this is a test";
System.out.println(removeChar(str,'s'));
}
public static String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != c)
r += s.charAt(i);
}
return r;
}
}
//thi i a tet
19.
public class Main {
public static void main(String args[]) {
String str = "this is a test";
System.out.println(removeCharAt(str, 3));
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
}
20.
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = "asdf".getBytes("UTF8");
new ByteArrayInputStream(bytes);
}
}
21.
class ChangeCase {
public static void main(String args[])
{
String s = "This is a test.";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}
22
public class Main {
public static void main(String[] argv) throws Exception {
String s1 = "s1";
StringBuffer sbuf = new StringBuffer("a");
boolean b = s1.contentEquals(sbuf);
}
}
23.
public class MainClass {
public static void main(String[] arg) {
String string1 = "abcde";
if(string1.startsWith("ab")) {
System.out.println("starts with ab");
}
}
}
starts with ab
24.
public class MainClass {
public static void main(String[] arg) {
String string1 = "abcde";
if(string1.endsWith("de")) {
System.out.println("ends with de");
}
}
}
ends with de
25
public class MainClass{
public static void main(String[] arg){
String str = "abcde";
System.out.println(str.charAt(2));
}
}
c
26.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.StringTokenizer;
/**
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @version $Id: StringUtils.java 685685 2008-08-13 21:43:27Z nbubna $
*/
public class Main {
/**
* Line separator for the OS we are operating on.
*/
private static final String EOL = System.getProperty("line.separator");
/**
* Chop i characters off the end of a string.
* This method assumes that any EOL characters in String s
* and the platform EOL will be the same.
* A 2 character EOL will count as 1 character.
*
* @param s String to chop.
* @param i Number of characters to chop.
* @return String with processed answer.
*/
public static String chop(String s, int i)
{
return chop(s, i, EOL);
}
/**
* Chop i characters off the end of a string.
* A 2 character EOL will count as 1 character.
*
* @param s String to chop.
* @param i Number of characters to chop.
* @param eol A String representing the EOL (end of line).
* @return String with processed answer.
*/
public static String chop(String s, int i, String eol)
{
if ( i == 0 || s == null || eol == null )
{
return s;
}
int length = s.length();
/*
* if it is a 2 char EOL and the string ends with
* it, nip it off. The EOL in this case is treated like 1 character
*/
if ( eol.length() == 2 && s.endsWith(eol ))
{
length -= 2;
i -= 1;
}
if ( i > 0)
{
length -= i;
}
if ( length < 0)
{
length = 0;
}
return s.substring( 0, length);
}
}
27.
2. 20. 5. Removes separator from the end of str if it's there, otherwise leave it alone.
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
public class Main {
/**
* Removes <code>separator</code> from the end of
* <code>str</code> if it's there, otherwise leave it alone.
*
* NOTE: This method changed in version 2.0.
* It now more closely matches Perl chomp.
* For the previous behavior, use {@link #substringBeforeLast(String, String
)}.
* This method uses {@link String#endsWith(String)}.
*
* <pre>
* StringUtils.chomp(null, *) = null
* StringUtils.chomp("", *) = ""
* StringUtils.chomp("foobar", "bar") = "foo"
* StringUtils.chomp("foobar", "baz") = "foobar"
* StringUtils.chomp("foo", "foo") = ""
* StringUtils.chomp("foo ", "foo") = "foo "
* StringUtils.chomp(" foo", "foo") = " "
* StringUtils.chomp("foo", "foooo") = "foo"
* StringUtils.chomp("foo", "") = "foo"
* StringUtils.chomp("foo", null) = "foo"
* </pre>
*
* @param str the String to chomp from, may be null
* @param separator separator String, may be null
* @return String without trailing separator, <code>null</code> if null Stri
ng input
*/
public static String chomp(String str, String separator) {
if (isEmpty(str) || separator == null) {
return str;
}
if (str.endsWith(separator)) {
return str.substring(0, str.length() - separator.length());
}
return str;
}
// Empty checks
//-----------------------------------------------------------------------
/**
* Checks if a String is empty ("") or null.
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}
28.
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
public class Main {
/**
* <code>\u000a</code> linefeed LF ('\n').
*
* @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexic
al.html#101089">JLF: Escape Sequences
* for Character and String Literals</a>
* @since 2.2
*/
public static final char LF = '\n';
/**
* <code>\u000d</code> carriage return CR ('\r').
*
* @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexic
al.html#101089">JLF: Escape Sequences
* for Character and String Literals</a>
* @since 2.2
*/
public static final char CR = '\r';
// Chopping
//-----------------------------------------------------------------------
/**
* Remove the last character from a String.
*
* If the String ends in <code>\r\n</code>, then remove both
* of them.
*
* <pre>
* StringUtils.chop(null) = null
* StringUtils.chop("") = ""
* StringUtils.chop("abc \r") = "abc "
* StringUtils.chop("abc\n") = "abc"
* StringUtils.chop("abc\r\n") = "abc"
* StringUtils.chop("abc") = "ab"
* StringUtils.chop("abc\nabc") = "abc\nab"
* StringUtils.chop("a") = ""
* StringUtils.chop("\r") = ""
* StringUtils.chop("\n") = ""
* StringUtils.chop("\r\n") = ""
* </pre>
*
* @param str the String to chop last character from, may be null
* @return String without last character, <code>null</code> if null String i
nput
*/
public static String chop(String str) {
if (str == null) {
return null;
}
int strLen = str.length();
if (strLen < 2) {
return "";
}
int lastIdx = strLen - 1;
String ret = str.substring(0, lastIdx);
char last = str.charAt(lastIdx);
if (last == LF) {
if (ret.charAt(lastIdx - 1) == CR) {
return ret.substring(0, lastIdx - 1);
}
}
return ret;
}
// Empty checks
//-----------------------------------------------------------------------
/**
* Checks if a String is empty ("") or null.
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}
29.
2. 21. 1. To replace one specific character with another throughout a string
public class MainClass {
public class Main {
public static void main(String[] arg) {
String text = "To be or not to be, that is the question.";
public static void main(String[] argv) throws Exception {
public class Main {
String newText = text.replace(' ', '/'); // Modify the string text
String string = "this is a string";
public static void main(String[] argv) throws Exception {
// Replace all occurrences of 'a' with 'o'
System.out.println(replace("this is a test", "is", "are"));
System.out.println(newText);
String newString = string.replace('a', 'o');
}
}
System.out.println(newString);
}
static String replace(String str, String pattern, String replace) {
}
int start = 0;
To/be/or/not/to/be,/that/is/the/question.
int index = 0;
StringBuffer result = new StringBuffer();
32.
30.
2. 21. 4. Replacing Substrings in a String
while ((index = str.indexOf(pattern, start)) >= 0) {
2. 21. 2. To remove whitespace from the beginning and end of a string (but not the interior)
result.append(str.substring(start, index));
result.append(replace);
start = index + pattern.length();
public class MainClass {
}
result.append(str.substring(start));
public static void main(String[] arg) {
return result.toString();
}
String sample = " This is a string ";
}
String result = sample.trim();
System.out.println(">"+sample+"<");
33.
System.out.println(">"+result+"<");
2. 21. 5. String.Replace
}
}
public class MainClass
>{ This is a string <
>This is a string<
public static void main( String args[] )
{
String s1 = new String( "hello" );
31.
String s2 = new String( "GOODBYE" );
2. 21. 3. Replacing Characters in a String: replace() method
String s3 = new String( " spaces " );
creates a new string with the
replaced characters.
System.out.printf( "s1 = %s\ns2 = %s\ns3 = %s\n\n", s1, s2, s3 );
// test method replace
System.out.printf("Replace 'l' with 'L' in s1: %s\n\n", s1.replace( 'l'
, 'L' ) );
} // end main
}
s1 = hello
s2 = GOODBYE
s3 = spaces
34.
2. 21. 6. Replaces all occourances of given character with new one and returns new String object.
public class Main {
public static void main(String args[]) {
String str = "This is a test.";
System.out.println(str.replace('T', 'A'));
}
}
//Ahis is a test.
35.
2. 21. 7. Replaces only first occourances of given String with new one and returns new String
object.
public class Main {
public static void main(String args[]) {
String str = "This is a test.";
System.out.println(str.replaceFirst("Th", "Ab"));
}
}
//Abis is a test.
36.
2. 21. 8. Replaces all occourances of given String with new one and returns new String object.
public class Main {
public static void main(String args[]) {
String str = "This is a test.";
System.out.println(str.replaceAll("is", "are"));
}
}
//Thare are a test.
37.
2. 21. 9. Replace/remove character in a String: replace all occurences of a given character
public class Main {
public static void main(String args[]) {
String myString = "'''";
String tmpString = myString.replace('\'', '*');
System.out.println("Original = " + myString);
System.out.println("Result = " + tmpString);
}
}
/*
Original = '''
Result = ***
*/
38.
2. 21. 10. To replace a character at a specified position
public class Main {
public static void main(String args[]) {
String str = "this is a test";
System.out.println(replaceCharAt(str, 5, 'c'));
}
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0, pos) + c + s.substring(pos + 1);
}
}
//this cs a test
39.
2. 21. 12. Replace multiple whitespaces between words with single blank
public class Main {
public static void main(String[] argv) {
System.out.println(">" + " asdf ".replaceAll("\\b\\s{2,}\\b", " ") + "<
");
}
}
//> asdf <
40.
2. 21. 14. Only replace first occurence
public class Main {
public static void main(String[] args) {
String text = "a b c e a b";
System.out.println(text.replaceFirst("(?:a b)+", "x y"));
}
}
//x y c e a b
41.
2. 21. 15. Get all digits from a string
public class Main {
public static void main(String[] argv) throws Exception {
System.out.println("ab1 2c9_8z yx7".replaceAll("\\D", ""));
}
}
42.
2. 21. 16. Returns a new string with all the whitespace removed
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
public class Main{
/**
* Returns a new string with all the whitespace removed
*
* @param s the source string
* @return the string without whitespace or null
*/
public static String removeWhiteSpace(String s)
{
String retn = null;
if (s != null)
{
int len = s.length();
StringBuffer sbuf = new StringBuffer(len);
for (int i = 0; i < len; i++)
{
char c = s.charAt(i);
if (!Character.isWhitespace(c))
sbuf.append(c);
}
retn = sbuf.toString();
}
return retn;
}
43.
2. 21. 17. Removes specified chars from a string
/*
* The contents of this file are subject to the Sapient Public License
* Version 1.0 (the "License"); you may not use this file except in complianc
e
* with the License. You may obtain a copy of the License at
* http://carbon.sf.net/License.html.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License f
or
* the specific language governing rights and limitations under the License.
*
* The Original Code is The Carbon Component Framework.
*
* The Initial Developer of the Original Code is Sapient Corporation
*
* Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
/**
* Utilities for strings.
*
*
* Copyright 2002 Sapient
* @since carbon 1.0
* @author Greg Hinkle, May 2002
* @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
*/
public class StringUtil {
/**
* Removes specified chars from a string.
*
* @param aString the string that will be examined to remove chars
* @param unWantedCharArray the char array containing the chars
* that should be removed from a string
* @return the string after removing the specified chars
*/
public static String removeCharsFromString(String aString, char[] unWantedC
harArray) {
Character character = null;
// Store unwanted chars in a hashset
Set unWantedCharSet = new HashSet();
for (int i = 0; i < unWantedCharArray.length; i++) {
character = new Character(unWantedCharArray[i]);
unWantedCharSet.add(character);
}
// Create result String buffer
StringBuffer result = new StringBuffer(aString.length());
// For each character in aString, append it to the result string buffer
// if it is not in unWantedCharSet
for (int i = 0; i < aString.length(); i++) {
character = new Character(aString.charAt(i));
if (!unWantedCharSet.contains(character)) {
result.append(aString.charAt(i));
}
}
// Return result
return result.toString();
}
}
43.
. 21. 18. Remove/collapse multiple spaces.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @version $Id: StringUtils.java 685685 2008-08-13 21:43:27Z nbubna $
*/
public class Main {
/**
* Remove/collapse multiple spaces.
*
* @param argStr string to remove multiple spaces from.
* @return String
*/
public static String collapseSpaces(String argStr)
{
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++)
{
char ch = argStr.charAt(cIdx);
if (ch != ' ' || last != ' ')
{
argBuf.append(ch);
last = ch;
}
}
return argBuf.toString();
}
}