Extract More than 3 Words After the First Word - blackberry

In the app I'm working on, I need to extract the first word from a String and put it into another String and the rest of the words in yet another String. I was able to extract the first word using:
String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner"
int spacePos3 = pString.indexOf(" ");
String pFirstWord = pString.substring(0,spacePos3);
Result : KOF0000094
Now I want the "Implementation:ListingRequest:User FO-Partner" to put
in another String.
Thanks for your help in advance

Simplest solution with what you already have.
String restOfString = pString.substring(spacePos3+1)

String pSecondWord = pString.substring(spacePos3 + 1);

String whole = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
String firstWord = "";
String restOfWords = "";
int spacesIndex = whole.indexOf(" ", 0);
restOfWords = whole.substring(spacesIndex, whole.length());
restOfWords = restOfWords.trim();
firstWord = whole.substring(0, spacesIndex);
firstWord = firstWord.trim();

This is simple string parsing... just find the first index of the first space... i.e. in a for loop...
if(string.charAt(i) == Characters.SPACE)
indexOfSpace = i;
Then your first word will be
String part1 = string.substring(0,indexOfSpace);
and the second string will be
String part2 = string.substring(indexOfSpace + 1);

Try using another call to substring(). What is the index of the first character in the string you want? What is the index of the last character?

You already have the index of the first space, which marks the end of the first word, so all you need to do is take the substring from the index immediately after that (so you don't include the space itself) to the end of the string.

You're probably better of using the split function
It would look something like this:
String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
String[] parts = pString.split(" ");
String partone = parts[0];
String partwo = parts[1] + " " +parts[2];
Or something similar, if there are going to be more spaces in the part following the first word you could use a loop or something similar;

You could use split, for instance...
static String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
static String[] pFirstWord = pString.split(" ");
/**
* #param args
*/
public static void main(String[] args) {
for(String word : pFirstWord) {
System.out.println(word);
}
}
This returned...
KOF0000094
Implementation:ListingRequest:User
FO-Partner
So the last string would be pFirstWord[1] + pFirstWord[2]

String class has a split method: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
Use that if you want to get all the words.
EDIT: as mentioned in the comments, String.split is not supported in BB Java.
Or this if you just want the first word and the rest of the string:
int index=yourstring.indexOf(" ");
String firstWord = yourstring.substring(0,index);
String rest = yourstring.substring(index+1);

Related

How to remove a character from string and concatinate it in Dart | Flutter

I have two variables
String firstInput = "1.1.5";
String secondInput = "1.1.6";
From this I want the output firstOutput = 115 secondOutput = 116
How to remove dots from the string and concatenate remains as one variable ?
You can use the replaceAll method.
It would look like String out = firstInput.replaceAll(".","");
You can also use replaceAllwith RE as shown below
void main(){
final myString = '1.3.4.6.6';
String withoutDots = myString.replaceAll(RegExp('\\.'), ''); "Here \\ is used to as esc char"
print(withoutDots); // prints 13466
}

Dart syntax of ".."

I have a question in Dart, as follows:
void main() {
String originUrl = "http://www.123.com?name={0}&age={1}";
String newUrl = originUrl.replaceFirst('{0}', 'Jack')
..replaceFirst('{1}', '20');
print(newUrl);
}
output:
http://www.123.com?name=Jack&age={1}
Why not below:
http://www.123.com?name=Jack&age=20
In your code you don't need to use the cascade operator, using that you won't get the String returned by replaceFirst('{1}', '20').
it should look like this:
// Replace {0} with Jack, and return a new String
String newUrl = originUrl.replaceFirst('{0}', 'Jack')
.replaceFirst('{1}', '20'); //Replace {1} with 20 in the last string and return a new String.

How can I insert a space character in every upper case letter expect the first one at each element of string array in DXL script?

I would like to edit the elements of string array with DXL script which is used in for loop. The problem will be described in the following:
I would like to insert space in front of every upper case letter expect the first one and it would be applied for all lines in string array.
Example:
There is a string array:
AbcDefGhi
GhiDefAbc
DefGhiAbc
etc.
and finally I would like to see the result as:
Abc Def Ghi
Ghi Def Abc
Def Ghi Abc
etc.
Thanks in advance!
Derived straightly from the DXL manual..
Regexp upperChar = regexp2 "[A-Z]"
string s = "yoHelloUrban"
string sNew = ""
while (upperChar s) {
sNew = sNew s[ 0 : (start 0) - 1] " " s [match 0]
s = s[end 0 + 1:]
}
sNew = sNew s
print sNew
You might have to tweak around the fact that you do not want EVERY capital letter to be replaced with , only those that are not at the beginning of your string.
Here's a solution written as a function that you can just drop into your code. It processes an input string character by character. Always outputs the first character as-is, then inserts a space before any subsequent upper-case character.
For efficiency, if processing a large number of strings, or very large strings (or both!), the function could be modified to append to a buffer instead of a string, before finally returning a string.
string spaceOut(string sInput)
{
const int intA = 65 // DECIMAL 65 = ASCII 'A'
const int intZ = 90 // DECIMAL 90 = ASCII 'Z'
int intStrLength = length(sInput)
int iCharCounter = 0
string sReturn = ""
sReturn = sReturn sInput[0] ""
for (iCharCounter = 1; iCharCounter < intStrLength; iCharCounter++)
{
if ((intOf(sInput[iCharCounter]) >= intA)&&(intOf(sInput[iCharCounter]) <= intZ))
{
sReturn = sReturn " " sInput[iCharCounter] ""
}
else
{
sReturn = sReturn sInput[iCharCounter] ""
}
}
return(sReturn)
}
print(spaceOut("AbcDefGHi"))

How to use client.read() to read string between two characters?

In my ESP8266 WiFi project I'm getting characters from a website through a GET request. The current code is this:
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
To get a string between particular characters, how do I edit this?
Put the code snip after read string operation and change the below divider delimeters with yours and gatheredStr will be your desired string:
char firstDivider = 'X';
char secondDivider = 'Y';
int firstDividerIndex = line.indexOf(firstDivider);
int secondDividerIndex = line.indexOf(secondDivider);
String gatheredStr = line.substring(firstDividerIndex, secondDividerIndex);

How to Tokenize String with Commas and Line Delimiter

I'm making a simple String Tokenizer in Swift like I would in Java...but it's really not working out for me.
The end of each line in my data source delimited with "^" and the data is separated by comma's.
For example: "string 1,string 2,string 3,^,string 1,string 2,string 3,^"
This is what I would do in Java...(I only want the first two strings in each line of data)
String delimeter = "^";
StringTokenizer tokenizedString = new StringTokenizer(responseString,delimeter);
String [] stringArray = new String [tokenizedString.countTokens()];
StringTokenizer tokenizedAgain;
String str1;
String str2;
String token;
for(int i =0; i< stringArray.length; i ++)
{
token = tokenizedString.nextToken();
tokenizedAgain = new StringTokenizer(token, ",");
tokenizedAgain.nextToken();
str1 = tokenizedAgain.nextToken();
str2 = tokenizedAgain.nextToken();
}
If someone could point me in the right direction that would really helpful.
I've looked at this: Swift: Split a String into an array
and this: http://www.swift-studies.com/blog/2014/6/23/a-swift-tokenizer
but I can't really find other resources on String Tokenizing in Swift. Thanks!
This extends Syed's componentsSeperatedByString answer but with Swift's map to create the requested Nx2 matrix.
let tokenizedString = "string 1, string 2, string 3, ^, string a, string b, string c, ^"
let lines = tokenizedString.componentsSeparatedByString("^, ")
let tokens = lines.map {
(var line) -> [String] in
let token = line.componentsSeparatedByString(", ")
return [token[0], token[1]]
}
println(tokens)
var delimiter = "^"
var tokenDelimiter = ","
var newstr = "string 1, string 2, string 3, ^, string 1, string 2, string 3,^"
var line = newstr.componentsSeparatedByString(delimiter) // splits into lines
let nl = line.count
var tokens = [[String]]() // declares a 2d string array
for i in 0 ..< nl {
let x = line[i].componentsSeparatedByString(tokenDelimiter) // splits into tokens
tokens.append(x)
}
println(tokens[0][0])

Resources