Extract string between slash - java

From the below URL, I am trying to extract 2 Strings i.e String a = "region/country"; and String b = "123xyz"; I tried using overloaded method of IndexOf but that too didnt help. Any pointers?
String URL = "http://abcd.com/region/country/state/123xyzCONSTANTTEXT.html";

You can use the Pattern class for this. If you want to match only the next two words after that url (that doesnt change) then you can use this. \w is a shorthand for [a-zA-Z0-9_]. If you are ok with _ then use \w or else use [a-zA-Z0-9].
The area inside the parenthesis is a matching group, if you want to separate out region and country simply put parenthesis around each of the \w individually and matcher.group(1) will contain region and matcher.group(2) will contain country
String URL = "http://abcd.com/region/country/state/123xyzCONSTANTTEXT.html";`
Pattern pattern = Pattern.compile("http://abcd.com/(\\w+/\\w+)");
Matcher matcher = pattern.matcher(URL);
if (matcher.find()) {
System.out.println(matcher.group(1)); //prints region/country
} else {
System.out.println("Match not found");
}

You could use the split() method. It takes one argument in the form of a regex string and outputs an array of strings, split around the regex.
String[] stringArray = URL.split("/");
Will probably output an array like:
String[] stringArray = {"http:","","abcd.com","region","country","state","123xyzCONSTANTTEXT.html"};
And then use split again on an array index:
String[] stringArray2 = stringArray[7].split("CONSTANTTEXT.html");
To retrieve 123xyz, which will be stored in stringArray2[0]

For example
public class Example {
public static void main(String[] args) {
String[] string = "http://abcd.com/region/country/state/123xyzCONSTANTTEXT.html".split("/");
String string1 = "";
String string2 = "";
for(int i =0; i < string.length ; i++) {
if(i == 3) {
string1 += string[i] + "/";
}
if(i == 4) {
string1 += string[i];
}
if(i == 6) {
string2 = string[i].substring(0, Math.min(string[i].length(), 6));
}
}
System.out.println(string1);
System.out.println(string2);
}
}
output:
region/country
123xyz

Related

Java - Make character after space uppercase?

I'm trying to have the letter after every space turn uppercase. Can someone tell me what's wrong with the following method? Given phrase "this is a test" it returns "ThIs Is A TesT" instead of "this Is A Test"
public String toTitleCase(String phrase) {
for (int i=0; i<phrase.length(); i++) {
if(phrase.substring(i,i+1).equals(" ")) {
phrase = phrase.replace(phrase.substring(i+1,i+2),phrase.substring(i+1,i+2).toUpperCase());
}
}
return phrase;
}
The problem in your code is that String.replace replaces each target character present in the String, and not only the one you want.
You could work directly on an array of chars instead of on the String:
public static String toTitleCase(String phrase) {
// convert the string to an array
char[] phraseChars = phrase.toCharArray();
for (int i = 0; i < phraseChars.length - 1; i++) {
if(phraseChars[i] == ' ') {
phraseChars[i+1] = Character.toUpperCase(phraseChars[i+1]);
}
}
// convert the array to string
return String.valueOf(phraseChars);
}
It's replacing all t, try below code.
It will help you.
String phrase="this is a test";
for (int i=0; i<phrase.length(); i++) {
if(phrase.substring(i,i+1).equals(" ")) {
System.out.println(phrase.substring(i+1,i+2));
phrase = phrase.replace(phrase.substring(i,i+2),phrase.substring(i,i+2).toUpperCase());
}
}
System.out.println(phrase);
Use streams (or split) to split your string into parts, don't do it manually using substring.
Try below code
String test = "this is a test";
UnaryOperator<String> capitalize = str ->
str.substring(0,1).toUpperCase() + str.substring(1).toLowerCase();
String result =
Stream.of(
test.split(" ")
).map(capitalize)
.collect(
Collectors.joining(" ")
);
System.out.println(result);
Output: This Is A Test
When you replace a substring it will replace the each occurrence of that substring - which is not necessarily the one you are trying to replace. This is why it is replacing letters inside words.
Switching to a StringBuilder here to poke individual characters. Note that we don't traverse the entire String because there is no next-character to capitalize at the last character.
public String toTitleCase(String phrase) {
StringBuilder sb = new StringBuilder(phrase);
for (int index = 0 ; index < phrase.length - 1 ; ++index) {
if (sb.charAt(index) == ' ') {
sb.setCharAt(index + 1, Character.toUppercase(sb.charAt(index + 1)));
}
}
return sb.toString();
}
If a letter is first in any word, it will be replaced everywhere. In your case, all t,i and a will be uppercase.
Taking example for is. It is find a space before. Than in if body, what actually happen:
phrase = phrase.replace("i","I");
And all i are replaced with I.
String class cannot replace at a specific position.
You have to options:
using StringBuilder which can replace at a specific position.
String toTitleCase(String phrase) {
StringBuilder sb= new StringBuilder(phrase);
for (int i=0; i<phrase.length(); i++) {
if(i==0 || phrase.charAt(i-1)==' ') {
sb.replace(i,i+1,phrase.substring(i,i+1).toUpperCase());
}
}
return sb.toString();
}
or with stream, which is the method I prefer because is one-line. This way you don't preserve white-spaces( multiple consecutive white-spaces will be replaced with only one space), but usually you want this.
Arrays.asList(phrase.split("\\s+")).stream().map(x->x.substring(0,1).toUpperCase()+x.substring(1)).collect(Collectors.joining(" "));

How to get String between last two underscore

I have a string "abcde-abc-db-tada_x12.12_999ZZZ_121121.333"
The result I want should be 999ZZZ
I have tried using:
private static String getValue(String myString) {
Pattern p = Pattern.compile("_(\\d+)_1");
Matcher m = p.matcher(myString);
if (m.matches()) {
System.out.println(m.group(1)); // Should print 999ZZZ
}
else {
System.out.println("not found");
}
}
If you want to continue with a regex based approach, then use the following pattern:
.*_([^_]+)_.*
This will greedily consume up to and including the second to last underscrore. Then it will consume and capture 9999ZZZ.
Code sample:
String name = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
Pattern p = Pattern.compile(".*_([^_]+)_.*");
Matcher m = p.matcher(name);
if (m.matches()) {
System.out.println(m.group(1)); // Should print 999ZZZ
} else {
System.out.println("not found");
}
Demo
Using String.split?
String given = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String [] splitted = given.split("_");
String result = splitted[splitted.length-2];
System.out.println(result);
Apart from split you can use substring as well:
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String ss = (s.substring(0,s.lastIndexOf("_"))).substring((s.substring(0,s.lastIndexOf("_"))).lastIndexOf("_")+1);
System.out.println(ss);
OR,
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String arr[] = s.split("_");
System.out.println(arr[arr.length-2]);
The get text between the last two underscore characters, you first need to find the index of the last two underscore characters, which is very easy using lastIndexOf:
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String r = null;
int idx1 = s.lastIndexOf('_');
if (idx1 != -1) {
int idx2 = s.lastIndexOf('_', idx1 - 1);
if (idx2 != -1)
r = s.substring(idx2 + 1, idx1);
}
System.out.println(r); // prints: 999ZZZ
This is faster than any solution using regex, including use of split.
As I misunderstood the logic from the code in question a bit with the first read and in the meantime there appeared some great answers with the use of regular expressions, this is my try with the use of some methods contained in String class (it introduces some variables just to make it more clear to read, it could be written in the shorter way of course) :
String s = "abcde-abc-db-ta__dax12.12_999ZZZ_121121.333";
int indexOfLastUnderscore = s.lastIndexOf("_");
int indexOfOneBeforeLastUnderscore = s.lastIndexOf("_", indexOfLastUnderscore - 1);
if(indexOfLastUnderscore != -1 && indexOfOneBeforeLastUnderscore != -1) {
String sub = s.substring(indexOfOneBeforeLastUnderscore + 1, indexOfLastUnderscore);
System.out.println(sub);
}

How to take a String of LastName, FirstName and return FirstInitial.LastName

Expected Input: Doe, John
Expected Output: J. Doe
public static void main(String[] args) {
String z = "Doe, John";
System.out.println(z);
String y = formatName(name);
System.out.println(y);
}
public static String formatName(String name) {
String str[] = name.split(",");
StringBuilder sb = new StringBuilder();
sb.append(str[1].charAt(0));
sb.append(". ");
sb.append(str[0]);
return sb.toString();
}
My output is not as expected.
Match (Optional) White Space with String.split Regular Expression
You have a space after the comma in your input, you could modify your regular expression in split from
String str[] = name.split(",");
to
String str[] = name.split(",\\s*");
to match and remove optional white-space. After I made the above change I ran your code, and got the (expected) output
Doe, John
J. Doe
Trim the Leading White Space
Alternatively, you could trim str[1] before getting the first character like
sb.append(str[1].trim().charAt(0)); //<-- will also remove leading space
Regular Expression With a Compiled Pattern
Another possible option is compiling a regex Pattern and using a Matcher like
// Match (and group) one more characters followed by a "," and
// optional whitespace. Then match (and group) one character followed
// any number of optional characters.
private static Pattern pattern = Pattern.compile("(.+),\\s*(.).*");
public static String formatName(String name) {
Matcher m = pattern.matcher(name);
if (m.matches()) {
return String.format("%s. %s", m.group(2), m.group(1));
}
return name;
}
Another simple way to get FirstInitial.LastName
Other than using split, you can use substring and based on the position of the comma ,, manipulate the name to get the output:
String s = "Doe, John";
s = s.replace(" ", ""); //remove spaces
int i = s.indexOf(","); //get pos of comma
String name = s.charAt(i+1) + ". " + s.substring(0, i); //create name
Output:
J. Doe
sb.append(str[1].charAt(0)); , index for charAt() should be 1 not 0 .
String str[] = name.split(","); will return [Doe, John], notice the space before second element.
better yet use split(", ")
I tried this based on what i understood. Use for loop and trim the items
public static String formatName(String name) {
String str[] = name.split(",");
for(int i = 0 ; i < str.length ; i++){
str[i] = str[i].trim();
//System.out.println("+"+str[i]+"+");
}
StringBuilder sb = new StringBuilder();
sb.append(str[1].charAt(0));
sb.append(".");
sb.append(str[0]);
sb.append(".");
return sb.toString().trim();
}

Splitting a string in two parts if p

I am attempting to split a word from its punctuation:
So for example if the word is "Hello?". I want to store "Hello" in one variable and the "?" in another variable.
Here is my code so far:
String inWord = "hello?";
if (inWord.contains(","+"?"+"."+"!"+";")) {
String parts[] = inWord.split("\\," + "\\?" + "\\." + "\\!" + "\\;");
String word = parts[0];
String punctuation = parts[1];
} else {
String word = inWord;
}
System.out.println(word);
System.out.println(punctuation);
My problem is that I am getting error: cannot find symbol when I try and print out the word and the punctuation.
Thanks for help in advance
There are other things wrong with your code but your question was why you get the 'cannot find symbol' error.
String inWord = "hello?";
String word;
String punctuation = null;
if (inWord.contains(","+"?"+"."+"!"+";")) {
String parts[] = inWord.split("\\," + "\\?" + "\\." + "\\!" + "\\;");
word = parts[0];
punctuation = parts[1];
} else {
word = inWord;
}
System.out.println(word);
System.out.println(punctuation);
The scope of a variable declaration like String word = ... is only the block (the pieces of code inside '{' and '}') that it's in. The variables word and punctuation don't exist in the scope in which you try to print them.
You need to declare your variables word and punctuation in the same scope (or an enclosing scope) of where you access them in your System.out.println
You made the following errors in your code.
1.Declare the string outside the if condition
2.inWord.contains(","+"?"+"."+"!"+";") this is equal to inword.contains(",?.!;") , so the condition will fail always and it goes to else condition
split() will not store the value based on which you split the string
eg
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
In this the value "-" can't be stored.Hope you understand what i'm trying to convey.
You could try with your custom contains function and StringTokenizer
as:
public class Test{
public static void main(String[] args) {
String inWord = "hello";
String[] wordAndPunctuation = null;
char[] punctuations =new char[]{',','?','.','!',';'};
StringTokenizer tokenizer = new StringTokenizer(inWord,new String(punctuations),true);
int i = 0;
if (Test.contains(inWord,punctuations)) {
while(tokenizer.hasMoreTokens()){
wordAndPunctuation = new String[tokenizer.countTokens()];
System.out.println(tokenizer.countTokens());
wordAndPunctuation[i] = tokenizer.nextToken();
i++;
}
}else{
System.out.println("No punctuation in "+inWord);
}
}
public static boolean contains(String str, char[] charArr){
System.out.println(Arrays.toString(charArr));
for(char c:charArr){
if(str.contains(String.valueOf(c)))
return true;
}
return false;
}
}
I would recommend parsing through the String and checking if the character is a punctuation method:
String sentence = "Hello? Is this Mrs. Doubtfire?"; // Example.
ArrayList<String> chunks = new ArrayList<>(); // Will store the "non-punctuated chunks"
ArrayList<Character> puncts = new ArrayList<>();// Will the punctuations in the "sentence"
char[] punctuations = {',','?','.','!',';'}; // Store punctuations here.
int lastIndex = 0;
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
for (char punctuation : punctuations) {
if (c == punctuation) {
chunks.add(sentence.substring(lastIndex, i).trim());
puncts.add(c);
lastIndex = i + 1;
}
}
}
System.out.println(chunks);
System.out.println(puncts);
Output:
[Hello, Is this Mrs, Doubtfire]
[?, ., ?]
And remember to import java.util.ArrayList!
Why don't you do this:
String s = "hello!";
Pattern p = Pattern.compile("(\\w+)?(\\W)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println("Word: " + m.group(1) + " | Punctuation: " + m.group(2));
}
Group1 will contain the word and Group2 will contain the punctuation.
Demo : http://ideone.com/ljIZFW

Making first letter capital using regex like in ucwords

I need to convert a String value in to Upper case (First letter to upper in every word).
This can be done in php by using ucwords() method.
Ex :
String myString = “HI GUYS”;
myString = myString. toLowerCase().replaceAll(“Regex”, “Some Charactor”)
Thanks with hi5
Using regex, it will be difficult. Try following simple code:
String str="hello world";
String[] words=str.split(" ");
for (String word : words) {
char upCase=Character.toUpperCase(word.charAt(0));
System.out.print(new StringBuilder(word.substring(1)).insert(0, upCase));
}
Output:
Hello World
Undermentioned will work great in all your situation
If you need to get first letter of all words capital ..
-----------------------------------------------------
public String toTheUpperCase(String givenString) {
String[] arr = givenString.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(Character.toUpperCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
return sb.toString().trim();
}
When you need first letter of first word to be capitalized
-------------------------------------------------------------
public String toTheUpperCaseSingle(String givenString) {
String example = givenString;
example = example.substring(0, 1).toUpperCase()
+ example.substring(1, example.length());
System.out.println(example);
return example;
}
How to use :: Try defining this code n your super class ( Best code practice )
Now when u need to use this method .. just pass String which you need to transform .
For Ex:: Let us assume our super class as CommanUtilityClass.java ...
Now you need this method in some activity say " MainActivity.java "
Now create object of super class as :: [ CommanUtilityClass cuc; ]
Final task -- use this method as described below:
your_text_view.setText(cuc.toTheUpperCase(user_name)); // for all words
your_text_view.setText(cuc.toTheUpperCaseSingle(user_name)); // for only first word ...
Let me know if you need more details for that ..
Enjoy
Cheers !
System.out.println(ucWord("the codes are better than words !!"));// in main method
private static String ucWord(String word) {
word = word.toLowerCase();
char[] c = word.toCharArray();
c[0] = Character.toUpperCase(c[0]);
int len = c.length;
for (int i = 1; i < len; i++) {
if (c[i] == ' ') {
i++;
c[i] = Character.toUpperCase(c[i]);
}
}
return String.valueOf(c);
}
You can use WordUtils from apache for same purpose,
WordUtils.capitalizeFully(Input String);
Here are simplified versions of the toUpperCase methods.
Change all first letters in the sentence to upper case.
public static String ucwords(String sentence) {
StringBuffer sb = new StringBuffer();
for (CharSequence word: sentence.split(" "))
sb.append(Character.toUpperCase(word.charAt(0))).append(word.subSequence(1, word.length())).append(" ");
return sb.toString().trim();
}
Change only the first word to upper case. (nice one-liner)
public static String ucFirstWord(String sentence) {
return String.valueOf(Character.toUpperCase(word.charAt(0))).concat(word.substring(1));
}
String stringToSearch = "this string is needed to be first letter uppercased for each word";
// First letter upper case using regex
Pattern firstLetterPtn = Pattern.compile("(\\b[a-z]{1})+");
Matcher m = firstLetterPtn.matcher(stringToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,m.group().toUpperCase());
}
m.appendTail(sb);
stringToSearch = sb.toString();
sb.setLength(0);
System.out.println(stringToSearch);
output:
This String Is Needed To Be First Letter Uppercased For Each Word

Categories