+ Start a Discussion
termfrequencytermfrequency 

String processing at character level

String processig at character level, is there any reason why the character level manipulation is missing in String in apex?

 

ex: String temp = 'apex string';

 

temp.charAt(0)

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

You can do it with a loop and the substring method.

 

 

String temp='apex string';
Integer i=0;
do{
	if(i<temp.length()){
		//temp.substirng(i,i+1) is what you use to get each character
                system.debug('\n\n'+temp.substring(i,i+1));
	}
	i=i+1;
}while(i < temp.length());

 

 

All Answers

JimRaeJimRae

You can do it with a loop and the substring method.

 

 

String temp='apex string';
Integer i=0;
do{
	if(i<temp.length()){
		//temp.substirng(i,i+1) is what you use to get each character
                system.debug('\n\n'+temp.substring(i,i+1));
	}
	i=i+1;
}while(i < temp.length());

 

 

This was selected as the best answer
termfrequencytermfrequency

Hi Jim,

 

Actually, reading a string  char by char is fundamental CS, i'm surprised to see apex is missing it, if there is any good reason to not to support it, im very interested in knowing it.

 

thanks

ram

 

bob_buzzardbob_buzzard

I suspect one reason is that SalesForce doesn't have the concept of a character primitive.

 

I agree it would be useful - generating soundex keys is rather painful otherwise.

Menachem Mendel ShanowitzMenachem Mendel Shanowitz
Here's a simple method: You can convert it to an array of single characters.
String temp = 'apex string';
List<String> tempArray = temp.split('');
Now you can access it by index or loop through it.
// get second character (p)
system.debug(tempArray[1]);

// loop through all characters
for (String character : tempArray) {
  system.debug(character);
}