Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to find the seventh character of a string in asp.net with c#.
My string is LD2007123451. Total Length of the string may vary.
I want to get the seventh charcter of the same

What I have tried:

Code is as below:
My string is
LD2007123451
.
a=a.substring(0,6)
Posted
Updated 12-Mar-20 1:15am

Well ... a string is a collection of characters, and it has an indexer, so:
C#
string str = "LD2007123451";
if (str.Length >= 7)
    {
    char c = str[6];
    }
 
Share this answer
 
String.Substring Method (System) | Microsoft Docs[^]
C#
seventhCharacter = a.Substring(6, 1);
// 6 because 7th character and 0-based indexing
// 1 because you only want one character
 
Share this answer
 
What you are looking for is the String.Substring(x,y) method. What this method will do is grab a portion of the string at character (x) for a length of (y) characters.

This is a fairly low-level operation and it is incredibly easy to get errors for this:
1. If x < 0 then you will throw an Argument Out of Range Exception
2. This error will also occur if x(start character) + y(length)is longer than the string

Reference:
String.Substring Method (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900