0

I am a new in Android development and I want to write a function which I have written for IOS in swift, so if anybody can help me with this then it will be great :

I want to convert a String from

String1 = "Guardians Of the Galaxy(G.T.G)"

into

String2 = "GTG"

All I want to is to get the value which is inside the brackets and remove the . from it and also if there's any space within them

here's how I do it in Swift maybe it'll help to understand

if the spaceLessFullForm = "Catch Me if You Can"

let SpaceLessFullForm = Fullform.condensedWhitespace
let delimiters: NSCharacterSet = NSCharacterSet(charactersInString: "()")
var splitString: [AnyObject] = SpaceLessFullForm.componentsSeparatedByCharactersInSet(delimiters)
let substring: String = splitString[1] as! String
print(substring)
let dotLessString = substring.stringByReplacingOccurrencesOfString(".", withString: "")
let upperCaseFirstCharcters = dotLessString.stringByReplacingOccurrencesOfString(" ", withString: "")

then my upperCaseFirstCharcters = "CMIUC"

2
  • what is problem tho? Commented May 25, 2016 at 6:53
  • want to extract the text which is inside the bracket of any string @vrundpurohit Commented May 25, 2016 at 6:54

6 Answers 6

2

You can split the string into parts.

String string = "Guardians Of the Galaxy(G.T.G)";
String[] parts = string.split("(");

Then just remove the characters that you dont need.

String neededString = parts[1].replace(".", "").replace(")", "");

This is sample, which might helps you to get the answer. Not tested

Sign up to request clarification or add additional context in comments.

Comments

1

Try this way..

String kek = "Guardians Of the Galaxy(G.T .G . V)";
int fInd = kek.indexOf("(") + 1;
int lInd = kek.indexOf(")");
String kek1 = kek.substring(fInd, lInd);
String kek2 = kek1.replace(".", "").replace(" ", "");
Toast.makeText(getApplicationContext(), kek2, Toast.LENGTH_SHORT)
            .show();

Result will be : GTGV

Comments

1

Following code will help you to extract the text in java:

    String str1 = "Guardians Of the Galaxy(G.T.G)";
    String str2 = "";
    boolean flag = false;
    for (int i = 0; i < str1.length(); i++) {
        if(str1.charAt(i) == '(') {
            flag = true;
            continue;
        } else if(str1.charAt(i) == ')') {
            break;
        }
        if(flag) {
            if(str1.charAt(i) != '.' && str1.charAt(i) != ' ') {
                str2 += str1.charAt(i);
            }
        }
    }
    Log.i("str2", str2);

Comments

1

Based on your usecase the simple solution would be to find the indexes of the brackets, take a substring and then replace the dots with nothing. If one of the brackets has an index of -1 you can find the position of each space an take the char after the space. Please note this code will compile but is a minimalistic example. It will fail if you have for instance 2 consecutive spaces, a space at the end of the text, or if you're missing one of the brackets.

public class Parser
{
    public static void main(String[] args) {
        String test = "Guardians Of the Galaxy (G.T.G)";
        String test2 = "Catch Me if You Can";
        String thisWillFail = "This will fail ";

        System.out.println(parseItem(test));
        System.out.println(parseItem(test2));
    }

    public static String parseItem(String item)
    {
        String result = "";
        int indexStart = item.indexOf("(") + 1;
        int indexEnd = item.indexOf(")");

        if(indexStart != -1 && indexEnd != -1) 
        {       
            String subString = item.substring(indexStart, indexEnd);
            result = subString.replace(".", "");
        }
        else
        {
            result += item.charAt(0);
            while(item.contains(" "))
            {
                int nextSpace = item.indexOf(" ");
                result += item.charAt(nextSpace + 1);
                item = item.substring(nextSpace + 1);
            }
        }
        return result.toUpperCase();
    }
}

Comments

1

Use this ::

   String str1 = "Guardians Of the Galaxy(G.T.G)";
        String str2 = str1.substring(str1.indexOf("(")+1,str1.indexOf(")")).replace(".","");

Comments

1

Try this:

String s = "Guardians Of the Galaxy(G.T.G)";
System.out.print(s.substring(s.indexOf('(')+1, s.indexOf(')')).replaceAll("\\.", "").replaceAll(" ", ""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.