1

How can I add space on both sides of a character of a string in java?

Ex- suppose string is "s_id=5 and s_name!=6" and if I want to add space on both the sides of = then output string will be like "s_id = 5 and s_name!=6"

I am trying this using replace and contains method...

I check if(str.contains("=")) then replace it with(" = ") but it also adds space for !=.

alhelal
  • 916
  • 11
  • 27
Sam
  • 57
  • 2
  • 8
  • You need to decide then after/before what characters the space should be added. You should specify what other characters should be ignored before `=`, for example, decide if `>=` or `<=` should be ignored. – Sergey Kalinichenko Jan 07 '12 at 05:21
  • Please update your question, as your question misleads the reader to believe you really want a string replacement, while its a rules based replacement. Being specific will help with more concrete answers – Salman Paracha Jan 07 '12 at 05:22
  • Additionally, please provide the complete criteria for the adding the space before and after "=". From your example, your rule seems arbitrary, could you please be more specific? – Salman Paracha Jan 07 '12 at 05:23
  • Objective is to add space on both the sides of each operator in string (=,!=). When I check that if character is =, I will add space but space is added for != also. because it also contains = that's the problem which i want to solve that I want to add spaces for a character which is STRICTLY the same character...hope now its more specific.. – Sam Jan 07 '12 at 05:32

4 Answers4

1

Use regex:

s = s.replaceAll("(?<![><!+-])[=]", " = ");

Put the characters you don't want preceeding = between square brackets in the regexp. The expression in the answer ignores +=, -+, <=, <=, and !=.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • thanks...its worked for my problem..But as I dont know much about regular expressions. Can you give regex to distinguish between >= and > means if I want to add space on both the sides of >= then it should not add in case of > Thanks in adv. – Sam Jan 07 '12 at 06:08
  • @Sam When you want to add spaces around *longer* strings and ignore *shorter* ones, you can avoid regular expressions altogether: you can call `s.replaceAll(">=", " >= ")`, and it will ignore `>` and `=` when they are *not* next to each other. – Sergey Kalinichenko Jan 07 '12 at 12:05
  • Learning regular expressions is a good thing, just start with it. – Has QUIT--Anony-Mousse Jan 07 '12 at 18:59
  • @dasblinkenlight here you have given example of ignoring +,<,> and ! etc..now what to write if I also want to ignore " before = ? – Sam Jan 09 '12 at 08:40
  • @Sam You can put double quotes into the list. Of course you will need to *escape* it with a backslash, like this `"[\"><!+-]"`; otherwise, Java would report a compile error. Note that if a dash `-` is present, it needs to be the first or the last character on the list, because it has special meaning inside square brackets if it has other characters on both sides. – Sergey Kalinichenko Jan 09 '12 at 10:50
  • 2
    @Sam: Try this book http://www.amazon.de/gp/product/0596528124/ref=as_li_ss_tl?tag=vitavonni-21 or any other book with "regular expressions" in the title and good reviews. – Has QUIT--Anony-Mousse Jan 10 '12 at 07:18
  • @dasblinkenlight I think your solution put extra space where there are space before and after the `=`. – alhelal Feb 15 '18 at 02:21
1
value = value.replaceAll("([$_\\d\\w])([^$_\\d\\w]+)", "$1 $2")
             .replaceAll("([^$_\\d\\w]+)([$_\\d\\w])", "$1 $2")
             .replaceAll("\\s+", " "))

This code will add separators to any operator, and remove unecessary spaces.

s_id=5 and s_name!=6 will become s_id = 5 and s_name != 6

Considering that variables can be composed of $ _ digits or letters

Rogel Garcia
  • 1,895
  • 14
  • 16
0

You can use the replaceAll() method in the String class to help achieve this

example:

final String spacedEquals = " = ";
String s = "s_id=5 and s_name!=6";

//this will add a space on either side of the '='
s.replaceAll("=", spacedEquals); 

hope this helps

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • No its not working.. Output of your code will be like: s_id = 5 and s_name! = 6 i.e. it adds space for != also... – Sam Jan 07 '12 at 05:33
0

try this method

    private String replace( String str, String pattern, String replace ) 
{
    int s = 0;
    int e = 0;
    StringBuffer result = new StringBuffer();

    while ( (e = str.indexOf( pattern, s ) ) >= 0 ) 
    {
        result.append(str.substring( s, e ) );
        result.append( replace );
        s = e+pattern.length();
    }
    result.append( str.substring( s ) );
    return result.toString();
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143