Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:39:34+00:00 2026-05-16T03:39:34+00:00

In an Android app, I am looking for the functionality of the .NET string

  • 0

In an Android app, I am looking for the functionality of the .NET string function [Trim(‘aaa’)] which will trim off the text “aaa” off a string. I don’t think this is natively available in Java and I haven’t seen that functionality in Android Java textutil library.

Is there an easy way to have a Java app trim a set of characters from the string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:39:35+00:00Added an answer on May 16, 2026 at 3:39 am

    Regex trimming

    The specification isn’t clear, but you can use regular expression to do this.

    Here’s an example:

        // trim digits from end
        System.out.println(
            "123a456z789".replaceAll("\\d+\\Z", "")
        );
        // 123a456z
    
        // trim digits from beginning
        System.out.println(
            "123a456z789".replaceAll("\\A\\d+", "")
        );
        // a456z789
    
        // trim digits from beginning and end
        System.out.println(
            "123a456z789".replaceAll("\\A\\d+|\\d+\\Z", "")
        );
        // a456z
    

    The anchors \A and \Z match the beginning and end of the input respectively. | is alternation. \d is the shorthand for the digit character class. + is “one-or-more-of” repetition specifier. Thus, the pattern \d+\Z is regex-speak for “sequence of digits at the end of the input”.

    References

    • java.util.regex.Pattern
    • regular-expressions.info – Anchors, Alternation, Repetition, Character Class.

    Literal trimming

    If you just want a literal suffix/prefix chopping, then no regex is required. Here’s an example:

    public static String chopPrefix(String s, String prefix) {
        if (s.startsWith(prefix)) {
            return s.substring(prefix.length());
        } else {
            return s;
        }
    }
    public static String chopSuffix(String s, String suffix) {
        if (s.endsWith(suffix)) {
            return s.substring(0, s.length() - suffix.length());
        } else {
            return s;
        }
    }
    public static String chopPresuffix(String s, String presuffix) {
        return chopSuffix(chopPrefix(s, presuffix), presuffix);
    }
    

    Then we can have:

        System.out.println(
            chopPrefix("abcdef", "abc")
        ); // def
    
        System.out.println(
            chopSuffix("abcdef", "ef")
        ); // abcd
    
        System.out.println(
            chopPresuffix("abcdef", "cd")
        ); // abcdef
    
        System.out.println(
            chopPresuffix("abracadabra", "abra")
        ); // cad
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking for some mechanism through which I can fetch android app's information
I'm looking for an intent-filter, like android.intent.category.HOME, that will allow an app to launch
Just starting out on android developing. To start off, I'm building an app which
I'm starting an Android app which will have multiple Activities. It will be using
I am making an Android app which requires a main view with similar functionality
I am looking to build a button in my Android app which must contain:
I am building an Android app which takes a string input and returns a
I'm looking to replicate the functionality shown on the android developer app tutorial. I
I just started looking at android app development. For my first test app i
Possible Duplicate: Register the Android App with C2DM so, I've been looking all over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.