Categories
functions php

PHP: Function to replace pattern of characters in a String

The functions str_replace is used to replace particular sequence of characters in a string.
Usage
[php]
str_replace(find,replace,str)
[/php]
here
find –> sequence of characters to be replaced
replace –> sequence of characters with which it is to be replaced
str –> the string inwhich modifications must be done.

example
[php]
str_replace(“ad”,”br”,”google ads”);
[/php]
the out put will be
[php]
google brs
[/php]

Categories
functions java

JAVA:Renaming Files

To rename a file in java you first declare the file variable with oldname and next one with newname and
file1.rename2(file2);
[java]
public static void renamedirectory(String oldname,String newname){
// File (or directory) with old name

File file = new File(oldname);

// File (or directory) with new name
File file2 = new File(newname);

// Rename file (or directory)
boolean success =false;
if(file.isDirectory()){
success = file.renameTo(file2);
} else {
System.out.println(“The file is not a directory “+oldname) ;
}
if (!success) {
System.out.println(“File was not successfully renamed”);
} else {
System.out.println(“File Renamed from “+oldname+” to “+newname);
}
}
[/java]
The above function prints out if it doesnot rename succesfully.

Categories
functions php

PHP: Renaming File

To rename a file in php use
rename();

you use the function like

rename(“oldname”, “newname”);

you have to include the path to file if it is present in sub directories where your php script is present.

Categories
functions matlab

Matlab: Inline Functions

There are great utilities in Matlab called inline functions. These are like macros in C.
you can define a functions of two or three variables or a single variable in matlab.

this is the syntax…

f = inline(‘expression’,’arg1,’arg2′)

and you call the function as

f(x,y)

now if you need to define x+y in matlab you need to write as

f = inline(‘x+y’,’x’,’y’);

by the way single quotes are necessary.

you call the function as
f(x,y);

f(1,2) gives results 3..

just paste the following lines in matlab command windo and press enter for testing

f = inline(‘x+y’,’x’,’y’)
f(2,3)

Njoy

Categories
functions ms office

MS Excel: Function to Extract Hypelinks from cells

This is function used to extract hyperlinks or urls from a cell in excel.
Open your excel sheet. Press Alt+F11. Then in the vba editor that appearrs go to menu
insert–>module. Paste the following function there and now press alt+q.

Function getURL(forThisCell As Range) As String
‘VBA UDF for getting URLs from a cell if any
retVal = “”
If forThisCell.Hyperlinks(1).Address <> “” Then
retVal = forThisCell.Hyperlinks(1).Address
End If
getURL = retVal
End Function

Now if you want to extract link from the cell A1 then enter the following in any of the cell
=getURL(A1)

Categories
functions php

PHP: Function ucwords

This function ucwords capitalizes first alphabet of every word in a String and returns it. This is useful for converting or renaming files or directories..

usage:
$string = “name sake”;
$stringresult = ucwords($string);

the result will be
Name Sake

Categories
functions php

PHP: Function Explode

This function is used to break or split a string in php into a number of substrings based on the criteria you give. Like if you want to break a line into substrings with ‘:’ as delimiters

echo $pieces[$num].”
“;
}

?>

You will have the output as an array of strings…

anna
johnny
maria

It is really useful function when dealing with html codes or fetching files from other sites

Categories
functions java

JAVA: Deleting File

This function is to delete files in java. It first checks whether it exists or not next check for write access rights or permissions and then check if the directory is empty if the given file is a directory.


public static void delfile(String fname){
File f = new File(fname);
//Check whether file exists or not
if (!f.exists()){
System.out.print("file doesnot exists");
} else if (!f.canWrite()){
System.out.print("write prtected");
} else if (f.isDirectory()) {
String[] files = f.list();
if (files.length > 0){
System.out.print("this is a directory");
}
}
boolean success = f.delete();
}
Categories
functions java

JAVA:Splitting a String

In java you can split a string into two strings based on the delimeter using function split().
Example Given below

String s1 = “You are a dog”;
String[] pieces = s1.split(” “);

here we are splitting the string with ” ” (single space) as delimiter. The result will be a array of strings.. like

pieces[0] = You
pieces[1] = are
pieces[2] = a
pieces[3] = dog