elegant
elegant

Reputation: 33

Replace semi colon (;) character in java


I am trying to truncate semi colon from both the ends of a string using this method:

private String truncateSemicolon(String inputString){
    String outputString=inputString;
    boolean isTruncated = false;
    Log.i(TAG, "*************I N P U T   T O  T R U N C A T E ************* "+outputString);
    while(!isTruncated){
        if(outputString.startsWith(";")){

            outputString=outputString.substring(1,outputString.length());
            Log.i(TAG, "START *************************************"+outputString);
        }
        if(outputString.endsWith(";")){

            outputString=outputString.substring(0,(outputString.length()-1));
            Log.i(TAG, "************************************* END"+outputString);
        }
        if(!outputString.endsWith(";") && !outputString.startsWith(";")){
            isTruncated=true;
        }
    }       
    Log.i(TAG, "*************O U T P U T   T O  T R U N C A T E ************* "+outputString);
    return outputString;
}

But this truncates from beginning only.
What is the problem??

Upvotes: 0

Views: 3387

Answers (9)

kundan kumar
kundan kumar

Reputation: 1

this is solution

    String str = "input_test_str";
    // inputTestStr :
    String[] arr = str.split("");
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < arr.length; i++) {
        if (arr[i].equals("_")) {
            sb.append(arr[++i].toUpperCase());
        } else {
            sb.append(arr[i]);
        }
    }
    System.out.println(sb);

Upvotes: 0

Zar E Ahmer
Zar E Ahmer

Reputation: 34370

Multiple ways to achieve this

String yourDesiredText= anytext.replaceAll(";$", "");

OR

yourDesiredText= yourDesiredText.replaceAll(";$", "");

OR

if (yourDesiredText.endsWith(";")) {
    yourDesiredText= yourDesiredText.substring(0, yourDesiredText.length() - 1);
}

Strings are immutable. That means you can't change them.

Therefore you have to either re-assign text or set it to a new variable.

Upvotes: 0

Andy
Andy

Reputation: 653

Your code is working fine you just follow a simple step:

String outputString=inputString.trim();

Upvotes: 2

Tarsem Singh
Tarsem Singh

Reputation: 14199

Your Code is correct ! you may be getting blank space after ; in your string

to avoid that use trim()

for that case try

if (outputString.trim().endsWith(";")) {
    outputString = outputString.substring(0,(outputString.trim().length() - 1));
    Log.i(TAG, "*************O U T P U T   T O  T R U N C A T E ************* "+outputString);
}

Upvotes: 1

Jason
Jason

Reputation: 11822

Your code already works correctly. Performing the substring only once will make the function more efficient.

private String truncateSemicolon(final String inputString) {
    int startPos = inputString.startsWith(";") ? 1 : 0;
    if(inputString.endsWith(";")) {
        return inputString.substring(startPos, inputString.length() - 1);
    } else {
        return startPos == 0 ? inputString : inputString.substring(startPos);
    }
}

Upvotes: 0

Korean_Karue
Korean_Karue

Reputation: 41

your code is right

this code is remove only first ";" and last ";" is right?

it is can't remove if last ";" have space next ex ( " ; " )

if you wan't ignore space

using a trim() method

Upvotes: 2

Aniket Thakur
Aniket Thakur

Reputation: 68975

    if(outputString.startsWith(";")){

        outputString=outputString.substring(1,outputString.length());
        Log.i(TAG, "START *************************************"+outputString);
    }
    if(outputString.endsWith(";")){

        outputString=outputString.substring(0,(outputString.length()-1));
        Log.i(TAG, "************************************* END"+outputString);
    }

For public String substring(int beginIndex, int endIndex) beginIndex is the beginning index, inclusive and endIndex is the ending index, exclusive.

So change outputString=outputString.substring(1,(outputString.length())); to outputString=outputString.substring(1,(outputString.length() - 1));

Similarly change in second if statement

outputString=outputString.substring(0,(outputString.length()-1)); with

outputString=outputString.substring(0,(outputString.length()-2));

So your code should be

    if(outputString.startsWith(";")){

        outputString=outputString.substring(1,outputString.length()-1);
        Log.i(TAG, "START *************************************"+outputString);
    }
    if(outputString.endsWith(";")){

        outputString=outputString.substring(0,(outputString.length()-2));
        Log.i(TAG, "************************************* END"+outputString);
    }

Upvotes: 0

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this single line may make you happy

String outputString=inputString.replace(";","");

Upvotes: 0

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Better way to remove semicolon ';' from your string is to use String.replaceAll().

String replacedString = inputString.replaceAll("^;|$;","");

Upvotes: 0

Related Questions