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:
So you can using some methods:
Method 1: quick to code
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 onlystring newstr = str.Replace(Environment.NewLine, "");
Method 3: Easy to codestr = 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();
12:21 AM
.Net
,
ASP
,
ASPX
,
C#
,
c# string
,
Special string
,
Visual Studio
[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
2. Remove all special characters but allow some
Example: Allow . and _
3. Easy function remove all special characters, but allow "_"
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();
}
11:12 AM
C#
,
c# string
,
Special string
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)
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)
11:12 AM
.Net
,
ASPX
,
C#
,
debug
,
debugger
,
Visual Studio
,
Visual Studio 2010
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?"!
: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?"!
5:03 AM
Benchmark
,
C#
,
Javascript
,
software
[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
2. Remove all special characters but allow some
Example: Allow . and _
3. Easy function remove all special characters, but allow "_"
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();
}
10:58 PM
C#
,
c# string
,
Special string
Fix error: "Could not load file or assembly 'Microsoft.Web.Extensions..."
One day, you are coding...
And run it!
But an error show!
1. Review web.config
- Install the latest Asp.NET Ajax - Open my web.config in my production server and replace this line
by
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!
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!
12:17 PM
.Net
,
ASPX
,
C#
,
dll
,
Extensions
[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
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 hereWhat 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 nullExample 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 linesResole all with
try{
//All code here
}
catch (NullReferenceException ex)
{
ShowMessageBox("Error" + ex.Message);
}
Subscribe to:
Posts
(
Atom
)