How to get the part of data from string:
csvFile = "c:/Users//PHV/01Surname local.csv"
i need to extract Surname from above string
UPD
what you think about it?
File f = new File(csvFile);
String[] parts = f.getName().split(" ");
String strParts = new String(parts[0]);
String finFileName = strParts.substring(2, strParts.length());
You need a regular expression. Something like:
Pattern p = Pattern.compile("^.*/[0-9]+(a-zA-Z)+ .*");
Matcher m = p.matcher(csvFile);
String surname;
if (m.matches()) {
surname = m.group(1);
} else {
System.out.println("filename seems malformed: " + csvFile);
}
UPDATE: Here is a tutorial about regular expressions but not sure it is the best. I think it must work for you though: http://docs.oracle.com/javase/tutorial/essential/regex/
I'm not sure I understand your question, but I assume you want to extract "Surname". If that's correct, please try this:
String surname = csvFile.substring(csvFile.lastIndexOf("/") + 3, csvFile.lastIndexOf(" "));
Related
If i have string like 08-1_2-4_1517614 and if i need to fetch the value "1517614" out of this for string manipulation in java
Any help will be much appreciated
String test = " 08-1_2-4_1517614";
String [] tokens = test.split("_");
System.out.println(tokens[tokens.length - 1]);
Or with regex:
String test = " 08-1_2-4_1517614";
System.out.println(test.replaceFirst(".+_", ""));
Or:
System.out.println(test.substring(test.lastIndexOf("_") + 1));
Here is a regex replace option:
String input = "08-1_2-4_1517614";
String output = input.replaceAll("^.*_", "");
A more general regex replace option using a capture group:
String output = input.replaceAll(".*(?<!\\d)(\\d+)$", "$1");
Or we could take a substring:
String output = input.substring(input.lastIndexOf("_") + 1);
Is the pattern always the same?
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int- could be your friend.
"08-1_2-4_1517614".substring(9, "08-1_2-4_1517614".length()) e.g.
Best way to do it, in case you want to resuse other parts of the original String.
String num = "08-1_2-4_1517614";
String[] parts = num.split("_");
String value = parts[parts.length - 1];
System.out.println(value);
The most simple is to use regex to find all the numbers after the final underscore.
Pattern patt = Pattern.compile("[^_]+$");
Matcher matcher = patt.matcher("08-1_2-4_1517614");
if(matcher.find()) {
System.out.println(matcher.group());
}else{
System.out.println("No Match Found");
}
Or use
str.substring(str.lastIndexOf("_") + 1, str.length())
I'd go for the one-liner:
System.out.println(myString.substring(9, 16));
Hi, you can see my code below. I have some strings Country, rank and grank in my code, initially they will be null, but if regex is mached, it should change the value. But even if regex is matched it is not changing the value it is always null. If I remove all if statements and append the string it works fine, but if match is not found it is throwing an exception. Please let me know how can I check this in if logic.
System.err.println(content);
Pattern c = Pattern.compile("NAME=\"(.*)\" RANK");
Pattern r = Pattern.compile("\" RANK=\"(.*)\"");
Pattern gr = Pattern.compile("\" TEXT=\"(.*)\" SOURCE");
Matcher co = c.matcher(content);
Matcher ra = r.matcher(content);
Matcher gra = gr.matcher(content);
co.find();
ra.find();
gra.find();
String country = null;
String Rank = null;
String Grank = null;
if (co.matches()) {
country = co.group(1);
}
if (ra.matches()) {
Rank = ra.group(1);
}
if (gra.matches()) {
Grank = gra.group(1);
}
You have to escape a single \ - use double \\ then it should work.
Tried this?
while (co.find()) {
System.out.print("Start index: " + co.start());
System.out.print(" End index: " + co.end() + " ");
System.out.println(co.group());
}
Personally I can't make your program work with / without the if so it's not a problem of logic but just a problem that it doesn't match the string for me
So I changed it to get something working, maybe you can use it :)
String content = "NAME=\"salut\" RANK=\"pouet\" TEXT=\"text\" SOURCE";
System.out.println(content);
System.out.println(content.replaceAll(("NAME=\"(.*)\"\\sRANK=\"(.*)\"\\sTEXT=\"(.*)\" SOURCE"), "$1---$2---$3"));
Output
NAME="salut" RANK="pouet" TEXT="text" SOURCE
salut---pouet---text
I want to extract a perticular image path string from a given string .
The String is http:\localhost:9090\SpringMVC\images\integration-icon.png
Now i want to get only the path after images like
\images\integration-icon.png
i tried this
Pattern pattern = Pattern.compile("SpringMVC");
Matcher matcher = pattern.matcher(str);
System.out.println("Checking");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
how can i get ?
String filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
or (haven't tried and looks somewhat odd)
String filename = filepath.substring(filepath.lastIndexOf("\\", "images\\".length()) + 1);
String string = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
int index = string.indexOf("images\\");
String output = string.substring(index);
String text = "http:\localhost:9090\SpringMVC\images\integration-icon.png"
String subText = text.subString(text.indexOf("\images"), text.length());
System.out.println(subText);
String in = "http:\\localhost:9090\\ZenoBusinessStore\\images\\integration-icon.png";
String op = in.replace("http:\\localhost:9090\\ZenoBusinessStore", "");
System.out.println(op);
ZenoBusinessStore must be the name of your project which is constant.
Now split the string
String s = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
String ary = s.split("ZenoBusinessStore");
Now the 2nd element of the array is your image path.
System.out.println(ary[1]);
Use '\\'. It's because backslash is used in escape sequence like '\n'. With a single \ the compiler have no way to know.
I need to change somethign like this -> Hello, go here http://www.google.com for your ...
grab the link, and change it in a method i made, and replace it back into the string like this
-> Hello, go here http://www.yahoo.com for your...
Here is what i have so far:
if(Text.toLowerCase().contains("http://"))
{
// Do stuff
}
else if(Text.toLowerCase().contains("https://"))
{
// Do stuff
}
All i need to do is change the URL in the String to something different. The Url in the String will not always be http://www.google.com, so i can not just say replace("http://www.google.com","")
Use regex:
String oldUrl = text.replaceAll(".*(https?://)www((\\.\\w+)+).*", "www$2");
text = text.replaceAll("(https?://)www(\\.\\w+)+", "$1" + traslateUrl(oldUrl));
Note: code changed to meet extra requirements in comments below.
you can grab the link from the string using below code. I assumed the string will contain only .com domain
String input = "Hello, go here http://www.google.com";
Pattern pattern = Pattern.compile("http[s]{0,1}://www.[a-z-]*.com");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
}
Have you tried something like:
s= s.replaceFirst("http:.+[ ]", new link);
This will find any word beginning with http up till the first white space and replace it with whatever you want
if you want to keep the link then you can do:
String oldURL;
if (s.contains("http")) {
String[] words = s.split(" ");
for (String word: words) {
if (word.contains("http")) {
oldURL = word;
break;
}
}
//then replace the url or whatever
}
You can try this
private String removeUrl(String commentstr)
{
String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:##%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(commentstr);
int i = 0;
while (m.find()) {
commentstr = commentstr.replaceAll(m.group(i),"").trim();
i++;
}
return commentstr;
}
What kind of method would I use to make this:
http://www.site.net/files/file1.zip
To
file1.zip?
String yourString = "http://www.site.net/files/file1.zip";
int index = yourString.lastIndexOf('/');
String targetString = yourString.substring(index + 1);
System.out.println(targetString);// file1.zip
String str = "http://www.site.net/files/file1.zip";
str = str.substring(str.lastIndexOf("/")+1);
You could use regex to extract the last part:
#Test
public void extractFileNameFromUrl() {
final Matcher matcher = Pattern.compile("[\\w+.]*$").matcher("http://www.site.net/files/file1.zip");
Assert.assertEquals("file1.zip", matcher.find() ? matcher.group(0) : null);
}
It'll return only "file1.zip". Included here as a test as I used it to validate the code.
Use split:
String[] arr = "http://www.site.net/files/file1.zip".split("/");
Then:
String lastPart = arr[arr.length-1];
Update: Another simpler way to get this:
File file = new File("http://www.site.net/files/file1.zip");
System.out.printf("Path: [%s]%n", file.getName()); // file1.zip