cancel
Showing results for 
Search instead for 
Did you mean: 

how to remove white sapces in a string ?

Former Member
0 Kudos

Hi,

i have a requirement like this.

i am getting vlaues like below :

R001 Name john

R001 comapny test

R001 position

R001 director reason trainig

now i want the output like this

R001 Name john

company test

position

director reason training

and also i want to delete the white sapces between the string.how to proceed on this?

Regards,

Pavani

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Pavani,

If the first string expression (in this case "R001") is getting repeated at the start of each line and what you want it to just show it at the beginning of first line, then better get the first repeated expression in a seperate string expression and then replace all the expressions equal to the one obtained("R001") earlier to empty String("").

Finally after trimming white spaces and inserting newline whatsoever the case may be finally append the repeated expression to the obtained string at the beginning of it as is done below:

/Get String after trimming white spaces and inserting extra new line after the end of each line to increase spacing between lines/

String str1 = wdContext.currentContextElement().getVa_StringNewline();

/Get the repeated expression in a string variable/

String startExp = str1.subString(0,4);

/Replace all repeated expression with empty string/

str1 = str1.replaceAll(startExp, "");

/Append the repeated expression in the beginning of the first line/

str1 = startExp + str1;

/Set the target expression to the context displaying in a textview or text edit/

wdContext.currentContextElement().setVa_StringNewline(str1);

Hope your problem is address well with this.

Regards,

Tushar Sinha

christiansche
Active Participant
0 Kudos

hi,

If the chacters at the beginning are always the same number, you can use

"R001 comapny test".substring(5)

So you can loop over your values and remove alway the same amount of characters at the beginning of the String.

Best regards,

Christian Schebesta

Former Member
0 Kudos

Hi Pavani,

Let the string with newline characters be set in the context under context variable Va_StringNewline which is either bound to textview or textedit. Use the following code to increase the spacing between lines:

String str1 = wdContext.currentContextElement().getVa_StringNewline();

if(str1.indexOf("\n")!=-1){

str1 = str1.replaceAll("\n", "\n\n");

}

wdContext.currentContextElement().setVa_StringNewline(str1);

Similarly, for trimming down spaces even between words use:

String str1 = wdContext.currentContextElement().getVa_StringNewline();

if(str1.indexOf(" ")!=-1){

str1 = str1.replaceAll(" ", "");

}

wdContext.currentContextElement().setVa_StringNewline(str1);

Regards,

Tushar Sinha

former_member185086
Active Contributor
0 Kudos

Hi

If Input is like

String a = "hello     everyone     out      ";

use

a = a.replaceAll("\\s+", " ");

Also, "
s+" will match single spaces which then get replaced with a single white space, so you could do "
s{2,}" which will match 2 or more white space characters, or "[ ]{2,}" if you don't want to replace new lines.

Best Regards

Satish Kumar

Former Member
0 Kudos

Hi Friend,

String a = "hello everyone out ";

a = a.replaceAll("
s+", " ");

System.out.println(a);

the Output is

hello everyone out

Or You can Do like this if you know the number of space.

String St = "Jeetendra Kumar Choudhary";

System.out.println(St.replace(" ", ""));

St.replaceAll(" ","")

the output is

JeetendraKumarChoudhary.

Regards

Jeetendra.