Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Saturday, March 31, 2012

String Trancate

Hi,

In following example I would like to Trancate first 5 digits and last 3 digits.

W04056040-PAY001

So I can get 6040-PAY

Any code behind examples?

Any help will be appriciated.

Thanks.

TekinUse the stringbuilder class (remove method):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuildermemberstopic.asp

Or if you want to do it old school, the intrinsic Mid function works well too.
Hi mtekin303,

There's many ways to solve this problem. Here's two ideas:


[Visual Basic]
Dim temp As String = "W04056040-PAY001"
Response.Write("Sample 1: " + temp.Substring(5,8) + "<br />")
Response.Write("Sample 2: " + temp.Remove(temp.Length-3,3).Remove(0,5))

[C#]
string temp = "W04056040-PAY001";
Response.Write("Sample 1: " + temp.Substring(5,8) + "<br />");
Response.Write("Sample 2: " + temp.Remove(temp.Length-3,3).Remove(0,5));


Hope this helps.
Thank you.

Stringbuilder resolved it.

Thanks again.
Thanks Chuck.

Stringbuilder resolved it.

Monday, March 26, 2012

Stringbuilder or table?

Hi

I am just wondering if i need dynamically create a html table, which way will be more efficient?

A. Use stringbuilder. For example, sb.append("<table border=0>""), for(i=0;i<10;i++){sb.append("<tr><td>abc</td></tr>")}, sb.append("</table>")

B. Use table control, t = new table, for(i=0;i<10;i++){tr=new tablerow, td=new tablecell, td.text="abc",tr.cells.add(td),t.rows.add(tr))

Thanks

~Mike


Hi

Honestly, i dont see it makes huge different

But I think B will quicker than A. because all table, row, cell are object. I think use object to create table will be faster than string built up.


Use the following code to test this:

private void RunTest(int count){ Stopwatch watch =new Stopwatch(); watch.Start();for (int i2 = 0; i2 < count; i2++) { Table table =new Table();for (int i = 0; i < 10; i++) { TableRow tr =new TableRow(); TableCell td =new TableCell(); td.Text ="abc"; tr.Cells.Add(td); table.Rows.Add(tr); } } watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); watch.Start();for (int i2 = 0; i2 < count; i2++) { StringBuilder sb =new StringBuilder(); sb.Append("<table border=0>");for (int i = 0; i < 10; i++) { sb.Append("<tr><td>abc</td></tr>"); } sb.Append("</table>"); } watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds);} 

I did the test with a count of 1500 which should show a difference and let it run 4 times. The outputs are as follows:

22 // Table
4 // StringBuilder


29
5

22
4

26
4

But, as you won't to that 1,500 times, there won't be a lot of difference concerning the performance.


Hi all,

I think the best way is using Table, TableRow and TableCell objects. You can create a table with all cells and rows you want and navigate throw them using their structure. However, if you want to create a simple table that has only two cells or something similar, it's quicker to use StringBuilder object or a string variable.


If the table doesn't need to be a server control, then I think it would be a little more efficient to use a StringBuilder. I?haven't?done?any?tests,?but?writing?out raw?HTML?generally incurs?less?overhead?than?instantiang?controls?for?everything. Any performance difference between the two approaches is going to be negligable though, so personally i'd go for whatever you find the more readable.

A third, and possible better approach would be to put your information into a DataTable and bind it to a GridView, DataList or Repeater.
I'd say A is going to be slightly more efficient but as already pointed out it'll be negligible. I personally like the markup in my code to be neat when viewed so B would probably better achieve that, even if its only for me debugging its output when it goes skewey.

Saturday, March 24, 2012

Stripping characters...

I'm trying to strip the characters from a text box...
but I only know how to apply it to text...
for example..

string fname = @dotnet.itags.org."*\John Doe";
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");

but how can i do it for a passed variable...
like @dotnet.itags.org.Request.Form["fname"];

string fname = @dotnet.itags.org.Request.Form["fname"];
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.comHave you tried

string pattern = @."^.*\\";
string name = Regex.Replace(Request.Form["fname"], pattern," ");

--
Regards

John Timney
Microsoft MVP

"VJ" <vncntj@.hotmail.com> wrote in message
news:%23jugUr6GGHA.532@.TK2MSFTNGP15.phx.gbl...
> I'm trying to strip the characters from a text box...
> but I only know how to apply it to text...
> for example..
>
> string fname = @."*\John Doe";
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
>
> but how can i do it for a passed variable...
> like @.Request.Form["fname"];
> string fname = @.Request.Form["fname"];
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com

Stripping characters...

I'm trying to strip the characters from a text box...
but I only know how to apply it to text...
for example..
string fname = @dotnet.itags.org."*\John Doe";
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");
but how can i do it for a passed variable...
like @dotnet.itags.org.Request.Form["fname"];
string fname = @dotnet.itags.org.Request.Form["fname"];
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.comHave you tried
string pattern = @."^.*\\";
string name = Regex.Replace(Request.Form["fname"], pattern," ");
Regards
John Timney
Microsoft MVP
"VJ" <vncntj@.hotmail.com> wrote in message
news:%23jugUr6GGHA.532@.TK2MSFTNGP15.phx.gbl...
> I'm trying to strip the characters from a text box...
> but I only know how to apply it to text...
> for example..
>
> string fname = @."*\John Doe";
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
>
> but how can i do it for a passed variable...
> like @.Request.Form["fname"];
> string fname = @.Request.Form["fname"];
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com

Stripping Needless Data From HttpContext.Current.Request.Form

I found this nice example of printing data to the default printer of the server when the submit button is clicked. But not sure how to strip out the names of the controls, etc.

Here is the code for those interested:

1Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)23 Dim yPos As Single = 2504 Dim leftMargin As Single = ev.MarginBounds.Left5 Dim topMargin As Single = ev.MarginBounds.Top6 Dim printFont = New Font("Arial", 10)78 Dim sb As StringBuilder = New StringBuilder()910 ' Page title and date/time.11 sb.Append("Maintenance Request")12 sb.Append(Environment.NewLine)13 sb.Append("DateTime: " + DateTime.Now.ToString() + Environment.NewLine)1415 ' Iterate submitted form fields and get field names.16 Dim fieldValue As String17 Dim fieldName As String1819 ' Exclude viewstate and submit button.20 For Each fieldName In HttpContext.Current.Request.Form21 Response.Write("Field Name: " & HttpContext.Current.Request.Form(fieldName) & "<br>")22 If fieldName = "__VIEWSTATE" Or fieldName = "Submit" Then2324 Else25 ' Get the field values.2627 fieldValue = HttpContext.Current.Request.Form(fieldName)2829 ' Add the field names and values to the page.30 ' Break the field values into 50 character segments so it will fit on the paper.31 ' Currently, this only accounts for fields of l50 characters or less.32 ' ISSUE: breaks in the middle of words instead of spaces3334 If fieldValue.Length > 100 Then35 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)36 sb.Append(" " + fieldValue.Substring(50, 50) + Environment.NewLine)37 sb.Append(" " + fieldValue.Substring(100, fieldValue.Length - 100) + Environment.NewLine)3839 ElseIf fieldValue.Length > 50 Then40 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)41 sb.Append(" " + fieldValue.Substring(50, fieldValue.Length - 50) + Environment.NewLine)42 Else43 sb.Append(fieldName + ": " + fieldValue + Environment.NewLine)4445 End If4647 End If4849 Next50 ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, yPos, New StringFormat())5152 End Sub
 
There has to be a better way. Any ideas?
Figured it out. Instead of using HttpContext.Current.Request.Form, I am just using the explicit names of the controls and appending the values to the stringbuilder. If anyone needs help with this, please feel free to contact me.Big Smile

Thursday, March 22, 2012

Structure/Array Question - Easy

I have a Structure and need to create array of Structures for example... How do I accomplish this?

Structure MyStruct

Dim Input1 As String

Dim Input2 As String

End Structure

dim t() as MyStruct

try this

Dim myarray(10)As MyStruct

(if you want the length of the array to be 10)


This is your Structure:

Structure myStruct
Dim Input1 As String
Dim Input2 As String
End Structure

if you want to have an array of your structure with a fix length... then you can do it as..

Dim t(10) As myStruct

if you want to have an array of your structure with dynamic length... i mean if you are not sure what is the length then... you can do it as...

Dim t1() As myStruct
Dim iRow As Integer = 0
While (True) '...an endless loop... just to show example...
ReDim Preserve t1(iRow)
t1(iRow) = New myStruct
t1(iRow).Input1 = ""
t1(iRow).Input2 = ""
iRow += 1
End While

hope it helps./.

Tuesday, March 13, 2012

Struggling With Concept

Hi,

What you need to do is access the usercontrol directly, for example: -

Page_Load(...)
{

((myusercontroltype)this.FindControl("UserControlInstance1")).<Property>
}

Hth,
Phil Winstanley
Microsoft ASP.NET MVP
http://www.myservicescentral.comThank you so much for that, I knew it should be possible, but I must have
missed that Method somehow. I tried this instantly and it work just
absolutely fine.

Thanks Again for your help.

Best Regards - OHM

"Phil Winstanley [Microsoft MVP ASP.NET]" <phil@.winstanley.name> wrote in
message news:caep52$9ft@.odah37.prod.google.com...
> Hi,
> What you need to do is access the usercontrol directly, for example: -
> Page_Load(...)
> {
> ((myusercontroltype)this.FindControl("UserControlInstance1")).<Property>
> }
> Hth,
> Phil Winstanley
> Microsoft ASP.NET MVP
> http://www.myservicescentral.com