1

"01 ABC"
"123 DEF"

How can I get the value of "01" and "123" in asp.net?

I have tried the following code:

Dim ddlSession As String = "01 ABC"
Dim getSpaceIndex As Integer = ddlSession.IndexOf(" ")
Dim getSessionCode As String = ddlSession.Remove(getSpaceIndex)

but the getSpaceIndex will keep return -1 to me...

1

3 Answers 3

2

It depends on what exactly you want.

If you want the substring until the space character, you can use:

string ddlSessionText = "01 ABC";
string sessionCode = ddlSessionText.Substring(0, ddlSessionText.IndexOf(' '));
Sign up to request clarification or add additional context in comments.

3 Comments

@Wee - Then this should suit you well.
return error: {"Length cannot be less than zero. Parameter name: length"}
@Wee - You probably don't actually have a space in your input string. Double-check that.
1
string.Substring(0, string.IndexOf(" "));

Comments

1

You can use split.

Assuming you are using C# in your ASP.NET page:

string s = "01 ABC";
s.split(' ')[0]; // will give you 01
s = "123 DEF";
s.split(' ')[0]; // will give you 123

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.