Remove a character from a string without knowing its position - c#-4.0

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

Related

How to get all characters from the right up to the particular character in dart

I am trying to get all characters from the right up to the '*' from below string.
String phoneNumber = '07*** ***253'
Here I want the substring as '253'
ex: '07*** ***4253'
result: 4253
You can split the string with split()
var _phone = "07*** ***253";
String _last = _phone.split('*').last;
Or same with regexp
RegExp reg = RegExp(r'[^*]*$');
var _matched = reg.allMatches(_phone);
_matches.last.group(0);

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

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

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

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.)

Resources