Tag Archives: string

Convert Java stacktrace to String

stacktrace
To present java stacktrace as string use next snippet:

Exception e = new Exception("EX!");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);

String stacktraceAsString = sw.toString();

Thanx to http://stackoverflow.com/a/1149712

Trim string in Java

char
Don’t like string frameworks? Use POJO java!

static String trim(String value, char character) {
    if (value == null || value.isEmpty()) {
        return value;
    }
    String normalized = value.trim();

    int startIndex = 0;
    while (normalized.charAt(startIndex) == character) {
        startIndex++;
    }

    int endIndex = value.length() - 1;
    while (normalized.charAt(endIndex) == character) {
        endIndex--;
    }

    return normalized.substring(startIndex, endIndex + 1).trim();
}

Simple!

Capitalize word algorithm in Java

capit
Here is simple snippet to capitalize string:

private static String capitalize(String value) {
    return value == null ? null : 
        value.substring(0, 1).toUpperCase() + value.substring(1);
}

Simple!

Thanx to
http://stackoverflow.com/questions/5725892/how-to-capitalize-the-first-letter-of-word-in-a-string-using-java

Malformed \uxxxx encoding error in Java

malformed
You may see the following error while java execution on windows OS:

Malformed \uxxxx encoding.

Possible solution:
Check your *.properties files and fix windows-pathes

from

C:\Some\path

to

C:/Some/path

or

C:\\Some\\path

Java symbol escaping!

Remove leading zeros from string in Batch

sacsC
It is often need to convert string to int in batch. But:

set /a int=0100
echo Int: %int%

Will result

Int: 64

It ‘s because lead zero – seen as a HEX (0xNUMBER)

To solve use next:

@echo off
setlocal enableextensions enabledelayedexpansion

REM Removes leading "0" from string

REM Settings
set z_strtoint=%1

REM Init
set strterm=___ENDOFSTRING___
set tmp=%z_strtoint%%strterm%

REM Action
:loop
set char=%tmp:~0,1%
set tmp=%tmp:~1%
REM echo char: %char%
if [!char!] EQU [0] set z_strtoint=!z_strtoint:~1!
if [!char!] NEQ [0] goto endloop
if [!tmp!] NEQ [!strterm!] goto loop
:endloop

REM Show result
echo %z_strtoint%

So, exec:

z_strtoint 0100
100

Enum with String values implementation in Java

enum_data_type
This is classical realization of enum with string values:

    private enum SomeTestEnum {

        ITEM1("TheStringNameOfItem1"),
        ITEM2("TheStringNameOfItem2"),
        ITEM3("TheStringNameOfItem3");

        private final String name;

        /**
         * Default constructor
         */
        SomeTestEnum(String name) {
            this.name = name;
        }

        /**
         * Getter for current instance name value
         */
        public String getName() {
            return name;
        }

        public static SomeTestEnum getByName(String name) {
            for (SomeTestEnum item : SomeTestEnum.values()) {
                if (item.getName().equals(name)) {
                    return item;
                }
            }
            return null;
        }
    }

Reverse String in Java

str
It is really amazing simple! Just use recursion to reverse a String:

public static String reverse(String value) {
    return value == null || value.length() <= 1 ? value 
        : reverse(value.substring(1)) + value.charAt(0);
}

Thanx to javinpaul http://javarevisited.blogspot.com/2012/01/how-to-reverse-string-in-java-using.html