how to extract first substring i a string containg /? [duplicate] - java

This question already has answers here:
Java: Getting a substring from a string starting after a particular character
(10 answers)
Closed 8 years ago.
I would like to extract ccadmin from /ccadmin/hrp/filelist ?
i know that i can get last substring using
String uri = "/ccadmin/hrp/filelist";
String commandKey = uri.substring(uri.lastIndexOf("/") + 1, uri.length());
but how to extract first Substring ccadmin?

If I understand your question, then you could use something like,
String uri = "/ccadmin/hrp/filelist";
String first = uri.substring(1, uri.indexOf("/", 2));
System.out.println(first);
Output is
ccadmin

Split the string by the / character:
String[] parts = uri.split("/");
parts[1] is what you want.

you can use this to split your string and then get the part of uri you need:
String uri = "/s/d/f";
String[] parts = uri.split("/");
String item = parts[1];
System.out.println( item );

Related

How to split a String into two arrays based on a delimiter in Java? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I have a String as below
String combinedString = "10.15%34.23%67.8% 89.00%100.00%44.44%";
As you can see above, there is a space between 67.8% and 89.00%
I would want to split them into two String arrays or two strings as below
String[] firstStringArray = {10.15%,34.23%,67.8%};
String[] secondStringArray = {89.00%, 100.00%, 44.44%};
or
String firstString = "10.15%34.23%67.8%";
String secondString = "89.00%100.00%44.44%";
Any idea on this please?
You could simply use String.split(" ") as follows:
String combinedString = "10.15%34.23%67.8% 89.00%100.00%44.44%";
String firstString = combinedString.split(" ")[0];
String secondString = combinedString.split(" ")[1];
System.out.println(firstString);
System.out.println(secondString);
Output would look like this:
10.15%34.23%67.8%
89.00%100.00%44.44%
You can use the white-space regex to split string based on delimiter of space, snippet as below,
String str = "10.15%34.23%67.8% 89.00%100.00%44.44%";
String[] splited = str.split("\\s+");

How to remove last character in String? [duplicate]

This question already has answers here:
Java: Simplest way to get last word in a string
(7 answers)
Closed 2 years ago.
how to remove last word in a string
word should be dynamic
for example:-String [] a={"100-muni-abc"};
i want output like this 100-muni
remove last one-abe
Try:
String a = "100-muni-abc";
String res = a.substring(0, a.lastIndexOf("-"));
Starting from
String str = "100-muni-abc";
Removing the last char
str = str.substring(0,str.length() - 1)
Removing the last 3 chars
str = str.substring(0,str.length() - 3)
A bit late huh?
Try this
String str = "100-muni-abc";
String [] parts = str.split("-");
System.out.println(parts[0]+"-"+parts[1]);
Try this
String text = What is the name of your first love?;
String lastWord = text.substring(text.lastIndexOf(" ")+1);
System.out.println(lastWord);
Output Answer : love?

JAVA string split with regex ("||") cannot be used? [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

How to split a string or get the right text from string [duplicate]

This question already has answers here:
Java: Simplest way to get last word in a string
(7 answers)
Closed 8 years ago.
I need some help to split up a string. Right now, my string contains this:
"RM 8 Text"
Now I only want Text to be printed in my string builder using append, how do I get rid of the RM 8 in the start of the string?
Right now I have done it this way, but there has to be an easier way.
String[] lines = fromServer.split("\\s+");
String line2 = lines[2];
logbuilder.append(line2);
Assuming that is standard format
String lastWord = fromServer.substring(fromServer.lastIndexOf(" ")+1);
You could simply replace the RM 8.
String content = fromServer.replaceAll("RM 8", "");
logBuilder.append(content);
try this
s = s.replaceAll(".*\\s+(\\S+)$", "$1")
Try the following code sample,
String abc = "RM 8 Text" ;
int a = abc.lastIndexOf(" ");
String xyz = abc.substring(a,abc.length());
System.out.println(xyz);

how to check if string contains '+' character [duplicate]

This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 6 years ago.
I want to check if my string contains a + character.I tried following code
s= "ddjdjdj+kfkfkf";
if(s.contains ("\\+"){
String parts[] = s.split("\\+);
s= parts[0]; // i want to strip part after +
}
but it doesnot give expected result.Any idea?
You need this instead:
if(s.contains("+"))
contains() method of String class does not take regular expression as a parameter, it takes normal text.
EDIT:
String s = "ddjdjdj+kfkfkf";
if(s.contains("+"))
{
String parts[] = s.split("\\+");
System.out.print(parts[0]);
}
OUTPUT:
ddjdjdj
Why not just:
int plusIndex = s.indexOf("+");
if (plusIndex != -1) {
String before = s.substring(0, plusIndex);
// Use before
}
It's not really clear why your original version didn't work, but then you didn't say what actually happened. If you want to split not using regular expressions, I'd personally use Guava:
Iterable<String> bits = Splitter.on('+').split(s);
String firstPart = Iterables.getFirst(bits, "");
If you're going to use split (either the built-in version or Guava) you don't need to check whether it contains + first - if it doesn't there'll only be one result anyway. Obviously there's a question of efficiency, but it's simpler code:
// Calling split unconditionally
String[] parts = s.split("\\+");
s = parts[0];
Note that writing String[] parts is preferred over String parts[] - it's much more idiomatic Java code.
[+]is simpler
String s = "ddjdjdj+kfkfkf";
if(s.contains ("+"))
{
String parts[] = s.split("[+]");
s = parts[0]; // i want to strip part after +
}
System.out.println(s);

Categories