Rectangle 27 1

A possible solution would be to strip the accent with the help of Apache Commons StringUtils.stripAccents(input) method:

String input = StringUtils.stripAccents("Jnos");
System.out.println(input); //Janos
Normalizer

Java ignore special characters in string matching - Stack Overflow

java string compare special-characters
Rectangle 27 150

@search_query = @search_query.gsub(/[^0-9a-z ]/i, '')
@search_query.gsub!(/[^0-9a-z ]/i, '')
\s
\\s

ruby on rails - How do I strip non alphanumeric characters from a stri...

ruby-on-rails ruby regex ruby-on-rails-3
Rectangle 27 4

In this case I would use the bang method (gsub! instead of gsub) in order to clean the input permanently.

#permanently filter all non-alphanumeric characters, except _
@search_query.gsub!(/\W/,'')

This avoids a situation where @seach_query is used elsewhere in the code without cleaning it.

In his example the return value isn't used, so that's pretty much irrelevant.

ruby on rails - How do I strip non alphanumeric characters from a stri...

ruby-on-rails ruby regex ruby-on-rails-3
Rectangle 27 3

This works fine in java.

It basically converts all accented characters into their deAccented counterparts followed by their combining diacritics. Now you can use a regex to strip off the diacritics.

import java.text.Normalizer;
import java.util.regex.Pattern;

public String deAccent(String str) {
    String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD); 
    Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    return pattern.matcher(nfdNormalizedString).replaceAll("");
}
"\\p{Block=CombiningDiacriticalMarks}"

Why would you post a Java solution when the question specifically asks for .NET?

How do I remove diacritics (accents) from a string in .NET? - Stack Ov...

.net string diacritics
Rectangle 27 3

This works fine in java.

It basically converts all accented characters into their deAccented counterparts followed by their combining diacritics. Now you can use a regex to strip off the diacritics.

import java.text.Normalizer;
import java.util.regex.Pattern;

public String deAccent(String str) {
    String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD); 
    Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    return pattern.matcher(nfdNormalizedString).replaceAll("");
}
"\\p{Block=CombiningDiacriticalMarks}"

Why would you post a Java solution when the question specifically asks for .NET?

How do I remove diacritics (accents) from a string in .NET? - Stack Ov...

.net string diacritics
Rectangle 27 3

This works fine in java.

It basically converts all accented characters into their deAccented counterparts followed by their combining diacritics. Now you can use a regex to strip off the diacritics.

import java.text.Normalizer;
import java.util.regex.Pattern;

public String deAccent(String str) {
    String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD); 
    Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    return pattern.matcher(nfdNormalizedString).replaceAll("");
}
"\\p{Block=CombiningDiacriticalMarks}"

Why would you post a Java solution when the question specifically asks for .NET?

How do I remove diacritics (accents) from a string in .NET? - Stack Ov...

.net string diacritics
Rectangle 27 1

I would have used the inclusion approach. Rather than exclude all but numbers, I would only included numbers. E.g.

@search_query.scan(/[\da-z\s]/i).join

ruby on rails - How do I strip non alphanumeric characters from a stri...

ruby-on-rails ruby regex ruby-on-rails-3
Rectangle 27 0

Problem is that requires duplicating the character-decoding logic in the stream.

There may not be a way to avoid that without customizing XMLParser?

Why not just use a customized XMLParser only when there is a SAXException? It would seem that if you get a bad xml file then it would be best to reject the entire file as the damaged part may lead to bad data being extracted.

blackberry - How should I strip invalid XML characters from a stream i...

xml blackberry java-me sax rim-4.2
Rectangle 27 0

first think you want to do is to strip the string or value from numbers using something like this

<input type="text" name="amount" value="N50,000.00 NGN" />
<input type="button" value="submit">



var amount =  $('input[name=amount]').val().replace(/[A-Za-z$-]/g, "");
   amount=amount.replace(",", "");
   amount=parseInt(amount).toFixed(0);

How do i remove all characters in a string Javascript? - Stack Overflo...

javascript string string-formatting money-format removeall
Rectangle 27 0

var text = "Hello there(hello#)";

var rgx = new Regex("[^a-zA-Z0-9]");

text = rgx.Replace(text, string.Empty);

c# - How do I strip non-alphanumeric characters (including spaces) fro...

c# asp.net .net regex
Rectangle 27 0

In .Net 4.0 you can use the IsNullOrWhitespace method of the String class to remove the so called white space characters. Please take a look here http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx However as @CodeInChaos pointed there are plenty of characters which could be considered as letters and numbers. You can use a regular expression if you only want to find A-Za-z0-9.

c# - How do I strip non-alphanumeric characters (including spaces) fro...

c# asp.net .net regex
Rectangle 27 0

<?php
$string = '<tagname attributes></closetagname>';
$clean = str_replace(array('<','>'),array('[',']'), $string);
echo $clean; //[tagname attributes][/closetagname]
?>

Of course, this would replace all instances of < and >.

How do I strip custom tags from text in a submitted html form using PH...

php html perl tags strip
Rectangle 27 0

" May 12".strip!  # => "May 12"

So do .strip! & gsub? That seems a bit inefficient, no?

match

regex - How do I strip spaces & special characters from a string in sp...

ruby regex
Rectangle 27 0

Remove all commas at the end of the string:

str = '1234,,,'
str = str.rstrip(',')
str.rstrip(',')

Yeah, I typed too fast. Fixed with the edit.

How do I strip the comma from the end of a string in Python? - Stack O...

python string
Rectangle 27 0

If you have whitespace/non printing characters then try something like this:

a_string = 'abcdef,\n'
a_string.strip().rstrip(',') if a_string.strip().endswith(',') else a_string.strip()

saves you the trouble of checking string lengths and figuring out slice indexes.

Of course if you do not need to do anything different for strings that do not end in a comma then you could just do:

a_string.strip().rstrip(',')

How do I strip the comma from the end of a string in Python? - Stack O...

python string
Rectangle 27 0

The complete regex for removal of invalid xml-1.0 characters is:

# #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
$str =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
# allowed: [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
$str =~ s/[^\x01-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
# restricted:[#x1-#x8][#xB-#xC][#xE-#x1F][#x7F-#x84][#x86-#x9F]
$str =~    s/[\x01-\x08\x0B-\x0C\x0E-\x1F\x7F-\x84\x86-\x9F]//go;

How can I strip invalid XML characters from strings in Perl? - Stack O...

xml perl
Rectangle 27 0

If you use an XML library to build your XML (as opposed to string concatenation, simple templates, etc), then it should take care of that for you. There is no point in reinventing the wheel.

@David: do these libraries simply strip the control characters from the incoming string?

As far as I'm aware, XML::LibXML doesn't do anything to text node content apart from reject it if it contains invalid characters. I'd be suprised if the other libraries did anything either.

newt, that's the point of using an XML library in the first place.

Of course it is, but he was asking about how to ensure that he didn't get this problem by ensuring that the text content didn't contain invalid characters.

@newt: I'm not completely sure what you mean by "this problem". I see XML::LibXML stripping out the "illegal" characters, except for nul, which it treats as the end of the data :(

How can I strip invalid XML characters from strings in Perl? - Stack O...

xml perl
Rectangle 27 0

Instead of reading the entire thing into memory as a big (potentially huge) string, consider a stream based approach instead.

Open the input stream and read a line at a time, making your replacements as needed. Open an output stream and write the modified line into it. Something like:

static void Main( string[] args )
{
    using( var inFs = File.OpenRead( @"C:\input.txt" ) )
    using( var reader = new StreamReader( inFs ) )
    using( var outFs = File.Create( @"C:\output.txt" ) )
    using( var writer = new StreamWriter( outFs ) )
    {
        int cur;
        char last = '0';
        while( ( cur = reader.Read() ) != -1 )
        {
            char next = (char)reader.Peek();
            char c = (char)cur;
            if( c != '\n' || last == '\r' )
                writer.Write( c );

            last = c;
        }
    }
}

This! You are going to run into memory/speed problems otherwise with massive files like this.

@JonB: Oh, yep, thanks, I misread his example. Regardless, the method is the same. I will whip up some sample code.

This won't work. TextReader.ReadLine will read up to any end of line sequence, including \n or \r by themselves. It also won't return the EOL sequence, so this will just strip out all newlines.

Also, there is a typo in the question title. It should be \n, not \r

c# - How do I strip \n characters from a file? - Stack Overflow

c# .net sed newline
Rectangle 27 0

The method you are looking for is String#chomp. It will remove any carriage return characters from the end of your string.

$("#t").html("#{title.chomp}");

As always, only use html_safe if you are completely sure the title variable is safe... especially since you are using it within a Javascript file! I did not include it in my code example because I just couldn't bring myself to do it. Plus, see the note on APIdock in regards to using html_safe on a variable that could be nil. I would suggest only using it on a string literal.

If there is a chance that your title may contain quotes that need to be escaped before used in Javascript (for instance '"Winter is Coming", I say' which will interpolate into your javascript as .html(""Winter is Coming", I say");) then you should also use the escape_javascript method as suggested in the link @mu is too short provided.

$("#t").html("#{j(title.chomp)}");
title = '"Winter is Coming"'
$("#t").html("\"Winter is Coming\"");

If there is also a chance that there is a carriage return in the middle of the title string, then String#gsub is the way to go.

$("#t").html("#{j(title.gsub(/[\n\r]/, " "))}");
gsub
'Winter\nis\nComing'
'Winter is Coming'
'WinterisComing'

And what happens when title is something like 'Is "Bobby Tables" Real?'? Or if there are newlines in the middle of title?

With those considerations in mind, it almost sounds like there needs to be a helper method in place to handle many different situations (like backslashes as well). What suggestions might you have?

don't reinvent the wheel. use the existing escape_javascript function.

@DGM I did suggest using the existing escape_javascript. I even provided a link to the documentation of it on APIdock. My comment about a helper method was before my edit to the answer.

I just saw all the references to chomp. the OP is asking a question that masks the real problem - I suspect they don't really want to remove newlines so much as make the javascript work. The subject is misleading from the real problem

javascript - How do I strip out a new line (\n) from my code in rails?...

javascript jquery ruby-on-rails ruby