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

  • SEARCH
  • Home
  • 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 7441239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:58:44+00:00 2026-05-29T10:58:44+00:00

I have to trim (including whitespaces within the string) all the strings in a

  • 0

I have to trim (including whitespaces within the string) all the strings in a list. I have written a method to do this trim using regex. I am using strArray[i] = trimString(strArray[i]); instead of using an enhanced for loop. I assume since string is immutable this way of assigning back to the same array element is correct. Am I right?

public void trimStringArray(String[] strArray){
    if(strArray!= null && strArray.length > 0){
        for(int i=0;i<strArray.length;i++){
            strArray[i] = trimString(strArray[i]);
        }           
    }
}
  • 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-29T10:58:45+00:00Added an answer on May 29, 2026 at 10:58 am

    Yes, that’s fine, and you wouldn’t be able to use the enhanced for loop. However, you can simplify your code by getting rid of the length > 0 check – there’s no harm in it executing the loop 0 times… and personally I would usually expect the parameter to such a method to be non-null anyway, leading to code like this:

    public void trimStringArray(String[] strArray) {
        Preconditions.checkNotNull(strArray);
        for(int i = 0; i < strArray.length; i++) {
            strArray[i] = trimString(strArray[i]);
        }
    }
    

    (Preconditions.checkNotNull comes from Guava in this case.)

    You could leave it accepting null – but do you really have many situations where it’s valid to have a null array, but you want to trim everything if it’s not?

    As a readability thing, I’d also encourage you to include a bit more whitespace – it’s definitely a personal preference, but I know I find code with no spaces, such as this, harder to read:

    for(int i=0;i<strArray.length;i++){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method like this: public string StartCapture(string[] args) { if(args[0].Trim() == null
I have an extension method like this: <Extension()> Public Function Trim(value As Object, MaxLength
I saw another post suggesting using this statement to trim string variables contained in
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
I have a bunch of strings I need to use .Trim() on, but they
I have the string 22 and I'm getting 3 as length; I used .trim()
Ok so i have a jQuery .change $('#application').change(function(){ var selected = $(this).val().trim(); alert(selected); if(selected
In JavaScript, how do I trim from the right(string end)? I have the following
I have been trying to use a C# Regex unsuccessfully to remove certain strings
I have the following code: $('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : '5px

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.