Apex - remove special characters from a string except for ''+" - string

In Apex, I want to remove all the special characters in a string except for "+". This string is actually a phone number. I have done the following.
String sampleText = '+44 597/58-31-30';
sampleText = sampleText.replaceAll('\\D','');
System.debug(sampleText);
So, what it prints is 44597583130.
But I want to keep the sign + as it is represents 00.
Can someone help me with this ?

Possible solutions
String sampleText = '+44 597/58-31-30';
// exclude all characters which you want to keep
System.debug(sampleText.replaceAll('[^\\+|\\d]',''));
// list explicitly each char which must be replaced
System.debug(sampleText.replaceAll('/|-| ',''));
Output in both case will be the same
|DEBUG| +44597583130
|DEBUG| +44597583130
Edit
String sampleText = '+0032 +497/+59-31-40';
System.debug(sampleText.replaceAll('(?!^\\+)[^\\d]',''));
|DEBUG|+0032497593140

Related

Getting substrings from a string in c# in the format Domain\Alias

I have a variable which has strings stored in the format "domain\alias" and I want to split this in two different strings domain and alias.
I have two solutions for the above case, but none of them are working in my case.
solution 1: separating alias from the string.
for this I am using the code below:
int index = name.IndexOf("\") + 1;
string piece = name.Substring(index);
where name is the variable which stores the string in the format "domain\alias"
This solution doesn't work for '\' however it works in case of '.'
solution 2:
separating domain from the string.
Here I got a solution below:
var domainFormattedString = #"fareast\v-sidmis";
var parts = domainFormattedString.Split('\\');
var domainString = parts[0];
return domainString;
this works, but it needs a string prefixed with #symbol and i have my string stored in the variable name for which this solution doesn't work.
Someone please help me to extract the two substrings from my variable name.
EDIT 1: Thanks all for your help! I figured out the issue...when i explicitly declare a string as: var x = "domian\alias" it creates and issue as \ is treated as a escape character by c# so i had to append # at the beginning. But I got to know that when a string is read from a user, the solution works!
\ has a special meaning so you need to override the escape sequence to be treated as normal character with another escape character.
string input = #"domain\alias";
int inputindex= input.IndexOf("\\");
string domain = input.Substring(0, inputindex);
string alias = input.Substring(inputindex+1);
Hope It helps eventhough better late than never :)

Removing special characters from a string In a Groovy Script

I am looking to remove special characters from a string using groovy, i'm nearly there but it is removing the white spaces that are already in place which I want to keep. I only want to remove the special characters (and not leave a whitespace). I am running the below on a PostCode L&65$$ OBH
def removespecialpostcodce = PostCode.replaceAll("[^a-zA-Z0-9]+","")
log.info removespecialpostcodce
Currently it returns L65OBH but I am looking for it to return L65 OBH
Can anyone help?
Use below code :
PostCode.replaceAll("[^a-zA-Z0-9 ]+","")
instead of
PostCode.replaceAll("[^a-zA-Z0-9]+","")
To remove all special characters in a String you can use the invert regex character:
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1]","");
System.out.println("str: <"+str+">");
output:
str: <>
to keep the spaces in the text add a space in the character set
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1 ]","");
System.out.println("str: <"+str+">");
output:
str: < >

Algorithms for "shortening" strings?

I am looking for elegant ways to "shorten" the (user provided) names of object. More precisely:
my users can enter free text (used as "name" of some object), they can use up to 64 chars (including whitespaces, punctuation marks, ...)
in addition to that "long" name; we also have a "reduced" name (exactly 8 characters); required for some legacy interface
Now I am looking for thoughts on how to generate these "reduced" names, based on the 64-char name.
With "elegant" I am wondering about any useful ideas that "might" allow the user to recognize something with value within the shortened string.
Like, if the name is "Production Test Item A5"; then maybe "PTIA5" might (or might not) tell the user something useful.
Apply a substring method to the long version, trim it, in case there are any whitespace characters at the end, optionally remove any special characters from the very end (such as dashes) and finally add a dot, in case you want to indicate your abbreviation that way.
Just a quick hack to get you started:
String longVersion = "Aswaghtde-5d";
// Get substring 0..8 characters
String shortVersion = longVersion.substring(0, (longVersion.length() < 8 ? longVersion.length() : 8));
// Remove whitespace characters from end of String
shortVersion = shortVersion.trim();
// Remove any non-characters from end of String
shortVersion = shortVersion.replaceAll("[^a-zA-Z0-9\\s]+$", "");
// Add dot to end
shortVersion = shortVersion.substring(0, (shortVersion.length() < 8 ? shortVersion.length() : shortVersion.length() - 1)) + ".";
System.out.println(shortVersion);
I needed to shorten names to function as column names in a database. Ideally, the names should be recognizable for users. I set up a dictionary of patterns for commonly occuring words with corresponding "abbreviations". This was applied ONLY to those names which were over the limit of 30 characters.

Blackberry Java replace char \n

I have an example string get from XML, contains: Hello\nWorld\n\nClick\nHere.\nThanks.
And then i want to replace the \n char with space char.
Already try using string replace, string substring, string indexOf. But cannot detect the \n char, iam trying using '\r\n' to detect, but didnt work.
String hello = "Hello\nWorld\n\nClick\nHere.\nThanks.";
String afterReplace = hello.replace('\n', ' ');
But still cannot remove/replace the \n with space.
Anyone can help me?
Thanks a lot.
If I understand correctly you have a string which when printed shows the \n characters and does not actually skip a line.
Hello\nWorld\n\nClick\nHere.\nThanks. would be represented in code by:
String s = "Hello\\nWorld\\n\\nClick\\nHere.\\nThanks."
Now s is equal to what you would obtain from your XML.
Try this:
String afterReplace = hello.replace('\\n', ' ');

Ensure a base64 encoded string never includes a nonalphanumerical character

Is there anyway to ensure that a base64 encoded string never includes a non-alphanumerical character?
For example, if I have a long string that I encode, is there something I can prepend or append to it that will ensure that when encoded with base64 will only include letters and numbers in the encoded string? Something like this:
String: 192.168.1.1
Encoded: MTkyLjE2OC4xLjE= <- I want to 'get rid' of the equal sign.
I tried appending } at the end of the string (new string is now 192.168.1.1}), and this worked (new encoded string: MTkyLjE2OC4xLjF9), but is there a method of ensuring every combination works?
Is this possible?
You can just rtrim() the equals signs away, which is what most people do.
but as for your question: when length of string / 3 is a whole number. So:
$pad = strlen($str) % 3; if($pad) { $str .= str_repeat(' ', $pad); }
but yeah, the parser will add the equals signs back in automatically just like that, to a multiple of 4, when you pass the string back in - so you dont need to keep them.
It's about the length. The = sign is padding to make the output a multiple of 4 base64 characters. 3 characters translate to 4 base64 characters, so you just need to make your input string a multiple of 3 characters in length somehow. In your case:
192.168.1.1 - 11 characters long, base64 ends with =
192.168.1.1$ - 12 characters long, base64 doesn't end with =
Choose a padding character you can easily remove.
The other alternative is to strip the = from the output, then make sure you append = signs to make a multiple of 4 characters before you try to base64 decode...

Resources