Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first 10 characters of a string?

Tags:

c

string

I have not been able to find any information with a web-search. Where should I be looking?

like image 530
user2341069 Avatar asked May 02 '13 22:05

user2341069


People also ask

How do I find the first 10 characters of a string?

To get the first 10 characters, use the substring() method. string res = str. Substring(0, 10);

How do you get the first 20 characters of a string?

slice() method to get the first N characters of a string, e.g. str. slice(0, 3) . The slice() method takes the start and stop indexes as parameters and returns a new string containing a slice of the original string.

How do I retrieve the first 5 characters from a string?

string str = yourStringVariable. Substring(0,5);

How do you extract the first 10 characters of a string in Python?

To access the first n characters of a string in Python, we can use the subscript syntax [ ] by passing 0:n as an arguments to it. 0 is the starting position of an index. n is the number of characters we need to extract from the starting position (n is excluded).


3 Answers

char myString[256]; // Input string
char dest[256];     // Destination string

strncpy(dest, myString, 10);
dest[10] = 0; // null terminate destination
like image 103
Lefteris E Avatar answered Oct 23 '22 18:10

Lefteris E


char source[] = "abcdefthijklmn";
char target[100];

strncpy(target, source, 10);
target[10] = '\0'; // IMPORTANT!
like image 33
tianz Avatar answered Oct 23 '22 19:10

tianz


Adding to the above answers:

char* someString = "your string goes here";

int main() 
{
  int n = 10;
  printf("(%.*s)\n", n, someString);

  return 0;
}
like image 40
Rahul Avatar answered Oct 23 '22 18:10

Rahul