Showing posts with label characters. Show all posts
Showing posts with label characters. Show all posts

Saturday, March 31, 2012

Trim all characters after the first " . " help need...

I need a really basic line of code which i can not find. I need to do is remove ALL characters after and inc the characters "." found in a variable. i have a variable which holds device with DNS name such as E096064-FWSM2.netdevice.companyname.com i want the variable to just hold the name E096064-FWSM2 the extension after the first "." can very greatly so i can not specify all possible options as i just do not know what they are going to be.

really hope someone can help me

paul

code below did the job

DNSnode = Left(fullDNSname, InStr(fullDNSname, ".")-1)

Trimming

I am trying to trim a session variable to get the last three characters.Here is the code:

Dim CancelTrim()AsChar = {"","cc9"}

If Session("confirm").trimend(CancelTrim) ="ca1"Then

Label1.Text ="it was cancelled"

EndIf

It's not working though, go figure. So, how would I get these to trim off the last three characters so I can do what I need to do?

Thanks

Hello,

so what does it return from ther TrimEnd call? TrimEnd trims from the end of the string, so isn't that from the wrong direction if you want the last three characters? So maybe TrimStart would work better?

On the other hand, couldn't you write something like:

Dim ConfirmStringAs String = Cstr(Session("confirm"))Dim LastThreeCharsAs String = ConfirmString.Substring(ConfirmString.Length - 3)If LastThreeChars ="ca1"Then'CheckEnd If

Trimming a string to remove special & non-numeric characters

the string format is "XXXXXX-06-X-1234". how can i trim it so that it removes all the dashes and non-numeric characters? the result after such a trim would be "061234"

thanks in advance.

Create a function that removes any characters you don't want. In this case, it's pretty easy:

Function ParseDigits(ByVal strRawValue asString)As String Dim strDigitsAs String =""If strRawValue =Nothing Then Return strDigitsFor Each cAs Char In strRawValue.ToCharArray()If c.IsDigitThen strDigits &= cEnd If Next c' return the number string, or "" if no numbers were in the string.Return strDigitsEnd Function

You can always iterate through each character and build a new string (Use the StringBuilder class). You could also try using RegEx.Replace method to replace all non-numeric characters with an empty string. I don't know which method would perform faster, if I had to guess, I'd say the manual iteration approach.

Does anyone know an easier way to do this?


quickest way to do this (and best performance is with a Regex:

C#:

string initialString ="XXXXXX-06-X-1234";System.Text.RegularExpressions.Regex nonNumericCharacters =new System.Text.RegularExpressions.Regex(@."\D");string numericOnlyString = nonNumericCharacters.Replace(initialString, String.Empty);

VB:

Dim initialString asString ="XXXXXX-06-X-1234"Dim nonNumericCharactersAs New System.Text.RegularExpressions.Regex(@."\D")Dim numericOnlyStringAs String = nonNumericCharacters.Replace(initialString,String.Empty)

Enjoy.


I think you did that backwards. You're having him match all numbers and replace them with an empty string. He WANTS numbers!

C#:

string initialString ="XXXXXX-06-X-1234";System.Text.RegularExpressions.Regex nonNumericCharacters =new System.Text.RegularExpressions.Regex(@."[^0-9]");string numericOnlyString = nonNumericCharacters.Replace(initialString, String.Empty);

VB:

Dim initialString asString ="XXXXXX-06-X-1234"Dim nonNumericCharactersAs New System.Text.RegularExpressions.Regex("[^0-9]")Dim numericOnlyStringAs String = nonNumericCharacters.Replace(initialString,String.Empty)

the "d" is capitalised:

/d = [0-9]

and

/D = [^0-9]

either way, you can use /D or [^0-9]


bipro/ds2goat

thanks for the help! that function works great. one more question though. sometimes the first set of characters (XXXXXX-) can have a number. is there anyway to totally ignore those first 6 characters, then trim the remaining characters using the function above?

example: "221300-06-D-1234" would come out as "061234"

thanks again for the help


bipro/ps2goat

thanks for the help! that function works great. one more question though. sometimes the first set of characters (XXXXXX-) can have a number. is there anyway to totally ignore those first 6 characters, then trim the remaining characters using the function above?

example: "221300-06-D-1234" would come out as "061234"

thanks again for the help


Is this a consistent thing? I.e., do all of the strings you are working with have those six characters you want to ignore? What kind of consistent pattern is there in the data?

yes, it is a consistent pattern (AAANNN-NN-A-NNNN), where "A" is alpha & "N" is numeric. i want to always ignore the first 6 characters, whether they be alpha or numeric.

thanks again.


You can get rid of the first 6 characters by

string _test = "AAANNN-NN-A-NNNN";
_test = _test.Replace(_test.Substring(0,6), string.Empty);

Now apply the above regular expression on the _test;

Thanks


works perfectly! here's my entire function:

Dim testAs String ="SPM300-06-D-1234"test = test.Replace(test.Substring(0, 6),String.Empty)Dim nonNumericCharactersAs New System.Text.RegularExpressions.Regex("\D")Dim numericOnlyStringAs String = nonNumericCharacters.Replace(test,String.Empty)Response.Write(numericOnlyString)
thanks again for all the help. i really appreciate it.
test = test.Replace(test.Substring(0, 6),String.Empty)
It seems to me that it makes more sense to do one operation than 2. e_screw told you to do

but it would read better if you did this instead:

test = test.Substring(6)
This will return all characters after index 6 (or the seventh character, since you don't need the first hypen either). Good programming is being lazy, and being lazy means writing less code to do what you want (less = more efficient). =)

Wednesday, March 28, 2012

Trmming a Field in .NET

In Classic ASP I can take a description field and only display the first 260 characters like this: <%= Left((rsFeature.Fields.Item("description").Value), 260) %>...

Any ideas how I would accomplish this in .NET?

Thanks for any help.

try

<%= rsFeature.Fields.Item("description").Value.ToString().Substring(0,260) %>

substring will split up a string given two parameters, start index, and length.

hth,

mcm


You don′t have Left() and Right() in .NET, but you can use Substring.

string s ="Hello, world!";Console.WriteLine(s.SubString(0, 5));//prints "Hello"

So I tried this: <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>...

and get this error:

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.Parameter name: length


What if the description string does not contain 260 characters?


<%# dsFeature.FieldValue("description", Container).ToString().Substring(0,100) %>... works great.

However, if I use <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>... I get an error because some of my descriptions have less than 260 characters. Any idea how I can handle this?

Thanks!


what you should do is use the code-behind to evaluate your string expression

so

string tmpStr = "";
if(theString.length > 260){
tmpStr = theString.Substring(0,260);
}else{
tmpStr = thisString.ToString();
}

and just bind tmpStr to your field in the front end.

hth,
mcm


Got it! Thanks!