Saturday, March 31, 2012

String String Function

All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that
are available in ASP are supported by ASP.NET as well except for the
string function 'String'.

The 'String' function takes this form:

String(number, character)

which returns a string consisting of *character* which is repeated
*number* times. For e.g.

String(3, "$")

would return

$$$

or

String(5, "A")

would return

AAAAA

But when I try to use this function in ASP.NET, the following error
gets generated

'String' is a class type and cannot be used as an expression.

It's pretty obvious what makes ASP.NET generate the above error but
how do I make use of this 'String' function in ASP.NET? Is there any
equivalent string function in ASP.NET?

Actually my main intention is to replace the first 10 characters of a
text with dots irrespective of the no. of characters in the text (or
the 'Len' of the text) which is why I needed to use the 'String'
function. Had ASP.NET supported the 'String' function, then the
following would have served my purpose:

strText = "The quick brown fox jumps over the lazy dog."
strText = String(10, ".") & Mid(strText, 11, Len(strText))

I can't use the 'Replace' function since the first 10 characters (or,
for that matter, the entire text) can be a combination any characters
(even non-alphanumeric).string str = new string(' ', 10);

<rn5a@dotnet.itags.org.rediffmail.comwrote in message
news:1170864460.723705.243170@dotnet.itags.org.k78g2000cwa.googlegr oups.com...

Quote:

Originally Posted by

All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that
are available in ASP are supported by ASP.NET as well except for the
string function 'String'.
>
The 'String' function takes this form:
>
String(number, character)
>
which returns a string consisting of *character* which is repeated
*number* times. For e.g.
>
String(3, "$")
>
would return
>
$$$
>
or
>
String(5, "A")
>
would return
>
AAAAA
>
But when I try to use this function in ASP.NET, the following error
gets generated
>
'String' is a class type and cannot be used as an expression.
>
It's pretty obvious what makes ASP.NET generate the above error but
how do I make use of this 'String' function in ASP.NET? Is there any
equivalent string function in ASP.NET?
>
Actually my main intention is to replace the first 10 characters of a
text with dots irrespective of the no. of characters in the text (or
the 'Len' of the text) which is why I needed to use the 'String'
function. Had ASP.NET supported the 'String' function, then the
following would have served my purpose:
>
strText = "The quick brown fox jumps over the lazy dog."
strText = String(10, ".") & Mid(strText, 11, Len(strText))
>
I can't use the 'Replace' function since the first 10 characters (or,
for that matter, the entire text) can be a combination any characters
(even non-alphanumeric).
>


On Feb 7, 9:17 pm, "Aidy" <a...@dotnet.itags.org.noemail.xxxa.comwrote:

Quote:

Originally Posted by

string str = new string(' ', 10);
>
<r...@dotnet.itags.org.rediffmail.comwrote in message
>
news:1170864460.723705.243170@dotnet.itags.org.k78g2000cwa.googlegr oups.com...
>
>
>

Quote:

Originally Posted by

All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that
are available in ASP are supported by ASP.NET as well except for the
string function 'String'.


>

Quote:

Originally Posted by

The 'String' function takes this form:


>

Quote:

Originally Posted by

String(number, character)


>

Quote:

Originally Posted by

which returns a string consisting of *character* which is repeated
*number* times. For e.g.


>

Quote:

Originally Posted by

String(3, "$")


>

Quote:

Originally Posted by

would return


>

Quote:

Originally Posted by

$$$


>

Quote:

Originally Posted by

or


>

Quote:

Originally Posted by

String(5, "A")


>

Quote:

Originally Posted by

would return


>

Quote:

Originally Posted by

AAAAA


>

Quote:

Originally Posted by

But when I try to use this function in ASP.NET, the following error
gets generated


>

Quote:

Originally Posted by

'String' is a class type and cannot be used as an expression.


>

Quote:

Originally Posted by

It's pretty obvious what makes ASP.NET generate the above error but
how do I make use of this 'String' function in ASP.NET? Is there any
equivalent string function in ASP.NET?


>

Quote:

Originally Posted by

Actually my main intention is to replace the first 10 characters of a
text with dots irrespective of the no. of characters in the text (or
the 'Len' of the text) which is why I needed to use the 'String'
function. Had ASP.NET supported the 'String' function, then the
following would have served my purpose:


>

Quote:

Originally Posted by

strText = "The quick brown fox jumps over the lazy dog."
strText = String(10, ".") & Mid(strText, 11, Len(strText))


>

Quote:

Originally Posted by

I can't use the 'Replace' function since the first 10 characters (or,
for that matter, the entire text) can be a combination any characters
(even non-alphanumeric).- Hide quoted text -


>
- Show quoted text -


Thanks a lot, Aidy :-)

String String Function

All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that
are available in ASP are supported by ASP.NET as well except for the
string function 'String'.
The 'String' function takes this form:
String(number, character)
which returns a string consisting of *character* which is repeated
*number* times. For e.g.
String(3, "$")
would return
$$$
or
String(5, "A")
would return
AAAAA
But when I try to use this function in ASP.NET, the following error
gets generated
'String' is a class type and cannot be used as an expression.
It's pretty obvious what makes ASP.NET generate the above error but
how do I make use of this 'String' function in ASP.NET? Is there any
equivalent string function in ASP.NET?
Actually my main intention is to replace the first 10 characters of a
text with dots irrespective of the no. of characters in the text (or
the 'Len' of the text) which is why I needed to use the 'String'
function. Had ASP.NET supported the 'String' function, then the
following would have served my purpose:
strText = "The quick brown fox jumps over the lazy dog."
strText = String(10, ".") & Mid(strText, 11, Len(strText))
I can't use the 'Replace' function since the first 10 characters (or,
for that matter, the entire text) can be a combination any characters
(even non-alphanumeric).string str = new string(' ', 10);
<rn5a@.rediffmail.com> wrote in message
news:1170864460.723705.243170@.k78g2000cwa.googlegroups.com...
> All the String functions like 'Len', 'Left', 'Right', 'Mid' etc. that
> are available in ASP are supported by ASP.NET as well except for the
> string function 'String'.
> The 'String' function takes this form:
> String(number, character)
> which returns a string consisting of *character* which is repeated
> *number* times. For e.g.
> String(3, "$")
> would return
> $$$
> or
> String(5, "A")
> would return
> AAAAA
> But when I try to use this function in ASP.NET, the following error
> gets generated
> 'String' is a class type and cannot be used as an expression.
> It's pretty obvious what makes ASP.NET generate the above error but
> how do I make use of this 'String' function in ASP.NET? Is there any
> equivalent string function in ASP.NET?
> Actually my main intention is to replace the first 10 characters of a
> text with dots irrespective of the no. of characters in the text (or
> the 'Len' of the text) which is why I needed to use the 'String'
> function. Had ASP.NET supported the 'String' function, then the
> following would have served my purpose:
> strText = "The quick brown fox jumps over the lazy dog."
> strText = String(10, ".") & Mid(strText, 11, Len(strText))
> I can't use the 'Replace' function since the first 10 characters (or,
> for that matter, the entire text) can be a combination any characters
> (even non-alphanumeric).
>
On Feb 7, 9:17 pm, "Aidy" <a...@.noemail.xxxa.com> wrote:
> string str = new string(' ', 10);
> <r...@.rediffmail.com> wrote in message
> news:1170864460.723705.243170@.k78g2000cwa.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> - Show quoted text -
Thanks a lot, Aidy :-)

String Stripping

Hi,

I am trying to create a page that will check user input against a value in database. This page will have a text box for user to enter serial #.
Serial # length is variable but last 3 digit is always number.
For examples:
W04051234-PAY001 in this example code is 1234-PAY
W05051234-PAT002 in this example code is 1234-PAT

A12056040-G001 in this example code is 6040-G
B11056080-SU001 in this example code is 6080-s

Which is the best approach to extract CODE from user input?

Any help will be appriciated.

Thank

TekinUse the Regular Expression class, RegExp with a regular expression that looks for:
4 digits (or is it 3?) followed by
dash
followed by 1 or letters

That expression is:
\d{4}\-[A-Za-z]+
Actually,

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

For examples:
W04051234-PAY001 in this example code is 1234-PAY
If you are attempting to strip a specific group of characters, the String class has numerous useful methods. I encourage you to read about the String class in the .net docs. I think the SubString() and Length() methods will be useful.

String to a Class name

Hi,

I've got some classes to access different types of databases and I have a string variable that define the type of db I want to access. What I want to do if is pssible use the value of this variable to access the class. I have the following code now.


switch (dbType)
{
case "MSSQL": return MSSQL.msSQLDataSet(sqlString);
case "ACCESS": return ACCESS.msSQLDataSet(sqlString);
case "ORACLE": return ORACLE.msSQLDataSet(sqlString);
case "MYSQL": return MYSQL.msSQLDataSet(sqlString);

default: return null;
}

I want to do something like


SomekindOfTransformation(dbType).msSQLDataSet(sqlString)

instead of using theswitch statement

Thanks,Hi Arty,

You can achieve it using Object.GetType and Type.InvokeMember. VisitObject.GetType andType.InvokeMember for complete details.

Here is a sample code:

using System;
using System.Reflection;

// This sample class has a field, constructor, method, and property.
class MyType
{
Int32 myField;
public MyType(ref Int32 x) {x *= 5;}
public override String ToString() {return myField.ToString();}
public Int32 MyProp
{
get {return myField;}
set
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", value, "value must be > 0");
myField = value;
}
}
}

class MyApp
{
static void Main()
{
Type t = typeof(MyType);
// Create an instance of a type.
Object[] args = new Object[] {8};
Console.WriteLine("The value of x before the constructor is called is {0}.", args[0]);
Object obj = t.InvokeMember(null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
Console.WriteLine("Type: " + obj.GetType().ToString());
Console.WriteLine("The value of x after the constructor returns is {0}.", args[0]);

// Read and write to a field.
t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetField, null, obj, new Object[] {5});
Int32 v = (Int32) t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetField, null, obj, null);
Console.WriteLine("myField: " + v);

// Call a method.
String s = (String) t.InvokeMember("ToString",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
Console.WriteLine("ToString: " + s);

// Read and write a property. First, attempt to assign an
// invalid value; then assign a valid value; finally, get
// the value.
try
{
// Assign the value zero to MyProp. The Property Set
// throws an exception, because zero is an invalid value.
// InvokeMember catches the exception, and throws
// TargetInvocationException. To discover the real cause
// you must catch TargetInvocationException and examine
// the inner exception.
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {0});
}
catch (TargetInvocationException e)
{
// If the property assignment failed for some unexpected
// reason, rethrow the TargetInvocationException.
if (e.InnerException.GetType() !=
typeof(ArgumentOutOfRangeException))
throw;
Console.WriteLine("An invalid value was assigned to MyProp.");
}
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {2});
v = (Int32) t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty, null, obj, null);
Console.WriteLine("MyProp: " + v);
}
}

Hope this helps,
Appan
Thank you Appana, that helped me a lot.

String to Base64

Hi,

I need to convert an ASCII string to Base64 encoding in VB .NET. All of the references I have found show how to convert a binary data stream to Base64, but I only need to convert a text string. What is the simplest way to do this?

Hi,

It is always advisable to convert to binary first before performing encoding or encrypting operations. You can convert the string to binary and then encode to Base64.

Hope this helps,

Vivek

string to byte array conversion

I've a string similiar to "A509DE5B" (Length == 8 ) where each 2 characters are 1 hex number. How to convert such string into array of bytes (Lenght == 4)? In C# please, it's important...Byte.Parse()?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystembyteclassparsetopic.asp
Hi LesioS!

I think you should try out something like this (I haven't tested it out myself, not sure if it works!!)


string sBytes = "A509DE5B";

Byte[] bytes = new Byte[(int)(sBytes.Length/2)];

int i = 0, j = 0;

while(i < sBytes.Length)
{
bytes[j] = Byte.Parse(str.Substring(i,2), NumberStyles.HexNumber);
i += 2;
}

Maybe it would work..

Good luck!
Oh, sorry, there's lots of mistakes in the code earlier code.

I tested this, and it worked, so be my guest! :)


string sBytes = "A509DE5B";
int i=0, j=0;
Byte[] bytes = new Byte[(int)(sBytes.Length/2)];
while(i < str.Length)
{
bytes[j] = Byte.Parse(sBytes.Substring(i,2), NumberStyles.HexNumber);
i += 2;
}


byte[] bytes = new byte[str.Length / 2];
for(int i = 0; i < str.Length / 2; i++)
bytes [i] = Byte.Parse(str.Substring(i * 2, 2), NumberStyles.HexNumber);

works fine... THX

But I wonder why there's no such function like ToCharArray for string object which produces byte array. In many cases functions from .NET Framework uses byte arrays, not char arrays :(

Try this:

// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}

Good luck!

Newbie


I have tried this

System.Text.

ASCIIEncoding encoding =new System.Text.ASCIIEncoding();string queryStringKey =ConfigurationManager.AppSettings["queryStringKey"];byte[] key = encoding.GetBytes(queryStringKey);return key;

to achieve this

//byte[] Key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6 };

but it is not working. What am I not getting? Newbie

String to convert into Double

Hello All,

I am having string object coming from a DataSet. It can contain either the Numeric Strings (like "123","123.23",...) or even the Character string (Like "America","Britain"...).

Now, I am assigning this value to another column in the dataset whose datatype is Doube. Now, I wanted to check before assigning whether whatever the String I am getting from the Source column can it be converted to Double or not?

i.e I want to know is there any object/method in .Net Framework using which I can know whether I can convert one datatype to another datatype or not?

Any help regarding this would be great.

Thanks in Advance,
Areef.Hi Areef,
I am not familiar with such an object, but you can use Double.TryParse to perform your task.
Hi pmd_areef,

Try the following for testing if value is a Double. Example:


[C#]
/// <summary>
/// Is the value supplied numeric (Integer, Double).
/// </summary>
/// <param name="number">Value to test</param>
/// <returns></returns>
public bool IsNumeric(object number)
{
double tempResult;
return IsNumeric(number, out tempResult);
}

public bool IsNumeric(object number, out double result)
{
return Double.TryParse(number.ToString(), System.Globalization.NumberStyles.Any,
System.Globalization.NumberFormatInfo.InvariantInfo, out result);
}


Hope this helps.