Posts

Showing posts with the label string

remove non ASCII characters from a String

Hi guys I had a problem with removing non-utf8 characters from string, which are not displaying properly. Characters are like this 0x97 0x61 0x6C 0x6F (hex representation). I am getting some encoded value from url. Let assume it's encoded email and value is ankitchauhan22@gmail.com When I tried to find out the length of this string as $email = somefunction($encodedStringFromUrl); $length = strlen($email); print $length; I was shocked. It's printing 37 instead of 24. Than I printed each index of this string on the string but after 24 character, nothing printed. I used trim() to remove whitespace but didn't work. Then I tried something which worked for me. This is a little snippet that will remove any non-ASCII characters from a string. $string = "ankitchauhan22@gmail.com รครณ"; $string = preg_replace('/[^(\x20-\x7F)]*/','', $string); Now It's printing 24.

split string in java

Java equivalent of PHP explode function In PHP we can easily break a long string into smaller parts by using the   explode()   function of PHP. $longstring="I am a cool guy " ; $brokenstring=explode(" ", $longstring); After the execution of the second command the variable $brokenstring is an array such that, $brokenstring[0]="I" $brokenstring[1]="am" $brokenstring[2]="a" $brokenstring[3]="cool" $brokenstring[4]="guy" In java we use String.split() function that achieves the same objective. class splitString     {         public static void main(String[] args)             {             String s1 = " I am a cool guy ";             String[] array = s1.split(" ");                    for(int i=0; i    ...

StringList class and use of it

This class can be used to manage lists of strings. It can take a list of strings separated by line breaks and extracts key value pairs separated by the = character.The class can return the list of strings as an array, add more strings to the list, and save or load the list from a file. stringlist_class.php class StringList{ private $Values=array(); private $Text; private $Lines=array(); /** * It's constructor func * * @param mixed $string String you want parse. * @return StringList */ public function StringList($string){ $this->Text=$string; $this->Lines=explode("\n",$string); $Count=count($this->Lines); Foreach ($this->Lines As $LineText){ $EsitPos=strpos($LineText,'='); $this->Values[substr($LineText,0,$EsitPos)]=substr($LineText,$EsitPos+1,strlen($LineText)); } return $this; } /** * Return The Text Or Set Text (ReCreate Object). * * @param mixed $SetTex it will change if it...