Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

[C#] 4 ways to remove "\r" or "\n" or "\t" new line or line break and tab characters from a string?

If you have a string, like this:

string str = "Line 1\n   Tab it.\t Break.\n'


How to remove breakline or tab in this string? 

So you can using some methods:

Method 1: quick to code
string newstr = Regex.Replace(str, @"\t|\n|\r", ""); // \t is tab, \n and \r is new line
Method 2: for New line only
string newstr = str.Replace(Environment.NewLine, "");
Method 3: Easy to code
str = str.Replace("\n", String.Empty);
str = str.Replace("\r", String.Empty);
str = str.Replace("\t", String.Empty);
Method 4: For speed and low memory, because is StringBuilder best!

var sb = new StringBuilder(s.Length);

foreach (char i in str)
    if (i != '\n' && i != '\r' && i != '\t')
        sb.Append(i);

str = sb.ToString();


[C#] 3 best ways to remove all special characters from string

You want to remove characters \ /:*?"<>|!@#$%^&() from a string?
Very easy! Follow some method:

1. Remove all special characters
string rExp = "[^\w\d]";
string tmp = Regex.Replace(n, rExp, "");

2. Remove all special characters but allow some
Example: Allow . and _
Regex.Replace(input, "[^a-zA-Z0-9._]", string.Empty)

3. Easy function remove all special characters, but allow "_"

 public static string Remove_Special_Characters(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }

Debugging mode not can not work with F10 and F11 Visual Studio 2010

(Continue with fix-debugger-not-working)
When I going to debug with Visual Studio 2010, It work!
But when I want to run line by line at break point. I press F11 and F10... => It not work!!!

Solutions to fix:
1. Install http://support.microsoft.com/default.aspx/kb/957912
2. Change the port of the Visual Studio development server (go to menu Project > Properties > Web > *Server: User Visual Studio Development Server: Specific port = 12345)

What is “ops/sec”?

Today, my friend ask me: "What is ops/sec?"
:D

ops/sec is operations per second. When you test benmark. It's meanoften how many time execute a test in a second.

ops/sec often using for benchmark devices as smartphone, GPU, CPU, read write disk, or test for framework,...

Now you can answer "What is ops/sec?"!

[C#] Remove all special character \ /:*?"<>|!@#$%^&() from a string

You want to remove characters \ /:*?"<>|!@#$%^&() from a string?
Very easy! Follow some method:

1. Remove all special characters
string rExp = "[^\w\d]";
string tmp = Regex.Replace(n, rExp, "");

2. Remove all special characters but allow some
Example: Allow . and _
Regex.Replace(input, "[^a-zA-Z0-9._]", string.Empty)

3. Easy function remove all special characters, but allow "_"

 public static string Remove_Special_Characters(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }

Fix error: "Could not load file or assembly 'Microsoft.Web.Extensions..."

One day, you are coding... And run it! But an error show!

Could not load file or assembly 'Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
You don\'t worry! And try some solutions

1. Review web.config

- Install the latest Asp.NET Ajax - Open my web.config in my production server and replace this line

<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"></add> 

by
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"></add> 
PublicKeyToken is in capitals....

2. Check your bin folder
Copy the System.Web.Extensions.dll into the app's bin directory if you have.

 3. Install ASP.NET AJAX 1.0
Check you had install it? If not, you download it at ASP.NET AJAX 1.0 

4. If you using Ajaxcontrol toolkit
Download and install it at Ajaxcontrol toolkit

Try one solution or try all until you resolve an error!

[C#] How to fix "object reference not set to an instance of an object"

Oh! No...
When you coding with .Net. Many time you meet an error: "object reference not set to an instance of an object".
What happen???

Example 1: Convert null to int
string query = "Select * from USERS where ID = '" + txtUID.Text + "'";
SqlCommand cmd = new SqlCommand(query , CN);
int count = (int)cmd.ExecuteScalar();//Error here
What problem? ExecuteScalar return to null when record not found in database. In case, you can using Convert class
Convert.ToInt32(cmd.ExecuteScalar());//It will return 0 when ExecuteScalar() return null
Example 2: Try get object properties when this object is null
var str= null;
if(str!="")// Error here
{
//do some thing
}
Resolve the problem: You can try
var str= null;
if(str !=null && str!="")// Error here
{
//do some thing
}
Or try this
var str= null;
if(!String.IsNullOrEmpty(str))// Error here
{
//do some thing
}
Example: Very much error!
// About 10000 lines
Resole all with
try{
//All code here
}
catch (NullReferenceException ex)
        {
            ShowMessageBox("Error" + ex.Message);
        }