Example usage for org.apache.commons.lang3 StringUtils substring

List of usage examples for org.apache.commons.lang3 StringUtils substring

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substring.

Prototype

public static String substring(final String str, int start, int end) 

Source Link

Document

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

The returned substring starts with the character in the start position and ends before the end position.

Usage

From source file:org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsLogItemRequestMessage.java

public DlmsLogItemRequestMessage(final String deviceIdentification, final boolean incoming, final boolean valid,
        final DlmsPushNotification message, final int payloadMessageSerializedSize) {
    this.deviceIdentification = deviceIdentification;
    this.organisationIdentification = null;
    this.incoming = incoming;
    this.valid = valid;
    this.payloadMessageSerializedSize = payloadMessageSerializedSize;

    // Truncate the log-items to max length.
    this.encodedMessage = StringUtils.substring(bytesToCArray(message.toByteArray()), 0, MAX_MESSAGE_LENGTH);
    this.decodedMessage = StringUtils.substring(message.toString(), 0, MAX_MESSAGE_LENGTH);
}

From source file:org.osgp.adapter.protocol.dlms.infra.messaging.DlmsLogItemRequestMessage.java

public DlmsLogItemRequestMessage(final String deviceIdentification, final boolean incoming, final boolean valid,
        final DlmsPushNotification message, final int payloadMessageSerializedSize) {
    this.deviceIdentification = deviceIdentification;
    this.incoming = incoming;
    this.valid = valid;
    this.payloadMessageSerializedSize = payloadMessageSerializedSize;

    // Truncate the log-items to max length.
    this.encodedMessage = StringUtils.substring(bytesToCArray(message.toByteArray()), 0, MAX_MESSAGE_LENGTH);
    this.decodedMessage = StringUtils.substring(message.toString(), 0, MAX_MESSAGE_LENGTH);
}

From source file:org.peercast.pecaport.AddMappingDialog.java

public AddMappingDialog(Context c, int pecaRunningPort, Collection<WanConnection> connected,
        final Inet4Address clientIp, final Listener listener) {

    String port, description;//from   ww  w.  j a v  a2s .  c  o  m
    if (pecaRunningPort > 0) {
        port = pecaRunningPort + "";
        description = PecaPortService.DESCRIPTION;
    } else {
        port = "";
        String modelName = StringUtils.substring(Build.MODEL, 0, 16);
        description = String.format("PecaPort(%s)", modelName);
    }

    View view = LayoutInflater.from(c).inflate(R.layout.add_port_dialog, null);
    vPrivateIp = (TextView) view.findViewById(R.id.vPrivateIp);
    vPrivateIp.setText(clientIp.getHostAddress());

    vConnections = (Spinner) view.findViewById(R.id.vConnections);

    vExternalPort = (EditText) view.findViewById(R.id.vExternalPort);
    vExternalPort.addTextChangedListener(mValidPortWatcher);
    vExternalPort.setText(port);

    vInternalPort = (EditText) view.findViewById(R.id.vInternalPort);
    vInternalPort.setText(vExternalPort.getText());
    vInternalPort.addTextChangedListener(mValidPortWatcher);

    vSomePort = (CheckBox) view.findViewById(R.id.vSamePort);
    vSomePort.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                vInternalPort.setText(vExternalPort.getText());
                vInternalPort.setEnabled(false);
            } else {
                vInternalPort.setEnabled(true);
            }
        }
    });
    vExternalPort.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (vSomePort.isChecked()) {
                vInternalPort.setText(vExternalPort.getText());
            }
        }
    });

    vTcp = (RadioButton) view.findViewById(R.id.vTcp);
    vDescription = (EditText) view.findViewById(R.id.vDescription);

    vDescription.setText(description);

    WanConnectionsAdapter wanAdapter = new WanConnectionsAdapter();

    wanAdapter.setConnections(connected);
    vConnections.setAdapter(wanAdapter);

    mBuilder = new AlertDialog.Builder(c).setTitle(R.string.t_add_port).setView(view)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    PortMapping mapping = new PortMapping();
                    mapping.setExternalPort(toUI2Bytes(vExternalPort));
                    mapping.setInternalPort(toUI2Bytes(vInternalPort));
                    mapping.setDescription(vDescription.getText().toString());
                    mapping.setInternalClient(vPrivateIp.getText().toString());
                    mapping.setEnabled(true);
                    mapping.setProtocol(vTcp.isChecked() ? PortMapping.Protocol.TCP : PortMapping.Protocol.UDP);
                    listener.onOkClick((WanConnection) vConnections.getSelectedItem(), mapping);
                }
            }).setIcon(R.drawable.ic_plus_box).setNegativeButton(android.R.string.cancel, null)
            .setCancelable(false);
}

From source file:org.sakaiproject.tool.gradebook.Category.java

public void setName(final String name) {
    // SAK-20071 - names over 255 chars cause DB insert failure
    this.name = StringUtils.substring(name, 0, 249);
}

From source file:org.silverpeas.core.util.TestMemoryData.java

@Test
public void getSizeConverted() {
    MemoryData memoryData = createDefaultMemoryData();
    Map<MemoryUnit, String> expected = new LinkedHashMap<MemoryUnit, String>();
    expected.put(MemoryUnit.B, getDefaultMemorySize().toString());
    expected.put(MemoryUnit.KB, "2073775844301.7451171875");
    expected.put(MemoryUnit.MB, "2025171722.9509229660034");
    expected.put(MemoryUnit.GB, "1977706.7606942607089877");
    expected.put(MemoryUnit.TB, "1931.3542584904889736208");
    for (Map.Entry<MemoryUnit, String> entry : expected.entrySet()) {
        assertThat(entry.getKey().name(),
                StringUtils.substring(memoryData.getSizeConverted(entry.getKey()).toString(), 0, 24),
                is(entry.getValue()));/* w w  w  . j  a va 2 s.c om*/
    }
}

From source file:org.silverpeas.core.util.TestTimeData.java

@Test
public void getTimeConverted() {
    TimeData timeData = createDefaultTimeData();
    Map<TimeUnit, String> expected = new LinkedHashMap<TimeUnit, String>();
    expected.put(TimeUnit.MILLI, getDefaultTime().toString());
    expected.put(TimeUnit.SEC, "2123546464564.9870000000");
    expected.put(TimeUnit.MIN, "35392441076.083116666666");
    expected.put(TimeUnit.HOUR, "589874017.93471861111111");
    expected.put(TimeUnit.DAY, "24578084.080613275462962");
    expected.put(TimeUnit.WEEK, "3511154.868659039351851");
    expected.put(TimeUnit.MONTH, "808046.599910573439878");
    expected.put(TimeUnit.YEAR, "67337.216659214453323");
    for (Map.Entry<TimeUnit, String> entry : expected.entrySet()) {
        assertThat(entry.getKey().name(),
                StringUtils.substring(timeData.getTimeConverted(entry.getKey()).toString(), 0, 24),
                is(entry.getValue()));/*from  www  .  java  2  s.c o  m*/
    }
}

From source file:org.silverpeas.core.util.time.TestDuration.java

@Test
public void getTimeConverted() {
    Duration duration = createDefaultTimeData();
    Map<TimeUnit, String> expected = new LinkedHashMap<>();
    expected.put(TimeUnit.MILLISECOND, getDefaultTime().toString());
    expected.put(TimeUnit.SECOND, "2123546464564.9870000000");
    expected.put(TimeUnit.MINUTE, "35392441076.083116666666");
    expected.put(TimeUnit.HOUR, "589874017.93471861111111");
    expected.put(TimeUnit.DAY, "24578084.080613275462962");
    expected.put(TimeUnit.WEEK, "3511154.868659039351851");
    expected.put(TimeUnit.MONTH, "808046.599910573439878");
    expected.put(TimeUnit.YEAR, "67337.216659214453323");
    for (Map.Entry<TimeUnit, String> entry : expected.entrySet()) {
        assertThat(entry.getKey().name(),
                StringUtils.substring(duration.getTimeConverted(entry.getKey()).toString(), 0, 24),
                is(entry.getValue()));/*from  w w w  . j  a  v  a 2  s  .c o m*/
    }
}

From source file:org.spicyurl.UrlParser.java

protected void parse() {
    String[] stage0 = StringUtils.splitByWholeSeparatorPreserveAllTokens(url.getRaw(), SCHEME_SEP, 2);

    url.setScheme(stage0[0]);//from www  .j ava2 s . c om
    if (stage0.length != 2) {
        url.getValidationErrorsModifiable().add(UrlErrors.HOST_IS_MISSING);
        return;
    }

    // Check for first separator, to split host from path/query/fragment
    int hostSeperatorIdx = StringUtils.indexOfAny(stage0[1], PATH_SEP, QUERY_SEP, FRAGMENT_SEP);
    if (hostSeperatorIdx == -1) {
        // Just host
        parseLoginHostPort(stage0[1]);
    } else {
        parseLoginHostPort(StringUtils.substring(stage0[1], 0, hostSeperatorIdx));
        parsePathQueryFregment(StringUtils.substring(stage0[1], hostSeperatorIdx));
    }

}

From source file:org.springside.examples.oadata.entity.BulletinDocView.java

@Transient
public String getAttachmentPathShort() {
    return StringUtils.substring(attachmentPath, findFirstGBK(attachmentPath), attachmentPath.length());
}

From source file:org.structr.core.function.AbbrFunction.java

@Override
public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
        throws FrameworkException {

    try {/*www.jav a2  s  .  c o m*/
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {

            return "";
        }

        int maxLength = Double.valueOf(sources[1].toString()).intValue();

        if (sources[0].toString().length() > maxLength) {

            return StringUtils
                    .substringBeforeLast(StringUtils.substring(sources[0].toString(), 0, maxLength), " ")
                    .concat("");

        } else {

            return sources[0];
        }

    } catch (final NumberFormatException nfe) {

        logException(nfe,
                "{0}: NumberFormatException in element \"{1}\". Can not parse \"{2}\" as Integer. Returning original string. Parameters: {3}",
                new Object[] { getName(), entity, sources[1], getParametersAsString(sources) });

        return sources[0];

    } catch (final IllegalArgumentException e) {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

        return usage(ctx.isJavaScriptContext());
    }

}