Filename as a String getParent and getName isn't it - string

How can I get Desktop/1.txt as a String
File f = new File("/home/jigar/Desktop/1.txt");
System.out.println(f.getParent());// /home/jigar/Desktop
System.out.println(f.getName()); //1.txt

Related

Remove \n from a given strings in dart

Given a string
String item = 'Hello\nWorld'
I want a function that removes \n from the given string
Something like this
String newItem = removeBacklash(item);
print(newItem); // newItem will be 'HelloWorld'
You can use .replaceAll()
String removeBacklash(String data) => data.replaceAll("\n", "");
More about String

ToArray and ToString return (unit -> string), not string. What should I do?

Notes
Why a type method that should return a string returns a 'unit -> string' instead and how to solve it? does not answer my question because it refers to incorrect input, while I do not have it.
I'm currently having a problem with the functions "ToString" returning a (unit -> string) type and not a string, and "ToArray" returning (unit -> string[]) and not string[]. Attempting to upcast to string[] or string has no success.
Here is the code:
let readZip filepath = ZipFile.OpenRead filepath
let replaceEnvs str = Environment.ExpandEnvironmentVariables str
let listFiles rawDir =
replaceEnvs rawDir
|> Directory.EnumerateFiles
let readModMetadata filepath =
let archive = readZip filepath
(archive.GetEntry "mcmod.info").ToString // Also becomes (unit -> string) and not string
[<EntryPoint>]
let main args =
let mods = listFiles modsFolder
let modAsArray = mods.ToArray // Becomes (unit -> string[]) and not string[]?
0
Why is this so, and is there a way to get only strings?
So I found it out.
You need to specify that no parameters go into the functions. The parenthesis in ToString() do just that, as they differentiate returning a function from returning a result.
I need more coffee.

Is there like "firstIndexOf" code?

String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"))
System.out.println(remv_last);
output
hello/master
My question is; how can I get this output, thanks for helping.
master/yoda
use indexOf
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(xStr.indexOf("/") + 1);
System.out.println(remv_last);
You can use simple indexOf it will give you the first occurrence only
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"));
int firstIndex = xStr.indexOf("/");
String firstCharacter = xStr.substring(firstIndex,xStr.length());
System.out.println(remv_last);
System.out.println(firstCharacter);
It will print required

Generate string of fixed length in C#

How can I generate a string of fixed length based on the input given in c#.
For example :
string s="1";
string newstring ;
I want the newstring to be "AB_000001";
// Similarly if
s="xyz";
//I want the newstring as
newstring = "AB_000xyz";
String s = "AB_000000";
String newString="xyz";
s = s.Remove(s.Length - newString.Length, newString.Length);
s = s + newString;
string basestr = "AB_000000";
string inp = "xyz";
string res = basestr.Substring(0, basestr.Length - inp.Length) + inp;

How to recursively check for a file's existence and rename if it exist, using groovy?

How do recursively check and rename the file if it already exist by appending some incrementing number?
I wrote the below function but it gives me an exception
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'E:\Projects\repo1\in_conv1.xml' with class 'java.lang.String' to class 'java.io.File'
Code
//newFilePath = E:\Projects\repo1\old\testcustprops.xml
String newFilePath = checkForExistenceAndRename(newFilePath,false)
private String checkForExistenceAndRename(String newFilePath, boolean flag){
File f = new File(newFilePath)
if(!flag){
if(f.exists()){
//renaming file
newFilePath=newFilePath[0..-5]+"_conv${rename_count++}.xml"
f = checkForExistenceAndRename(newFilePath,false)
}
else
f = checkForExistenceAndRename(newFilePath,true)
}
else
return newFilePath
}
You are trying to do:
f = checkForExistenceAndRename(newFilePath,false)
Where f is a File. But your function returns a String
Not sure if it works or not (I haven't tested your function), but you could try:
private String checkForExistenceAndRename(String newFilePath, boolean flag){
File f = new File(newFilePath)
if(!flag){
if(f.exists()){
//renaming file
newFilePath = newFilePath[0..-5]+"_conv${rename_count++}.xml"
newFilePath = checkForExistenceAndRename(newFilePath,false)
}
else
newFilePath = checkForExistenceAndRename(newFilePath,true)
}
return newFilePath
}
Also, there's no need to use recursion...
Why not just do:
private String getUniqueName( String filename ) {
new File( filename ).with { f ->
int count = 1
while( f.exists() ) {
f = new File( "${filename[ 0..-5 ]}_conv${count++}.xml" )
}
f.absolutePath
}
}

Resources