Blackberry Java replace char \n - string

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', ' ');

Related

Flutter remove extra white space between string

how to remove extra white space between following string ?
String word= "Hai where are you from" ;
word.split(" ") does not work for this condition.
this will remove the white space in the string
try
String word= "Hai where are you from";
print(word.replaceAll(' ', ''));
You can you two functions for String:
ReplaceAll Function
String str;
str.replaceAll(" ", " ");
Trim function
str.trim();
If you want to remove extra white spaces you can use this code. Just call the cleanupWhitespace method and it will return the cleaned up string.
final whitespaceRE = RegExp(r"(?! )\s+| \s+");
String cleanupWhitespace(String input) => input.split(whitespaceRE).join(" ");
It works like this for ex if you have a string like this:
Hello world
It will replace all the white space with a single white space.
Hello world

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

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

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: < >

Issue with \ and \\ when calling String Split()

I am trying to split some string on the basis of newline character '\n'
I have this delimiter stored in resx file as:
Name: RecordDelimiter
Value: \n
When I retrieve this value from .resx file it is always returned as '\n' and
split function does not return accurate results.
However when I try with string "\n", it's working fine
Here is my code -
private static void GetRecords()
{
string recordDelimiter = #"\n";
string recordDelimiter1 = "\n"; // only this returns correct result
string recordDelimiter2 = ResourceFile.RecordDelimiter; //from resx file, returns \\n :-(
string recordDelimiter3 = ResourceFile.RecordDelimiter.Replace("\\", #"\"); //try replacing \\n with \n
string fileOutput = "aaa, bbb, ccc\naaa1, bbb1, ccc1\naaa2, bbb2, ccc2";
string[] records = fileOutput.Split(new string[] { recordDelimiter }, StringSplitOptions.None);
string[] records1 = fileOutput.Split(new string[] { recordDelimiter1 }, StringSplitOptions.None);
string[] records2 = fileOutput.Split(new string[] { recordDelimiter2 }, StringSplitOptions.None);
string[] records3 = fileOutput.Split(new string[] { recordDelimiter3 }, StringSplitOptions.None);
int recordCount = records.Count(); //returns 1
int recordCount1 = records1.Count(); //returns 3 -- only this returns correct result
int recordCount2 = records2.Count(); //returns 1
int recordCount3 = records3.Count(); //returns 1
}
I want to keep the delimiter in resx file.
Can anyone please guide if I am missing something?
Thank you!
The reason your second method is the only one returning the correct result is that it is the only one where the delimiter is the new line character. "\n" is just a representation of the newline character in C#, and #"\n" is the literal string of a slash followed by the letter n. In other words #"\n" != "\n".
So if you wanted to store the delimiter character in resx, you would need to show us the code of how you are storing it there. Currently it seems to just be stord as a literal string, and not the actual control characters.
One (very rough) fix would be to take the string from the Resources and call .Replace(#"\n", "\n") depending on what exactly is stored in the file. I will update my answer if/when I find a better solution, or once you have updated your question.
EDIT:
Ok, found a somewhat gimmicky solution. The core problem is how do you write just \n, correct? Well, I made a test project, with a textbox and the following code:
this.textBox1.Text = "1\n2";
Fire up this project, select all of the text in the textbox, and copy to clipboard. Then go to your real project's resources, and paste the value from your clipboard. Then carefully delete the numbers from around the control character. And there you go, \n control character in a resource string. (The reason for the numbers was that it wasn't possible to select only the control character from the textbox.)

Remove a character from a string without knowing its position

I want to remove a character ('.') from a string without knowing its position.
For example.
string test = "4.000";
But the '.' will always change.
Thanks!
If you only want to remove the first occurrence of that character:
string newString = test.Remove(test.IndexOf(".", 1));
If you want to remove all occurrences of that character:
string newString = test.Replace(".", "");
Use String.Replace.
string stringToReplace = ".";
string test = "4.000";
test = test.Replace(stringToReplace, "");

Resources