Convert a String into an Array of Characters

Converting String to Character array in Java

Use this:

String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);

Split string into array of character strings

"cat".split("(?!^)")

This will produce

array ["c", "a", "t"]

Convert String into Array of Strings in C

So we basically loop through the string, and check if we found the "split character' and we also check that we didn't find the 'curr_letter' as the next character.

We keep track of the consumed length, the current length (used for memcpy later to copy the current string to the array).

When we find a position where we can add the current string to the array, we allocate space and copy the string to it as the next element in the array. We also add the current_length to consumed, and the current_length is reset.

We use due_to_end to find out if we have a / in the current string, and remove it accordingly.

Try:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
char *str = "a/apple/arm/basket/bread/car/camp/element/...";
char split_char = '/';
char nosplit_char = 'a';

char **array = NULL;
int num_elts = 0;

// read all the characters one by one, and add to array if
// your condition is met, or if the string ends
int current_length = 0; // holds the current length of the element to be added
int consumed = 0; // holds how much we already added to the array
for (int i = 0; i < strlen(str); i++) { // loop through string
current_length++; // increment first
int due_to_end = 0;
if ( ( str[i] == split_char // check if split character found
&& ( i != (strlen(str) - 1) // check if its not the end of the string, so when we check for the next character, we don't overflow
&& str[i + 1] != nosplit_char ) ) // check if the next char is not the curr_letter(nosplit_char)
|| (i == strlen(str) - 1 && (due_to_end = 1))) { // **OR**, check if end of string
array = realloc(array, (num_elts + 1) * sizeof(char *)); // allocate space in the array
array[num_elts] = calloc(current_length + 1, sizeof(char)); // allocate space for the string
memcpy(array[num_elts++], str + consumed, (due_to_end == 0 ? current_length - 1 : current_length)); // copy the string to the current array offset's allocated memory, and remove 1 character (slash) if this is not the end of the string

consumed += current_length; // add what we consumed right now
current_length = 0; // reset current_length
}
}

for (int i = 0; i < num_elts; i++) { // loop through all the elements for overview
printf("%s\n", array[i]);
free(array[i]);
}
free(array);
}

How to convert a string to character array in c (or) how to extract a single char form string?

In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,

char myString[] = "This is some text";

You can access any character as a simple char by using myString as an array, thus:

char myChar = myString[6];
printf("%c\n", myChar); // Prints s

Hope this helps!
David

How to convert a string into an array of individual characters in Bash?

str="Hello world!"
for (( i=0 ; i < ${#str} ; i++ )) {
arr[$i]=${str:i:1}
}

#print
printf "=%s=\n" "${arr[@]}"

output

=H=
=e=
=l=
=l=
=o=
= =
=w=
=o=
=r=
=l=
=d=
=!=

You can assign into array the result of any command using the

mapfile -t array < <(command args)

Unfortunately, defining the custom delimiter -d needs bash 4.4.+. Let say, want break the above string into 2 char pieces - using grep

mapfile -t -d ''  a2 < <(grep -zo .. <<<"$str")
printf "=%s=\n" "${a2[@]}"

output:

=He=
=ll=
=o =
=wo=
=rl=
=d!=

Convert a String into an Array of Characters

You will want to use str_split().

$result = str_split('abcdef');

http://us2.php.net/manual/en/function.str-split.php

Recommended way to convert String into Array of chars in Javascript

Well, a quick profile in Node will tell you that word.split('') is the fastest, followed by [...word], then Array.from(word). This was my benchmark:

const word = '1783638745638476548765873hdsgkfj';console.time('arrayFrom');for (let i = 0; i < 10000; i++) {  const arrayFrom = Array.from(word);}console.timeEnd('arrayFrom');console.time('split');for (let i = 0; i < 10000; i++) {  const stringSplit = word.split('');}console.timeEnd('split');console.time('spread');for (let i = 0; i < 10000; i++) {  const spread = [...word];}console.timeEnd('spread');


Related Topics



Leave a reply



Submit