Replacing white spaces is not working - replaceAll('\\s+'

I have a string where I want to replace all of the spaces with plus (+) symbols.

I've tried a number of different ways but the debug log is always showing that nothing is being replaced

I'm passing in a = 101 e wilson st c = madison z = 53703

the end result should be: 101+e+wilson+st+madison+53703

However it's coming out as: 101 e wilson st+madison+53703

string param = a + '+' + c + '+' + z; So the replace all Regex is not working.


  public void getCoord(string a, string c, string z){             Http h = new Http();             HttpRequest req = new HttpRequest();              a.replaceAll('\\s+', '-');             c.replaceAll('\\s+', '-');             z.replaceAll('\\s+', '-');              string param = a + '+' + c + '+' + z;             system.debug('looking for coordinates');             req.setEndpoint(''+ param +'&sensor=false'); 


The replaceAll method returns the modified string, it does not modify the reference to the value passed.

So you should change it to this and it should work for you:

a = a.replaceAll('\\s+', '-');
c = c.replaceAll('\\s+', '-');
z = z.replaceAll('\\s+', '-');

Eric has a good answer, but this will also work for this specific case, just a generic method for what you're trying to do, this will return a working format for POST to google

public static String buildAddressForGooglePost(String street, String city, String state, String postalCode, String country)
    String result = '';

    if (street != null) result += street +', ';
    if (city != null) result += city +', ';
    if (state != null) result += state +' ';
    if (postalCode != null) result += postalCode +', ';
    if (country != null) result += country;

    result = EncodingUtil.urlEncode(result, 'UTF-8');

    return result;

Category: apex Time: 2016-07-28 Views: 0

Related post

  • I need regular expression for allow only letters,numbers and white space but not allow special characters 2016-01-27

    I need regular expression for allow only letters,numbers and white space but not allow special characters This is my code <input type="text" name="station_name" class="form-control" data-validation="required" dat

  • Replace white space in filename with hyphen 2016-02-11

    I'm using the following line to rename an mp4 file by adding a timestamp to the end of the name. mediaName_ts = "%s_%s.mp4" %(pfile, time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())) But I have problems accessing the file when the fil

  • Replacing white-space / characters with backslashes 2015-03-06

    This question already has an answer here: Looping through files with spaces in the names? 10 answers Why does my shell script choke on whitespace or other special characters? 3 answers I want to convert a bunch of mkvs to mp4s with the following comm

  • Outlook2010 : Sending hyperlinks containing a space, is not working 2012-03-05

    Previously I used Outlook 2003 and I was able to send a hyperlink with a space by typing < before entering the hyperlink and a > after typing the hyperlink and the whole thing would become a valid clickable link. Like so <\\network share with spa

  • Rsync over SSH path with spaces does not work with quotes 2015-03-16

    I am able to successfully RSYNC over SSH as long as the paths do not have spaces in them. When the path does have spaces, it does not work. I have tried slashes, quotes, and double quotes. When I use slashes, the output states that it is a success bu

  • Replacing blank character is not working inside script 2012-08-08

    In a shell script I tried to replace blank space by \n with this command echo -e $var |tr ' ' '\n' Outside the script it workd perfectly but inside there is no effect . This how i use in the script: var =$(echo -e $var | tr ' ' '\n' ) Thanks . ------

  • Android - Replacing a fragment is not working 2016-02-15

    public boolean onOptionsItemSelected(MenuItem item) { List<Fragment> listOfFragments = new ArrayList<>(); FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); tabHost.setCurrentTab(0); if (my_menu_status) { listOfFragmen

  • non-breaking space does not work in my WORD 2010 2013-07-09

    When I choose non-breaking space in word 2010, it only inserts a does not move the text to the next line. When I try the hotkey sequence, it brings up my scanner settings. So I tried to make a new hotkey sequence for a non-breaking space,

  • Why does SPACE sometimes not work with more (or less) 2014-04-10

    Usually with |more I press space to get another screenful. But with some commands space just does nothing, and I need to actually keep doing SPACE-ENTER to scroll forward a screen at a time. Both more and less behave the same. On this machine this co

  • Find and Replace shapefile source script not working 2015-05-12

    I'm very new to python so please forgive me if this is a simple error. I have quite a few MXD's in which each have a few layers referencing the same source. However I have since moved this data to a new folder. I have written a script to automaticall

  • labelprefix to newrefcontext replacing prefixnumbers to printbibliography not working 2016-06-30

    After updating biblatex I get the following error message for this code: \usepackage[style=ieee, backend=bibtex, bibencoding=ascii, defernumbers=true, maxnames=10]{biblatex} \addbibresource{bib/references.bib} %... \printbibliography[heading=none, pr

  • Space is not working in latex 2014-06-18

    I want to have the following output . For this purpose , I have the following code . \begin{align*} Maximize \mathbf{z = x_1+2x_2+3x_3} \end{align*} \begin{align*} Subject to \mathbf{x_1 + \hspace{2cm}+2x_3 \leq 2 }\\ \mathbf{ \hspace{2cm}x_2+2x_3 \l

  • Insufficient space - solution not working 2015-03-16

    I've looked at the solution for this and it doesnt work. Some information from my device Phone model V3+ Android version 4.4.2 Build number ALPS.JB3.MP.V1 Storage is set to SD card Preferred install location is internal device storage Internal Storag

  • Replace with value '$' does not work in javascript 2016-01-15

    i tried to replace a string, provided the string regex with a value that has $ in the end. Can anyone tell me what is happening in Looking into mdn string replace docs, i found it is expected. But what should one do he want to ignore this. Means i wa

  • Does reducing white space improve or degrade the user experience? 2013-02-08

    As a web designer, I work on a lot of ecommerce websites. One piece of feedback that comes in time and time again, is to make vertical spacing smaller, not only to get more products above the fold, but to decrease the amount of scrolling down the pag

  • White spaces between color tabs and two other questions 2012-10-27

    !Partially solved! How to remove small white spaces between color blocks? (when you compile this tex there are small white spaces between the & \cellcolor{EM}\& & \cellcolor{EM}\\) I found an answer, but I think that this answer is bad: &

  • How strict should you be about indentation/white space? 2013-11-26

    Our development process is as follows code the task -> someone else QAs code and documentation -> task is merged into trunk. Recently a colleague is refusing to pass the code QA due to issues with indentation and whitespace. Here are examples of the

  • Using White Space: How Emptiness Can Give So Much to Design 2016-07-12

    [youtube]Ut116mBuPpg[/youtube] Rowan Atkinson: Welcome to Hell Before we start, take a look at a minute or so of the video above. What did you notice? Most likely, Rowan Atkinson's incredible wit, but did you notice the way he makes use of silence to

  • Should I leave white space inside accordion? 2012-10-09

    I'm building some web editor for charts and decided to group various independent chart options inside accordion. A question poped up about whitespace inside contents of some accordion panes. Basically we have 2 options of accordion layout: – Wirefram

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 1.761 (s). 13 q(s)